-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_model.py
123 lines (101 loc) · 4.56 KB
/
random_model.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
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 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+', '_')
X_train, test_df, y_train, y_test = train_test_split(result, y_true, stratify=y_true, test_size=0.2)
train_df, cv_df, y_train, y_cv = train_test_split(X_train, y_train, stratify=y_train, test_size=0.2)
train_class_distribution = train_df['Class'].value_counts().sort_index()
test_class_distribution = test_df['Class'].value_counts().sort_index()
cv_class_distribution = cv_df['Class'].value_counts().sort_index()
my_colors = 'rgbkymc'
cv_class_distribution.plot(kind='bar')
plt.xlabel('Class')
plt.ylabel('Number of Data Points per Class')
plt.grid()
plt.show()
test_data_len = test_df.shape[0]
cv_data_len = cv_df.shape[0]
cv_predicted_y = np.zeros((cv_data_len, 9))
for i in range(cv_data_len):
rand_probs = np.random.rand(1, 9)
cv_predicted_y[i] = ((rand_probs/sum(sum(rand_probs)))[0])
print("Log loss on Cross Validation data using Random Model", log_loss(y_cv, cv_predicted_y, eps=1e-15))
test_predicted_y = np.zeros((test_data_len, 9))
for i in range(test_data_len):
rand_probs = np.random.rand(1, 9)
test_predicted_y[i] = ((rand_probs/sum(sum(rand_probs)))[0])
print("Log loss on Test data using Random Model", log_loss(y_test, test_predicted_y, eps=1e-15))
predicted_y = np.argmax(test_predicted_y, axis=1)
c = confusion_matrix(y_test, predicted_y)
labels = [1, 2, 3, 4, 5, 6, 7, 8, 9]
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()
b = (c/c.sum(axis=0))
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()
a = (((c.T)/(c.sum(axis=1))).T)
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()