mirror of
https://github.com/ltcptgeneral/IdealRMT-DecisionTrees.git
synced 2025-09-06 15:27:23 +00:00
Compare commits
5 Commits
61a451b82d
...
afc882a569
Author | SHA1 | Date | |
---|---|---|---|
|
afc882a569 | ||
6de3807fe2 | |||
|
fc16d3c586 | ||
7bee40ecf9 | |||
|
e811171a73 |
128
CompressedTreeParser.ipynb
Normal file
128
CompressedTreeParser.ipynb
Normal file
@@ -0,0 +1,128 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "938dec51",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import argparse\n",
|
||||
"from sklearn.tree import DecisionTreeClassifier, plot_tree, _tree\n",
|
||||
"from sklearn.metrics import accuracy_score\n",
|
||||
"from sklearn.tree import export_graphviz\n",
|
||||
"import pydotplus\n",
|
||||
"from matplotlib import pyplot as plt\n",
|
||||
"from labels import mac_to_label\n",
|
||||
"import json\n",
|
||||
"import math"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "442624c7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"Set1 = pd.read_csv('data.csv').values.tolist()\n",
|
||||
"X = [i[0:3] for i in Set1]\n",
|
||||
"Y =[i[3] for i in Set1]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "12ad454d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"predict_Yt = []\n",
|
||||
"with open('compressed_tree.json', 'r') as file:\n",
|
||||
" data = json.load(file)\n",
|
||||
" classes = data[\"classes\"]\n",
|
||||
" for x in X:\n",
|
||||
" counter = 0\n",
|
||||
" class_set = []\n",
|
||||
" for feature in reversed(data['layers']): #Have to reverse this list due to structure of the data.csv file and how it aligns with the compressed tree layers\n",
|
||||
" for node in data['layers'][feature]:\n",
|
||||
" if node['min'] is None:\n",
|
||||
" if x[counter] < node['max']:\n",
|
||||
" class_set.append(node['classes'])\n",
|
||||
" break #is this an issue?\n",
|
||||
" else:\n",
|
||||
" continue\n",
|
||||
" elif node['max'] is None:\n",
|
||||
" if node['min'] < x[counter]:\n",
|
||||
" class_set.append(node['classes'])\n",
|
||||
" break #is this an issue?\n",
|
||||
" else:\n",
|
||||
" continue\n",
|
||||
" elif node['min'] < x[counter] and x[counter] < node['max']:\n",
|
||||
" class_set.append(node['classes'])\n",
|
||||
" break #is this an issue?\n",
|
||||
"\n",
|
||||
" counter += 1\n",
|
||||
" result = set(class_set[0])\n",
|
||||
" for s in class_set[1:]:\n",
|
||||
" result.intersection_update(s)\n",
|
||||
"\n",
|
||||
" #predict_Yt.append(list(result))\n",
|
||||
" #print(result)\n",
|
||||
" if len(result) == 1:\n",
|
||||
" prediction = list(result)[0]\n",
|
||||
" pred_class = classes[prediction]\n",
|
||||
" predict_Yt.append(pred_class)\n",
|
||||
" else:\n",
|
||||
" predict_Yt.append(None)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "8b4c56b6",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"0.8448217242194891\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"correct = 0\n",
|
||||
"for i in range(len(Y)):\n",
|
||||
" prediction = predict_Yt[i]\n",
|
||||
" if prediction != None and Y[i] == prediction:\n",
|
||||
" correct += 1\n",
|
||||
"\n",
|
||||
"print(correct / len(Y))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.7"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
File diff suppressed because one or more lines are too long
@@ -109,13 +109,13 @@
|
||||
"\t\tvalue = condition[\"value\"]\n",
|
||||
"\n",
|
||||
"\t\t# move the min/max for the corresponding feature in compressed\n",
|
||||
"\t\tif operation == \"<=\" and compressed[feature][\"min\"] is None:\n",
|
||||
"\t\tif operation == \"<=\" and compressed[feature][\"max\"] is None:\n",
|
||||
"\t\t\tcompressed[feature][\"max\"] = value\n",
|
||||
"\t\telif operation == \">\" and compressed[feature][\"max\"] is None:\n",
|
||||
"\t\telif operation == \">\" and compressed[feature][\"min\"] is None:\n",
|
||||
"\t\t\tcompressed[feature][\"min\"] = value\n",
|
||||
"\t\telif operation == \"<=\" and value < compressed[feature][\"min\"]:\n",
|
||||
"\t\telif operation == \"<=\" and value < compressed[feature][\"max\"]:\n",
|
||||
"\t\t\tcompressed[feature][\"max\"] = value\n",
|
||||
"\t\telif operation == \">\" and value > compressed[feature][\"max\"]:\n",
|
||||
"\t\telif operation == \">\" and value > compressed[feature][\"min\"]:\n",
|
||||
"\t\t\tcompressed[feature][\"min\"] = value\n",
|
||||
"\n",
|
||||
"\tpath[\"compressed\"] = compressed"
|
||||
|
@@ -263,7 +263,7 @@
|
||||
"[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
|
||||
"id mapping: \n",
|
||||
"[['dst_range', 'dst_meta'], ['src_range', 'src_meta'], ['protocl_range', 'protocl_meta'], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]\n",
|
||||
"TCAM bits: 3584\n",
|
||||
"TCAM bits: 3520\n",
|
||||
"RAM bits: 522\n"
|
||||
]
|
||||
}
|
||||
|
Reference in New Issue
Block a user