-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.py
102 lines (83 loc) · 3.21 KB
/
Code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
‘‘‘Importing the function’’’
from sklearn import tree
from sklearn.neural_network import MLPClassifier
#import matplotlib.pyplot as plt
from sklearn.naive_bayes import GaussianNB as p
from sklearn.metrics import confusion_matrix
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score
from sklearn import svm
import math
import numpy as np
import pandas as pd
#‘‘‘K-Nearest Neighbors Function’’’
def KNN(x_training,y_training,x_testing,y_testing):
print("\nKNN")
clf=KNeighborsClassifier(n_neighbors=9)
clf.fit(x_training,y_training)
cvs=cross_val_score(clf,x_testing,y_testing,cv=5)
result=clf.predict(x_testing)
cmknn=confusion_matrix(y_testing,result)
print("Confusion_Matrix\n\n",cmknn)
print("Accuracy =",((cmknn[0][0]+cmknn[1][1])/len(y_testing))*100)
print("Mean=",cvs.mean())
#‘‘‘NaiveB Function’’’
def NaiveB(x_training,y_training,x_testing,y_testing):
print("\n\nNaiveBaysian")
gnb=p()
gnb.fit(x_training,y_training)
y_pre=gnb.predict(x_testing)
cm=confusion_matrix(y_testing,y_pre)
print("Confusion_Matrix\n",cm)
print("Accuracy=",((cm[0][0]+cm[1][1])/len(y_testing)*100))
#‘‘‘Decision Tree Function’’’
def DecisionTree(x_training,y_training,x_testing,y_testing):
print("\n\nDecision Tree")
clf=tree.DecisionTreeClassifier()
clf=clf.fit(x_training,y_training)
y_predict=clf.predict(x_testing)
CM=confusion_matrix(y_testing,y_predict)
accuracy=sum(CM.diagonal())/len(y_test)
print("Confusion_Matrix\n",CM)
print("Accuracy=",accuracy*100)
#‘‘‘Artificial Neural Network Function’’’
def NeuralNetwork(x_training,y_training,x_testing,y_testing):
print("\n\n Artificial Neural Network")
clf = MLPClassifier(solver='lbfgs', alpha=1e-
5,hidden_layer_sizes=(5,3),random_state=1) #default hidden layer=100
clf.fit(x_training,y_training) #The default solver ‘adam’ works pretty well on
relatively large datasets
y_predict=clf.predict(x_testing)
cm=confusion_matrix(y_testing,y_predict)
print("Confusion matrix\n\n",cm)
print("Accuracy=",sum(cm.diagonal())/len(x_testing)*100)
#‘‘‘Support Vector Machine Function’’’
def SVM(x_training,y_training,x_testing,y_testing):
print("\n\nSVM")
clf = svm.SVC(gamma='auto')
clf.fit(x_training,y_training)
y_predict=clf.predict(x_testing)
cm=confusion_matrix(y_testing,y_predict)
print("Confusion matrix\n\n",cm)
print("Accuracy=",sum(cm.diagonal())/len(x_testing)*100)
#‘‘‘Algorithm’’’
def algorithm(x_train,y_train,x_test,y_test):
acc=[]
NaiveB(x_train,y_train,x_test,y_test,acc)
KNN(x_train,y_train,x_test,y_test,acc)
DecisionTree(x_train,y_train,x_test,y_test,acc)
NeuralNetwork(x_train,y_train,x_test,y_test,acc)
SVM(x_train,y_train,x_test,y_test,acc)
#‘‘‘Importing csv file’’’
d=pd.read_csv("breast_cancer_weka_dataset.csv")
c=d.copy()
x_train=c.loc[:350,:"mitosis"]
y_train=c.loc[:350,"class"]
x_test=c.loc[350:,:"mitosis"]
y_test=c.loc[350:,"class"]
#‘‘‘Applying function’’’
NaiveB(x_train,y_train,x_test,y_test)
KNN(x_train,y_train,x_test,y_test)
DecisionTree(x_train,y_train,x_test,y_test)
NeuralNetwork(x_train,y_train,x_test,y_test)
SVM(x_train,y_train,x_test,y_test)