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_Taklif3.py
191 lines (143 loc) · 6.23 KB
/
Demo_Taklif3.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from sklearn import tree
from sklearn import svm
import numpy as np
import pandas as pd
from sklearn import metrics
enb_frame = pd.read_csv('ENB2012_data.csv')
enb=enb_frame.values
X = enb[:768,:8]
y = enb[:768,8]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train,y_test= train_test_split(X, y,test_size=0.2 )
model = tree.DecisionTreeRegressor(max_depth=5)
tree_model = model.fit(X_train, y_train)
predict = tree_model.predict(X_train)
# metrics
from sklearn.metrics import r2_score, mean_squared_error
print("MSE error is :", mean_squared_error(y_train, predict))
print("r2 score is :", r2_score(y_train, predict))
from sklearn.linear_model import LinearRegression
regr = LinearRegression()
regr.fit(X_train, y_train)
predictReg = regr.predict(X_train)
print("MSE error is :", mean_squared_error(y_train, predictReg))
print("r2 score is :", r2_score(y_train, predictReg))
###reading data
Autism=pd.read_csv('Autism-Adult.csv')
nanind=[]
for ind in range( len(Autism['age numeric'])):
if (Autism['age numeric'][ind]=='?'):
nanind.append(ind)
Autism=Autism.drop(nanind)
X=Autism.values[:,:12]
y=Autism.values[:,12]
y_edit= np.array([1 if yinstance=='yes' else 0 for yinstance in y ])
X[:,11]=np.array([1 if xinstance=='f' else 0 for xinstance in X[:,11] ])
X_train,X_test,y_train,y_test=train_test_split(X,y_edit,test_size=0.2,random_state=0)
### tree based methods for classification
# criterion = 'gini' or 'entropy'
tree_classifier = tree.DecisionTreeClassifier(criterion='entropy', max_depth=10)
tree_model = tree_classifier.fit(X_train, y_train)
predict = tree_model.predict(X_test)
print("Accuracy:",metrics.accuracy_score(y_test, predict))
print("Precision:",metrics.precision_score(y_test, predict))
print("Recall:",metrics.recall_score(y_test, predict))
print("F_measure:",metrics.f1_score(y_test, predict))
print( "classification error is :", np.sum(predict != y_test) / len(y_test) )
########## Bagging method ######
from sklearn.ensemble import BaggingClassifier, RandomForestClassifier
bagging = BaggingClassifier()
bagging_model = bagging.fit(X_train, y_train)
bagging_predict = bagging_model.predict(X_test)
print("Accuracy:",metrics.accuracy_score(y_test, bagging_predict ))
print("Precision:",metrics.precision_score(y_test, bagging_predict ))
print("Recall:",metrics.recall_score(y_test, bagging_predict ))
print("F_measure:",metrics.f1_score(y_test, bagging_predict ))
print( "classification error is :", np.sum(bagging_predict != y_test) / len(y_test) )
########### Random Forest ###########
random_forest = RandomForestClassifier(min_samples_split=5, min_samples_leaf=2, max_depth=10)
random_forest_model = random_forest.fit(X_train, y_train)
random_forest_predict = random_forest_model.predict(X_test)
print("Accuracy:",metrics.accuracy_score(y_test, random_forest_predict))
print("Precision:",metrics.precision_score(y_test, random_forest_predict ))
print("Recall:",metrics.recall_score(y_test, random_forest_predict ))
print("F_measure:",metrics.f1_score(y_test, random_forest_predict ))
print( "classification error is :", np.sum(random_forest_predict != y_test) / len(y_test) )
svmachine = svm.SVC(gamma='auto',kernel='linear')
svm_model = svmachine.fit(X_train, y_train)
y_pred = svm_model.predict(X_test)
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))
print("F_measure:",metrics.f1_score(y_test, y_pred))
print( "classification error is :", np.sum(svm_model.predict(X_test) != y_test) / len(y_test) )
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression()
logregmodel = logreg.fit(X_train, y_train)
y_pred = logregmodel.predict(X_test)
########Confusion matrix
from sklearn import metrics
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))
##########Naive Bayes #########
from sklearn.naive_bayes import GaussianNB
GNB = GaussianNB()
GNBmodel = GNB.fit(X_train, y_train)
y_pred = GNBmodel.predict(X_test)
########Confusion matrix
from sklearn import metrics
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))
print("F_measure:",metrics.f1_score(y_test, y_pred))
######QDA ###########
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
QDA= QuadraticDiscriminantAnalysis()
QDAmodel = QDA.fit(X_train, y_train)
y_pred = QDAmodel.predict(X_test)
########Confusion matrix
from sklearn import metrics
cnf_matrix = metrics.confusion_matrix(y_test, y_pred)
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))
print("F_measure:",metrics.f1_score(y_test, y_pred))
######LDA ###########
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
LDA= LinearDiscriminantAnalysis()
LDAmodel = LDA.fit(X_train, y_train)
y_pred = LDAmodel.predict(X_test)
########Confusion matrix
from sklearn import metrics
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))
print("F_measure:",metrics.f1_score(y_test, y_pred))
###### Classification with 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
from sklearn import metrics
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))
print("F_measure:",metrics.f1_score(y_test, y_pred))
1+1