-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultilayer_perceptron.py
143 lines (84 loc) · 3.33 KB
/
Multilayer_perceptron.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
import numpy as np
import pandas as pd
from tensorflow import set_random_seed
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, Dropout
from tensorflow.keras.utils import to_categorical, plot_model
from sklearn.metrics import confusion_matrix
from eli5.sklearn import PermutationImportance
def permutation_importance(colnames,model,niter,test_data,y_test,random_state):
sub_data=test_data.to_numpy()
iloss, iacc = model.evaluate(sub_data, y_test, batch_size=batch_size)
importances=np.zeros(len(colnames))
print(test_data.shape)
for i in range(0, len(colnames)):
sub_imp=np.zeros(niter)
for j in range(0,niter):
sub_pd=test_data.copy(deep=True)
sub_pd[colnames[i]]=np.random.permutation(sub_pd[colnames[i]])
sub_pd=sub_pd.to_numpy()
closs, cacc = model.evaluate(sub_pd, y_test, batch_size=batch_size)
sub_imp[j]=iacc-cacc
importances[i]=np.mean(sub_imp)
importances_pd=pd.DataFrame(list(zip(feature_names,importances)),columns=["feature","weight"])
return importances_pd
fname="taxa_data_nested_genera.tsv"
train_taxa=pd.read_csv(fname,sep="\t")
train_taxa
y_train=train_taxa['events'].values
random_state=20
np.random.seed(random_state)
num_labels = len(np.unique(y_train))
set_random_seed(random_state)
num_labels
y_train = to_categorical(y_train)
train_taxa=train_taxa.drop(columns=['events',"X.OTU.ID"])
feature_names=train_taxa.columns
taxa_train_perm=train_taxa.copy(deep=True)
train_taxa=train_taxa.to_numpy()
train_taxa
train_taxa= train_taxa.astype('float32')
input_size=train_taxa.shape[1]
input_size
batch_size = 128
hidden_units = 256
dropout = 0.03
model = Sequential()
model.add(Dense(hidden_units, input_dim=input_size))
model.add(Activation('sigmoid'))
model.add(Dropout(dropout))
model.add(Dense(hidden_units))
model.add(Activation('sigmoid'))
model.add(Dropout(dropout))
model.add(Dense(hidden_units))
model.add(Activation('sigmoid'))
model.add(Dropout(dropout))
model.add(Dense(num_labels))
model.add(Activation('softmax'))
model.summary()
plot_model(model, to_file='mlp-mnist.png', show_shapes=True)
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(train_taxa, y_train, epochs=100, batch_size=batch_size,class_weight={0:0.1, 1:48})
test_taxa=pd.read_csv(fname,sep="\t")
y_test=to_categorical(test_taxa['events'].values)
y_original=test_taxa['events'].values.astype('int32')
y_original
y_test
test_taxa=test_taxa.drop(columns=['events','X.OTU.ID'])
test_taxa=test_taxa.to_numpy()
test_taxa.shape
loss, acc = model.evaluate(test_taxa, y_test, batch_size=batch_size)
loss
acc
print("\nTest accuracy: %.1f%%" % (100.0 * acc))
y_pred=model.predict_classes(test_taxa).astype('int32')
y_pred
print(confusion_matrix(y_original,y_pred))
random_state=20
#perm = PermutationImportance(model, random_state=random_state,n_iter=15).fit(test_taxa,y_test)
#weights=permutation_importance(feature_names,model,15,taxa_train_perm,y_train,random_state)
#weights=pd.DataFrame(list(zip(feature_names,perm.feature_importances_,perm.feature_importances_std_)),columns=["feature","weight","weight_sd"])
#weights=weights.sort_values(by="weight",ascending=False)
#weights.to_csv("multilpcp_order_taxa_nested_weights.tsv",sep="\t",index=False)