Monday 1 April 2019

Classification using python3, sklearn with simple example

from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn import svm
from sklearn.metrics import accuracy_score,confusion_matrix

features=[[1.2,1.3],[1.15,1.25],[1.02,1.23],[1.12,1.13]]
labels=[1,1,0,0]
train,test,train_labels,test_labels=train_test_split(features,labels,test_size=0.33)
#gnb=GaussianNB()
gnb = svm.SVC(gamma=0.001)
model=gnb.fit(train,train_labels)
predictions=gnb.predict(test)
print(confusion_matrix(test_labels,predictions))
print(accuracy_score(test_labels,predictions))
# refer https://scikit-learn.org/stable/modules/svm.html
#here i used svm classifier, if u use a DecisionTreeClassifier() and plot the decision tree
#follow the video https://www.youtube.com/watch?v=yU87snVC_Go
from sklearn import tree
from io import StringIO
from IPython.display import Image
with open("classifier.txt","w") as f:
f= tree.export_graphviz(classifier, out_file=f)

# A text file by name classifier will be saved to your current working directory
#Copy the contents of the file and go webgraphviz.com and paste it and click on
#Generate Graph button


No comments:

Post a Comment