-
Notifications
You must be signed in to change notification settings - Fork 3
/
single_feature.py
119 lines (93 loc) · 3.54 KB
/
single_feature.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
# -*- coding: utf-8 -*-
# This is a file created by Administrator at 2016/8/28
# Project Name: LinkPrediction
# Author: chuanting zhang
# Email: chuanting.zhang@gmail.com
# Redistribution of this code is permitted.
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.cross_validation import train_test_split, StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn import metrics
import matplotlib.pyplot as plt
from scipy import interp
def create_baseline():
model = Sequential()
model.add(Dense(out_dim,
input_dim=in_dim,
init='normal',
activation='relu'
))
model.add(Dense(out_dim/2,
init='normal',
activation='relu'
))
model.add(Dense(1, init='normal', activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model
def build_model_mlp():
np.random.seed(11)
mlp_estimators = list()
mlp_estimators.append(('scaler', StandardScaler()))
mlp_estimators.append(('mlp',
KerasClassifier(build_fn=create_baseline,
nb_epoch=100,
batch_size=50,
verbose=0)))
return mlp_estimators
def train_test(X, Y, ratio):
estimators = build_model_mlp()
clf = Pipeline(estimators)
# clf = RandomForestClassifier(n_jobs=-1, n_estimators=12)
mean_tpr = 0.0
mean_fpr = np.linspace(0., 1., 30)
auc_all = []
folds = StratifiedKFold(Y, n_folds=10,
shuffle=True,
random_state=np.random.randint(1, 100))
# num_of_exp = 1
for i, (train, test) in enumerate(folds):
print "第%d折." % i
# x_train, x_test, y_train, y_test = \
# train_test_split(X, Y,
# test_size=ratio,
# random_state=np.random.randint(1, 100))
x_train, y_train = X[train], Y[train]
x_test, y_test = X[test], Y[test]
clf.fit(x_train, y_train)
y_pred = clf.predict_proba(x_test)[:, 1]
fpr, tpr, _ = metrics.roc_curve(y_test, y_pred)
mean_tpr += interp(mean_fpr, fpr, tpr)
mean_tpr[0] = 0.0
auc_all.append(metrics.roc_auc_score(y_test, y_pred))
mean_tpr /= len(folds)
auc_array = np.array(auc_all)
auc = auc_array.mean()
auc_std = auc_array.std()
mean_tpr[-1] = 1.0
return mean_tpr, auc, auc_std
fp_hand = 'e:/speak_data/data2/balanced_sample/first_4hop_hand_1.csv'
df_hand = pd.read_csv(fp_hand, header=0)
df_hand = df_hand.reindex(np.random.permutation(df_hand.index))
df_hand = df_hand.drop(['tag', 'aff', 'sp'], axis=1)
data = df_hand.values
Y = data[:, -3].astype(int)
X = data[:, 0:-3].astype(float)
X = StandardScaler().fit_transform(X)
in_dim = X.shape[1]
out_dim = 2*in_dim
final_fpr = np.linspace(0., 1., 30)
# MLP
test_size = 0.2
mlp_tpr, mlp_auc, mlp_std = train_test(X, Y, test_size)
print("MLP AUC: %.3f(%.3f)" % (mlp_auc, mlp_std))
plt.plot(final_fpr, mlp_tpr, '--ro', label='MLP AUC: %.3f(%.3f)' % (mlp_auc, mlp_std))
plt.legend()
plt.grid()
plt.show()