-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChapter12AuditAlgorithm.py
298 lines (256 loc) · 6.12 KB
/
Chapter12AuditAlgorithm.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 14 10:40:55 2018
@author: Administrator
"""
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.model_selection import KFold
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.datasets import load_boston
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.linear_model import Lasso
from sklearn.linear_model import ElasticNet
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.svm import SVR
'''
......审查分类算法.......
'''
'''
逻辑回归
'''
#读取数据
filename='diabetes.csv'
names=['preg','plas','pres','skin','test','mass','pedi','age','class']
data=pd.read_csv(filename)
#将数据分为输入数据和输出数据
array=data.values
X=array[:,0:8]
y=array[:,8]
num_folds=10
seed=7
kfold=KFold(n_splits=num_folds,random_state=seed)
model=LogisticRegression()
result=cross_val_score(model,X,y,cv=kfold)
print("逻辑回归",result.mean())
'''
LDA算法
'''
#将数据分为输入数据和输出数据
array=data.values
X=array[:,0:8]
y=array[:,8]
num_folds=10
seed=7
kfold=KFold(n_splits=num_folds,random_state=seed)
model=LinearDiscriminantAnalysis()
result=cross_val_score(model,X,y,cv=kfold)
print("LDA算法",result.mean())
'''
非线性算法
'''
'''
KNN
'''
#将数据分为输入数据和输出数据
array=data.values
X=array[:,0:8]
y=array[:,8]
num_folds=10
seed=7
kfold=KFold(n_splits=num_folds,random_state=seed)
model=KNeighborsClassifier()
result=cross_val_score(model,X,y,cv=kfold)
print("KNN算法",result.mean())
'''
Bayes
'''
#将数据分为输入数据和输出数据
array=data.values
X=array[:,0:8]
y=array[:,8]
num_folds=10
seed=7
kfold=KFold(n_splits=num_folds,random_state=seed)
model=GaussianNB()
result=cross_val_score(model,X,y,cv=kfold)
print("Bayes算法",result.mean())
'''
DecisionTree
'''
#将数据分为输入数据和输出数据
array=data.values
X=array[:,0:8]
y=array[:,8]
num_folds=10
seed=7
kfold=KFold(n_splits=num_folds,random_state=seed)
model=DecisionTreeClassifier()
result=cross_val_score(model,X,y,cv=kfold)
print("DecisionTree算法",result.mean())
'''
SVM
'''
#将数据分为输入数据和输出数据
array=data.values
X=array[:,0:8]
y=array[:,8]
num_folds=10
seed=7
kfold=KFold(n_splits=num_folds,random_state=seed)
model=SVC()
result=cross_val_score(model,X,y,cv=kfold)
print("SVM算法",result.mean())
'''
......审查回归算法.......
波士顿房价数据集
'''
'''
线性回归算法
'''
datasets=load_boston()
X=datasets.data
y=datasets.target
n_splits=10
seed=7
kflod=KFold(n_splits=n_splits,random_state=seed)
model=LinearRegression()
#均方误差
scoring='neg_mean_squared_error'
result=cross_val_score(model,X,y,scoring=scoring,cv=kflod)
print("线性回归算法",result.mean())
'''
岭回归
'''
datasets=load_boston()
X=datasets.data
y=datasets.target
n_splits=10
seed=7
kflod=KFold(n_splits=n_splits,random_state=seed)
model=Ridge()
#均方误差
scoring='neg_mean_squared_error'
result=cross_val_score(model,X,y,scoring=scoring,cv=kflod)
print("岭回归算法",result.mean())
'''
Lasso回归
'''
datasets=load_boston()
X=datasets.data
y=datasets.target
n_splits=10
seed=7
kflod=KFold(n_splits=n_splits,random_state=seed)
model=Lasso()
#均方误差
scoring='neg_mean_squared_error'
result=cross_val_score(model,X,y,scoring=scoring,cv=kflod)
print("Lasso回归算法",result.mean())
'''
弹性网络回归
'''
datasets=load_boston()
X=datasets.data
y=datasets.target
n_splits=10
seed=7
kflod=KFold(n_splits=n_splits,random_state=seed)
model=ElasticNet()
#均方误差
scoring='neg_mean_squared_error'
result=cross_val_score(model,X,y,scoring=scoring,cv=kflod)
print("弹性网络回归算法",result.mean())
'''
KNN回归
'''
datasets=load_boston()
X=datasets.data
y=datasets.target
n_splits=10
seed=7
kflod=KFold(n_splits=n_splits,random_state=seed)
model=KNeighborsRegressor()
#均方误差
scoring='neg_mean_squared_error'
result=cross_val_score(model,X,y,scoring=scoring,cv=kflod)
print("KNN回归算法",result.mean())
'''
回归树
'''
datasets=load_boston()
X=datasets.data
y=datasets.target
n_splits=10
seed=7
kflod=KFold(n_splits=n_splits,random_state=seed)
model=DecisionTreeRegressor()
#均方误差
scoring='neg_mean_squared_error'
result=cross_val_score(model,X,y,scoring=scoring,cv=kflod)
print("回归树算法",result.mean())
'''
SVM回归
'''
datasets=load_boston()
X=datasets.data
y=datasets.target
n_splits=10
seed=7
kflod=KFold(n_splits=n_splits,random_state=seed)
model=SVR()
#均方误差
scoring='neg_mean_squared_error'
result=cross_val_score(model,X,y,scoring=scoring,cv=kflod)
print("SVM回归算法",result.mean())
'''
机器学习算法的比较
糖尿病数据
'''
#读取数据
filename='diabetes.csv'
names=['preg','plas','pres','skin','test','mass','pedi','age','class']
data=pd.read_csv(filename)
#将数据分为输入数据和输出数据
array=data.values
X=array[:,0:8]
y=array[:,8]
num_folds=10
seed=7
kfold=KFold(n_splits=num_folds,random_state=seed)
models={}
models['LR']=LogisticRegression()
models['LDA']=LinearDiscriminantAnalysis()
models['KNN']=KNeighborsClassifier()
models['CART']=DecisionTreeClassifier()
models['SVM']=SVC()
models['NB']=GaussianNB()
results=[]
for name in models:
result=cross_val_score(models[name],X,y,cv=kfold)
results.append(result)
msg='%s:%.3f(%.3f)'%(name,result.mean(),result.std())
print(msg)
#图表显示
fig=plt.figure()
#标题
fig.suptitle('Algorithm Comparison')
#画出子图,参数一:子图总行数,参数二:子图总列数;参数三:子图位置
ax=fig.add_subplot(111)
#画出箱线图
plt.boxplot(results)
#设置x轴名称
ax.set_xticklabels(models.keys())
plt.show()