-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm_rf_lg_rfe.py
195 lines (167 loc) · 6.49 KB
/
svm_rf_lg_rfe.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
import numpy as np
from sklearn.feature_selection import RFE, RFECV
from sklearn.svm import SVC, SVR
from sklearn.svm import LinearSVC
from sklearn import model_selection
import pandas as pd
from imblearn.under_sampling import RandomUnderSampler
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
from matplotlib import pyplot as plt
from imblearn.combine import SMOTEENN
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix,classification_report
from sklearn.metrics import f1_score,precision_score,recall_score,roc_auc_score,accuracy_score,roc_curve
from sklearn.preprocessing import StandardScaler,RobustScaler,MinMaxScaler
ee = SMOTEENN()
ss = RobustScaler()
class RandomForestClassifierWithCoef(RandomForestClassifier):
def fit(self, *args, **kwargs):
super(RandomForestClassifierWithCoef, self).fit(*args, **kwargs)
self.coef_ = self.feature_importances_
rf = RandomForestClassifierWithCoef(n_estimators=500, min_samples_leaf=5, n_jobs=-1)
def svmRFE(features,label):
ee = SMOTEENN()
features, label = ee.fit_sample(features, label)
label.astype(np.int)
X_train, X_test, y_train, y_test = train_test_split(features, label, test_size=0.3, random_state=66)
y = y_train
#print(y.head())
x = X_train
print(features.head())
print('开始训练特征')
svc = SVC(kernel="linear", C=1, probability=True)
rfe = RFE(estimator=svc, n_features_to_select=3, step=1)
rfe.fit(x, y)
print('训练特征选择完成,输出特征:')
result = features.columns[rfe.get_support()]
print(result)
print(rfe.ranking_)
re = RFECV(estimator=svc, step=1, cv=5)
re.fit(x, y)
result2 = features.columns[re.get_support()]
y_pred2 = re.predict(X_test)
y_proba2 = re.predict_proba(X_test)[:, 1]
print(classification_report(y_test, y_pred2, digits=2))
print(result2)
print('特征输出完毕')
fpr1, tpr1, thresholds1 = roc_curve(y_true=y_test, y_score=y_proba2)
return fpr1,tpr1
def rfRFE(features,label):
ee = RandomUnderSampler(random_state=0)
features, label = ee.fit_sample(features, label)
label.astype(np.int)
X_train, X_test, y_train, y_test = train_test_split(features, label, test_size=0.3, random_state=66)
y = y_train
#print(y.head())
x = X_train
print(features.head())
print('开始训练特征')
rf = RandomForestClassifierWithCoef(n_estimators=500, min_samples_leaf=5, n_jobs=-1)
re = RFECV(estimator=rf, step=1, cv=10)
re.fit(x, y)
result2 = features.columns[re.get_support()]
y_pred2 = re.predict(X_test)
y_proba2 = re.predict_proba(X_test)[:, 1]
print(classification_report(y_test, y_pred2, digits=2))
print(result2)
print(re.ranking_)
print('特征输出完毕')
fpr1, tpr1, thresholds1 = roc_curve(y_true=y_test, y_score=y_proba2)
return fpr1,tpr1
def lgRFE(features,label):
ee = RandomUnderSampler(random_state=6)
features, label = ee.fit_sample(features, label)
label.astype(np.int)
X_train, X_test, y_train, y_test = train_test_split(features, label, test_size=0.5, random_state=6)
y = y_train
#print(y.head())
x=X_train
#x = ss.fit_transform(X_train)
#X_test=ss.fit_transform(X_test)
print(features.head())
print('开始训练特征')
rf = LogisticRegression(max_iter=10000)
re = RFECV(estimator=rf, step=1, cv=10)
re.fit(x, y)
result2 = features.columns[re.get_support()]
y_pred2 = re.predict(X_test)
y_proba2 = re.predict_proba(X_test)[:, 1]
print(classification_report(y_test, y_pred2, digits=2))
print(result2)
print(re.ranking_)
print('特征输出完毕')
fpr1, tpr1, thresholds1 = roc_curve(y_true=y_test, y_score=y_proba2)
return fpr1,tpr1
df=pd.read_table("HiSeqV2_BRCA_outcome.txt",header=0,index_col=0)
df=df.T
#df = pd.read_csv('E:\Pycharm\PycharmLearning\RF\\RFEtestyu1012.csv', encoding='gbk')
print(df.head())
module1=[]
module2=[]
module3=[]
module4=[]
module5=[]
moduleinfo=pd.read_csv("module_2.csv",index_col=None,header=None)
moduleinfo=moduleinfo.T
print(moduleinfo.head())
for i in range(0,len(moduleinfo.iloc[:,0])):
if moduleinfo.iloc[i,1]=="1":
module1.append(moduleinfo.iloc[i,0])
if moduleinfo.iloc[i,1]=="2":
module2.append(moduleinfo.iloc[i,0])
if moduleinfo.iloc[i,1]=="3":
module3.append(moduleinfo.iloc[i,0])
if moduleinfo.iloc[i,1]=="4":
module4.append(moduleinfo.iloc[i,0])
if moduleinfo.iloc[i, 1] == "5":
module5.append(moduleinfo.iloc[i, 0])
print("module1",module4)
for i in range(1,6):
if i == 1:
features = df[module1]
print(len(module1))
label = df.Label
fpr1,tpr1=svmRFE(features,label)
if i == 2:
features = df[module2]
print(len(module2))
label = df.Label
fpr2,tpr2=svmRFE(features,label)
if i == 3:
features = df[module3]
print(len(module3))
label = df.Label
fpr3,tpr3=svmRFE(features, label)
if i == 4:
features = df[module4]
print(len(module4))
label = df.Label
fpr4, tpr4 = svmRFE(features, label)
if i == 5:
features = df[module5]
print(len(module5))
label = df.Label
fpr5, tpr5 = svmRFE(features, label)
plt.style.use('seaborn-darkgrid')
Font={'size':18, 'family':'Times New Roman'}
Font2={'size':17, 'family':'Times New Roman'}
plt.figure(figsize=(8,6))
plt.tick_params(labelsize=15)
plt.xlim(0,1) ##设定x轴的范围
plt.ylim(0.0,1.1) ## 设定y轴的范围
plt.xlabel('False Postive Rate',Font2)
plt.ylabel('True Postive Rate',Font2)
roc_auc1 = metrics.auc(fpr1, tpr1)
roc_auc2 = metrics.auc(fpr2, tpr2)
roc_auc3 = metrics.auc(fpr3, tpr3)
roc_auc4 = metrics.auc(fpr4, tpr4)
roc_auc5 = metrics.auc(fpr5, tpr5)
plt.plot(fpr1, tpr1, 'b', label='Module1 = %0.3f' % roc_auc1 , color='Red',linewidth=2)
plt.plot(fpr2, tpr2, 'b', label='Module2 = %0.3f'% roc_auc2 , color='k',linewidth=2)
plt.plot(fpr3, tpr3, 'b', label='Module3 = %0.3f' % roc_auc3, color='RoyalBlue',linewidth=2)
plt.plot(fpr4,tpr4,label='Module4 = %0.3f' % roc_auc4, color='seagreen',linewidth=2)
plt.plot(fpr5,tpr5,label='Module5 = %0.3f' % roc_auc5, color='gold',linewidth=2)
plt.legend(loc = 'lower right', prop=Font)
plt.show()