-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_forest.py
186 lines (154 loc) · 7.55 KB
/
random_forest.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
import pandas as pd
import matplotlib.pyplot as plt
import re
import time
import warnings
import numpy as np
from nltk.corpus import stopwords
from sklearn.decomposition import TruncatedSVD
from sklearn.preprocessing import normalize
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.manifold import TSNE
import seaborn as sns
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics.classification import accuracy_score, log_loss
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import SGDClassifier
from imblearn.over_sampling import SMOTE
from collections import Counter
from scipy.sparse import hstack
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC
from sklearn.model_selection import StratifiedKFold
from collections import Counter, defaultdict
from sklearn.calibration import CalibratedClassifierCV
from sklearn.naive_bayes import MultinomialNB
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
import math
from sklearn.metrics import normalized_mutual_info_score
from sklearn.ensemble import RandomForestClassifier
warnings.filterwarnings("ignore")
from mlxtend.classifier import StackingClassifier
from sklearn import model_selection
from sklearn.linear_model import LogisticRegression
data_variants = pd.read_csv('training/training_variants')
data_text = pd.read_csv("training/training_text", sep="\|\|", engine="python", names=["ID", "TEXT"], skiprows=1)
stop_words = set(stopwords.words('english'))
def report_log_loss(train_x, train_y, test_x, test_y, clf):
clf.fit(train_x, train_y)
sig_clf = CalibratedClassifierCV(clf, method='sigmoid')
sig_clf.fit(train_x, train_y)
sig_clf_probs = sig_clf.predict_proba(test_x)
return log_loss(test_y, sig_clf_probs, eps=1e-15)
def plot_confusion_matrix(test_y, predict_y):
C = confusion_matrix(test_y, predict_y)
A = (((C.T)/(C.sum(axis=1))).T)
B = (C/C.sum(axis=0))
labels = [1,2,3,4,5,6,7,8,9]
print("-"*20, "Confusion Matrix", "-"*20)
plt.figure(figsize=(20,7))
sns.heatmap(C, annot=True, cmap='YlGnBu', fmt='.3f', xticklabels=labels, yticklabels=labels)
plt.xlabel('Predicted Class')
plt.ylabel('Original Class')
plt.show()
print("-"*20, "Precision Matrix(Column Sum=1)", "-"*20)
plt.figure(figsize=(20,7))
sns.heatmap(B, annot=True, cmap='YlGnBu', fmt='.3f', xticklabels=labels, yticklabels=labels)
plt.xlabel('Predicted Class')
plt.ylabel('Original Class')
plt.show()
print("-"*20, "Recall Matrix(Row Sum=1)", "-"*20)
plt.figure(figsize=(20,7))
sns.heatmap(A, annot=True, cmap='YlGnBu', fmt='.3f', xticklabels=labels, yticklabels=labels)
plt.xlabel('Predicted Class')
plt.ylabel('Original Class')
plt.show()
def get_impfeature_names(indices, text, gene, var, no_features):
gene_count_vec = CountVectorizer()
var_count_vec = CountVectorizer()
text_count_vec = CountVectorizer(min_df = 3)
gene_vec = gene_count_vec.fit(train_df('Gene'))
var_vec = var_count_vec.fit(train_df('Variation'))
text_vec = text_count_vec.fit(train_df('TEXT'))
fea1_len = len(gene_vec.get_feature_names())
fea2_len = len(var_count_vec.get_feature_names())
word_present = 0
for i, v in enumerate(indices):
if (v < fea1_len):
word = gene_vec.get_feature_names()[v]
yes_no = True if word == gene else False
if yes_no:
word_present += 1
print(i, "Gene feature [{}] present in test data point [{}]".format(word, yes_no))
elif(v < fea1_len + fea2_len):
word = var_vec.get_feature_names()[v-(fea1_len)]
yes_no = True if word == var else False
if yes_no:
word_present += 1
print(i, "Variation feature [{}] present in test data point [{}]".format(word, yes_no))
else:
word = text_vec.get_feature_names()[v-(fea1_len + fea2_len)]
yes_no = True if word in text.split() else False
if yes_no:
word_present += 1
print(i, "Text feature [{}] present in test data point [{}]".format(word, yes_no))
print("Out of the top ",no_features, " features ", word_present, "are present in query point")
def data_text_preprocess(total_text, ind, col):
if type(total_text) is not int:
string = ""
total_text = re.sub('[^a-zA-Z0-9\n]', ' ', str(total_text))
total_text = re.sub('\s+', ' ', str(total_text))
total_text = total_text.lower()
for word in total_text.split():
if not word in stop_words:
string += word + " "
data_text[col][ind] = string
for index, row in data_text.iterrows():
if type(row['TEXT']) is str:
data_text_preprocess(row['TEXT'], index, 'TEXT')
result = pd.merge(data_variants, data_text, on='ID', how='left')
result[result.isnull().any(axis=1)]
result.loc[result['TEXT'].isnull(), 'TEXT'] = result['Gene'] + ' ' + result['Variation']
y_true = result['Class'].values
result.Gene = result.Gene.str.replace('\s+', '_')
result.Variation = result.Gene.str.replace('\s+', '_')
gene_vectorizer = CountVectorizer()
result_gene = gene_vectorizer.fit_transform(result['Gene'])
variation_vectorizer = CountVectorizer()
result_variation = variation_vectorizer.fit_transform(result['Variation'])
text_vectorizer = CountVectorizer(min_df=3)
result_text = text_vectorizer.fit_transform(result['TEXT'])
result_text = normalize(result_text, axis=0)
result_gene = hstack((result_gene, result_variation))
result = hstack((result_gene, result_text)).tocsr()
X_train, test_df, y_train, y_test = train_test_split(result, y_true, stratify=y_true, test_size=0.2, random_state=43)
train_df, cv_df, y_train, y_cv = train_test_split(X_train, y_train, stratify=y_train, test_size=0.2, random_state=43)
alpha = [100, 200, 500, 1000, 2000]
max_depth = [5, 10]
cv_log_error_array = []
for i in alpha:
for j in max_depth:
print("for n_estimators = ", i, " and max depth = ", j)
clf = RandomForestClassifier(n_estimators=i, criterion='gini', max_depth=j)
clf.fit(train_df, y_train)
sig_clf = CalibratedClassifierCV(clf, method='sigmoid')
sig_clf.fit(train_df, y_train)
sig_clf_probs=sig_clf.predict_proba(cv_df)
cv_log_error_array.append(log_loss(y_cv, sig_clf_probs, labels=clf.classes_, eps=1e-15))
print("Log Loss : ", log_loss(y_cv, sig_clf_probs))
best_alpha = np.argmin(cv_log_error_array)
print("The best alpha : ", alpha[int(best_alpha/2)])
clf = RandomForestClassifier(n_estimators=alpha[int(best_alpha/2)], criterion='gini', max_depth=10)
clf.fit(train_df, y_train)
sig_clf = CalibratedClassifierCV(clf, method='sigmoid')
sig_clf.fit(train_df, y_train)
sig_clf_probs=sig_clf.predict_proba(train_df)
print("For best alpha, the training log Loss : ", log_loss(y_train, sig_clf_probs))
sig_clf_probs=sig_clf.predict_proba(cv_df)
print("For best alpha, the CV log Loss : ", log_loss(y_cv, sig_clf_probs))
sig_clf_probs=sig_clf.predict_proba(test_df)
print("For best alpha, the testing log Loss : ", log_loss(y_test, sig_clf_probs))
print("Number of misclassified points: ", np.count_nonzero((sig_clf.predict(cv_df)-y_cv))/y_cv.shape[0])