forked from rucliyang/Intro2ds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchap06-ML.py
170 lines (117 loc) · 3.52 KB
/
chap06-ML.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
import os
os.chdir("D:\\ml")
################
## 回归分析 ##
################
import pandas as pd
d0 = pd.read_excel('lm.xlsx')
import statsmodels.api as sm
X = d0[['x1', 'x2']]
y = d0['y']
m1 = sm.OLS(y, sm.add_constant(X))
m1.fit().summary()
################
## 主成分分析 ##
################
d1 = pd.read_csv("decathlon.csv")
d1 = d1.drop(['name'], axis=1)
from sklearn.decomposition import PCA
pca1 = PCA(n_components=2)
pca1.fit(d1)
pca1.explained_variance_ratio_
################
## 聚类分析 ##
################
# 层次聚类
import pandas as pd
import scipy.cluster.hierarchy as hcluster
d1 = pd.read_excel("football.xlsx")
d1.index = d1['team']
d1 = d1.drop(['team'], axis=1)
h1 = hcluster.fclusterdata(d1, criterion = 'maxclust', t = 3)
h1
# K-Means 聚类分析(分为两类)
import matplotlib.pyplot as plt
import numpy as np
from sklearn.cluster import KMeans
km1 = KMeans(n_clusters = 2)
km1.fit(d1)
km1.cluster_centers_
km1.labels_
# DBSCAN
d1 = pd.read_csv("cludata.csv")
from sklearn.cluster import DBSCAN
c1 = DBSCAN(eps = 0.5, min_samples = 4)
c1.fit(d1)
c1.labels_
plt.scatter('x', 'y', data = d1[c1.labels_ == 0], c = "red", marker='o', label='0')
plt.scatter('x', 'y', data = d1[c1.labels_ == 1], c = "blue", marker='o', label='1')
plt.show()
##########################
## LDA分类及性能评价 ##
##########################
import pandas as pd
d1 = pd.read_excel("diabetes.xlsx")
X = d1.drop(['class'], axis=1)
y = d1['class']
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
m1 = LinearDiscriminantAnalysis()
m1.fit(X, y)
m1.coef_
m1.intercept_
m1.score(X, y)
p1 = m1.predict(X)
from sklearn.metrics import classification_report
print(classification_report(y, p1, labels=['pos', 'neg']))
from sklearn.metrics import roc_auc_score
prob1 = m1.predict_proba(X)
print(roc_auc_score(y, prob1[:,1]))
from sklearn.metrics import roc_curve
import matplotlib.pyplot as plt
fpr, tpr, thresholds = roc_curve(y,prob1[:,1], pos_label = "pos")
plt.plot(fpr,tpr)
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(X,y, test_size=0.4)
m1.fit(x_train, y_train)
p1 = m1.predict(x_test)
print(classification_report(y_test, p1, labels=['pos', 'neg']))
####################
## 其他分类算法 ##
####################
# 逻辑斯蒂回归
from sklearn.linear_model import LogisticRegression
m2 = LogisticRegression()
m2.fit(X, y)
m2.coef_
m2.intercept_
m2.score(X, y)
x_train, x_test, y_train, y_test = train_test_split(X,y, test_size=0.1)
m2.fit(x_train, y_train)
m2.score(x_test, y_test)
# 决策树
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import export_graphviz
from sklearn.feature_extraction import DictVectorizer
m3 = DecisionTreeClassifier()
m3.fit(X, y)
m3.score(X, y)
x_train, x_test, y_train, y_test = train_test_split(X,y, test_size=0.1)
m3.fit(x_train, y_train)
m3.score(x_test, y_test)
# 随机森林
from sklearn.ensemble import RandomForestClassifier
m4 = RandomForestClassifier()
m4.fit(X, y)
m4.score(X, y)
x_train, x_test, y_train, y_test = train_test_split(X,y, test_size=0.1)
m4.fit(x_train, y_train)
m4.score(x_test, y_test)
# SVM
from sklearn.svm import SVC
m5 = SVC()
m5.fit(X, y)
m5.score(X, y)
x_train, x_test, y_train, y_test = train_test_split(X,y, test_size=0.1)
m5.fit(x_train, y_train)
m5.score(x_test, y_test)