-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagnosis.py
261 lines (177 loc) · 6.99 KB
/
diagnosis.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
import pandas as pd
import numpy as np
from pprint import pprint
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.ensemble import VotingClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score
from time import time
from scipy.stats import randint as sp_randint
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.layers import Input, Conv2D, Lambda, merge, Flatten,MaxPooling2D
from keras.models import Model
from keras.regularizers import l2
from keras import backend as K
import numpy.random as rng
#splitter
class TrainTestSplitter():
def __init__(self, column, ration):
self.trainTestRatio = ration
self.targetColumn = column
def execute(self, df):
y = df.pop(self.targetColumn)
X = df
X_tr,X_test,y_train,y_test = train_test_split(X.index,y,test_size=self.trainTestRatio)
df_train = X.loc[X_tr]
df_test = X.loc[X_test]
return df_train,df_test,y_train,y_test
class ColumnsEncoder():
def __init__(self):
self.columns = []
def execute(self, df, columns):
encoded = self.transform(df, columns)
return encoded
def transform(self,X,columns):
output = X.copy()
if columns is not None:
for col in columns:
output[col] = LabelEncoder().fit_transform(output[col])
else:
for colname,col in output.iteritems():
output[colname] = LabelEncoder().fit_transform(col)
return output
def fit_transform(self,X,y=None):
return self.fit(X,y).transform(X)
class ColumnsRemover():
def __init__(self):
self.columns = []
def execute(self, df, columns):
for c in columns:
df.drop(c, axis=1, inplace=True)
return df
class ColumnsFilter():
def __init__(self):
self.columns = []
def execute(self, df, columns):
for c in columns:
df = df[df[c].notnull()]
return df
class TfIdfProcessor():
def __init__(self):
self.columns = []
def tokenize_and_stem(self,text):
#tokens = [word for sent in nltk.sent_tokenize(text) for word in nltk.word_tokenize(sent)]
#filtered_tokens = []
#for token in tokens:
# if re.search('[a-zA-Z]', token):
# filtered_tokens.append(token)
#stems = [wordnet_lemmatizer.lemmatize(t) for t in filtered_tokens]
#stems = [stemmer.stem(t) for t in filtered_tokens]
players = text.split('|')
return [x.lower() for x in players]
def tokenize_only(text):
# first tokenize by sentence, then by word to ensure that punctuation is caught as it's own token
tokens = [word.lower() for sent in nltk.sent_tokenize(text) for word in nltk.word_tokenize(sent)]
filtered_tokens = []
# filter out any tokens not containing letters (e.g., numeric tokens, raw punctuation)
for token in tokens:
if re.search('[a-zA-Z]', token):
filtered_tokens.append(token)
return filtered_tokens
def getTfIdfMatrixForDF(self, df,columns):
local_df = df
tfidf_vectorizer = CountVectorizer(tokenizer=self.tokenize_and_stem, binary=True)
for c in columns:
#print(c)
valuesOfDF = local_df.pop(c).values
#print(valuesOfDF)
X = tfidf_vectorizer.fit_transform(valuesOfDF.astype('U')).toarray()
for i, col in enumerate(tfidf_vectorizer.get_feature_names()):
local_df[col] = X[:, i]
return local_df
def execute(self, df, columns):
transformed = self.getTfIdfMatrixForDF(df,columns)
return transformed
def W_init(shape,name=None):
"""Initialize weights as in paper"""
values = rng.normal(loc=0,scale=1e-2,size=shape)
return K.variable(values,name=name)
#//TODO: figure out how to initialize layer biases in keras.
def b_init(shape,name=None):
"""Initialize bias as in paper"""
values=rng.normal(loc=0.5,scale=1e-2,size=shape)
return K.variable(values,name=name)
df = pd.read_csv('data.csv', sep=',')
columns_to_remove = ['id']
remover = ColumnsRemover()
df = remover.execute(df, columns_to_remove)
columns_to_filter_none = []
filt = ColumnsFilter()
df = filt.execute(df, columns_to_filter_none)
player_columns = []
tf = TfIdfProcessor()
df = tf.execute(df, player_columns)
#df.to_csv('test_player_matrix_1.csv')
columns_to_encode = ['diagnosis']
enc = ColumnsEncoder()
df = enc.execute(df, columns_to_encode)
print(df.head(5))
print(df.shape)
target_column = 'diagnosis'
train_test_ration = 0.2
train_test = TrainTestSplitter(target_column, train_test_ration)
print("Getting splits...")
X_train,X_test,y_train,y_test = train_test.execute(df)
x_train = X_train.as_matrix(columns=None)
x_test = X_test.as_matrix(columns=None)
y_train = y_train.as_matrix(columns=None)
y_test = y_test.as_matrix(columns=None)
print("Starting model...")
model = Sequential()
model.add(Dense(64, input_dim=30, activation='relu',kernel_initializer=W_init,kernel_regularizer=l2(2e-4)))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu',kernel_regularizer=l2(2e-4),kernel_initializer=W_init,bias_initializer=b_init))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu',kernel_regularizer=l2(2e-4),kernel_initializer=W_init,bias_initializer=b_init))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu',kernel_regularizer=l2(2e-4),kernel_initializer=W_init,bias_initializer=b_init))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid',kernel_regularizer=l2(2e-4),kernel_initializer=W_init,bias_initializer=b_init))
#rmsprop
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
start = time()
model.fit(x_train, y_train,
epochs=200,
batch_size=64)
score = model.evaluate(x_test, y_test, batch_size=64)
print(model.metrics_names)
print("score: ", score)
print("Took seconds: ", str(time() - start))
np.savetxt("original.txt", y_test, newline=" ")
predictions = model.predict(x_test)
np.savetxt("predicted.txt", predictions, newline=" ")
rounded = [int(round(x[0])) for x in predictions]
np.savetxt("predicted_1.txt", rounded, newline=" ")
score = accuracy_score(y_test,rounded)*100
print(score, "%")