This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Demo_Taklif2.py
165 lines (113 loc) · 4.18 KB
/
Demo_Taklif2.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import numpy as np
import pandas as pd
data1 = pd.read_csv("breast-cancer.csv")
X=data1.values[:,1:10]
y=data1.values[:,10]
y=np.array([1 if yinstance==4 else 0 for yinstance in y ])
missing_value=['?']
data2= pd.read_csv("breast-cancer.csv", na_values=missing_value)
print(data2.isnull().sum())
nanArray=data2['Bare Nuclei'].isnull()
nanind=[]
for ind in range( len(data2['Bare Nuclei'])):
if (nanArray[ind]==True):
nanind.append(ind)
print(nanind)
# ##removing missing values
# data2=data2.drop(nanind)
##replacing
bmedian = data2['Bare Nuclei'].median()
data2['Bare Nuclei'].fillna(bmedian,inplace=True)
X=data2.values[:,1:10]
y=data2.values[:,10]
y=np.array([1 if yinstance==4 else 0 for yinstance in y ])
### split
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3,random_state=0)
####LogisticRegression
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression()
LogregModel = logreg.fit(X_train, y_train)
predicts=LogregModel.predict(X_train)
from sklearn import metrics
print("Accuracy:",metrics.accuracy_score(y_train, predicts))
print("Error:",1-metrics.accuracy_score(y_train, predicts))
####naive bayes
from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
gnbModel =gnb.fit(X_train, y_train)
####Linear Regression
from sklearn.linear_model import LinearRegression
LR= LinearRegression()
LRmodel = LR.fit(X_train, y_train)
y_regpred = LRmodel.predict(X_test)
y_pred= [1 if x>=0.4 else 0 for x in y_regpred]
########Confusion matrix
cnf_matrix = metrics.confusion_matrix(y_pred , y_test)
print(cnf_matrix)
#####Metrics
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
print("Precision:",metrics.precision_score(y_test , y_pred))
print("Recall:",metrics.recall_score(y_test, y_pred))
###K_fold
#######LogisticRegression
from sklearn.model_selection import KFold
kf = KFold(n_splits=10,shuffle=True)
kfsplit = kf.get_n_splits(X)
kfoldlogreg =LogisticRegression()
KFoldACC = []
KFoldPREC = []
KFoldREC = []
for train_index, test_index in kf.split(X):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
kfoldlogreg.fit(X_train, y_train)
logpredict=kfoldlogreg.predict(X_test)
KFoldACC.append(metrics.accuracy_score(y_test,logpredict))
KFoldPREC.append(metrics.precision_score(y_test,logpredict))
KFoldREC.append(metrics.recall_score(y_test,logpredict))
print("Accuracy for logistic-10foldCV",np.mean(KFoldACC))
print("Precsion for logistic-10foldCV",np.mean(KFoldPREC))
print("Recall for logistic-10foldCV",np.mean(KFoldREC))
###LinearRegression
kfoldregr = LinearRegression()
acclist=[]
prelist=[]
reclist=[]
for train_index, test_index in kf.split(X):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
kfoldregrmodel = kfoldregr.fit(X_train, y_train)
predictions = kfoldregr.predict(X_test)
regrpredic = np.array([1 if x >= 0.5 else 0 for x in predictions])
acc = metrics.accuracy_score(y_test, regrpredic)
acclist.append(acc)
recall = metrics.recall_score(y_test, regrpredic)
reclist.append(recall)
precession = metrics.precision_score(y_test, regrpredic)
prelist.append(precession)
print('Kfold accuracy',np.mean(acclist))
print('Kfold precession',np.mean(prelist))
print('Kfold recall: ',np.mean(reclist))
####Leave One Out Cross Validation
##Logistic Regression
from sklearn.model_selection import LeaveOneOut
loo = LeaveOneOut()
loo.get_n_splits(X)
loologreg = LogisticRegression()
predicts=[]
for train_index, test_index in loo.split(X):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
loologregModel =loologreg.fit(X_train, y_train)
predicts.append(loologreg.predict(X_test))
predict = np.array(predicts)
cnf_matrix = metrics.confusion_matrix(y, predict)
print(cnf_matrix)
acc = metrics.accuracy_score(y, predict)
print("Logistic Regression Accuracy by LOOCV", acc)
recall = metrics.recall_score(y, predict)
print("Logistic Regression Recall by LOOCV", recall)
precession = metrics.precision_score(y, predict)
print("Logistic Regression Precession by LOOCV", precession)
1+1