Upload Vol.py File

parent 40bcb354
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.impute import SimpleImputer
from sklearn.metrics import accuracy_score
from sklearn.metrics import f1_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
# Importing the dataset
train = pd.read_csv("/Users/ASHEN/Education/SLIIT/Research/Volumetric/packet_data.csv",index_col=0,low_memory=False)
train.shape
# Feature Scaling
from sklearn import preprocessing
for f in train.columns:
if train[f].dtype=='object':
label = preprocessing.LabelEncoder()
label.fit(list(train[f].values))
train[f] = lbl.transform(list(train[f].values))
train.fillna((-999), inplace=True)
train=np.array(train)
train = train.astype(float)
Y = train['Label']
X = train.drop("Label",axis=1)
print(train.shape)
print(X.shape)
print(Y.shape)
# Spliting the dataset into the training set and test set
seed = 7
test_size = 0.33
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=test_size, random_state=seed)
print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)
# Fitting Decision Tree to the training set
model=DecisionTreeClassifier(max_depth=5,random_state=0)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(model)
# Plotting the tree, here it should access graph's first element
from sklearn import tree
from sklearn.externals.six import StringIO
import pydot
dot_data = StringIO()
tree.export_graphviz(model, out_file=dot_data)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph[0].write_pdf("tree.pdf")
# Predicting the data[round(value) for value in y_pred]
y_pred = model.predict(X_test)
# Creating confusion metrics to check the performance of algorithm
cm=confusion_matrix(y_test, y_pred)
print("confusion matrix:\n",cm)
# Accuracy
accuracy = accuracy_score(y_test, y_pred)
print("accuracy:",accuracy)
# F1 Score
f1score=f1_score(y_test, y_pred)
print("f1_score:",f1score)
# Precision Score
pr=precision_score(y_test,y_pred)
print("Precision:",pr)
# Recall Score
rs=recall_score(y_test,y_pred)
print("Recall_score:",rs)
# Misclassified Samples
misclassified_samples = X_test[y_test != y_pred]
mc=misclassified_samples.shape[0]
print("Misclassified :",mc)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment