-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNLP_svm.py
179 lines (140 loc) · 8.07 KB
/
NLP_svm.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
import pandas as pd
df = pd.read_csv('training_set.csv')
df['combined_grade'] = 0
# clean up the training data:
# remove the questions that have raw_answer_text and cleaned_answer_text as NaN
# as having those values as NaN doesn't make any sense since what is to grade when
# there isn't any stuff to actually grade
df_simplified = df[
['id', 'problem_id', 'cleaned_answer_text', 'grade_0', 'grade_1', 'grade_2', 'grade_3', 'grade_4', 'combined_grade',
'dataset']]
print(df_simplified.loc[(df_simplified['problem_id'] == 1619)])
# df_simplified['combined_grade'].fillna(" ", inplace=True)
df_training = df_simplified.loc[(df_simplified['dataset'] == 'training')]
df_evaluation = df_simplified.loc[(df_simplified['dataset'] == 'evaluation')]
print(df_training.shape)
print(df_evaluation.shape)
df_training = df_training[pd.notnull(df_training['grade_0'])]
df_training.loc[df_training.grade_0 == 1, 'combined_grade'] = 0.0
df_training.loc[df_training.grade_1 == 1, 'combined_grade'] = 0.25
df_training.loc[df_training.grade_2 == 1, 'combined_grade'] = 0.5
df_training.loc[df_training.grade_3 == 1, 'combined_grade'] = 0.75
df_training.loc[df_training.grade_4 == 1, 'combined_grade'] = 1.0
# print(df_training['problem_id'].value_counts())
# print(len(df_training['problem_id'].unique()))
unique_train = df_training['problem_id'].unique()
unique_eval = df_evaluation['problem_id'].unique()
print('\n----------------------\n')
print(df_evaluation['problem_id'].unique().shape)
print(df_evaluation['problem_id'].shape)
print('\n----------------------\n')
# print(' A - B : ', list(set(unique_train)-set(unique_eval)))
# print(' B - A : ', list(set(unique_eval)-set(unique_train)))
def make_prediction():
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import LinearSVC
from sklearn.svm import SVC
from sklearn.metrics import roc_auc_score
from sklearn.calibration import CalibratedClassifierCV
from sklearn.model_selection import train_test_split
import numpy as np
# from sklearn import metrics
count = 0
rmse_array = 0
auc_array = 0
answers = pd.DataFrame({0.0: [], 0.25: [], 0.5: [], 0.75: [], 1.0: [], 'id': []})
col_list = (answers).columns.tolist()
special_cases = [496, 1200, 1196, 1619, 1113, 493, 494, 1219, 1316, 1106, 1092, 1332, 1112, 1120, 1169, 1172, 1173, 1182, 1188, 1217,
1222, 1228, 1221, 1224]
# 83 has all ones only one 0
# 496, 494, 1092, 1120, 1217, 1228 only has stop words
# 1619 empty
# 493, 494, 1219, 1316, 1106, 1332, 1112, 1169, 1172, 1173, 1182, 1188, 1222, 1228, 1221, 1224 no terms remain after pruning
for problem_id in unique_eval:
# train = df_train_clean.loc[(df_train_clean['problem_id'] == problem_id)]['raw_answer_text']
x_train = df_training.loc[(df_training['problem_id'] == problem_id)]['cleaned_answer_text']
y_train = df_training.loc[(df_training['problem_id'] == problem_id)]['combined_grade']
x_test = df_evaluation.loc[(df_evaluation['problem_id'] == problem_id)]['cleaned_answer_text']
x_test_id = df_evaluation.loc[(df_evaluation['problem_id'] == problem_id)][['id']]
print('for problem id : ', problem_id)
# print(x_train.value_counts())
# print(x_train.head())
# print('most frequent occurance : ', x_train.value_counts().argmax())
print('\n------------------------------------\n')
# print(answers.shape)
# print(x_test.shape)
# print('\n------------------------------------\n')
list_of_y = y_train.unique()
if (problem_id == 493):
test_sadfasdf = (min(y_train.value_counts().unique()) > 1)
print('break point here')
# the third condition 'th_flag' was put in because certain sets had only one graded as 0 and all
# graded as 1 which meant it couldn't be trained properly
th_flag = False
if (len(y_train) > 0) :
th_flag = (min(y_train.value_counts().unique()) > 1)
if (len(list_of_y) > 1) and (not problem_id in special_cases) and th_flag:
# tf-idf feature matrix
# TODO: this logic is wrong empty answers should be given a 0 where as argmax() gives the most frequent answer which might be scored as 1
# Solution: track their problem ids and make adjustments to the relevant locations?
# if not x_train.empty and not x_train.isnull().all():
# x_train.fillna(x_train.value_counts().argmax(), inplace=True)
#
# # problem_id = 32 has NaN in the question as well -_-
# if not x_test.empty and not x_test.isnull().all():
# x_test.fillna(x_test.value_counts().argmax(), inplace=True)
x_train_train, x_train_test, y_train_train, y_train_test = train_test_split(x_train, y_train, test_size=0.3,
random_state=42, stratify=y_train)
tfidf_vectorizer = TfidfVectorizer(max_df=0.9, min_df=2, max_features=1000,
stop_words='english') # max_df=0.90, min_df=2, max_features=1000,
tfidf_vectorizer.fit(pd.concat([x_train, x_test]).values.astype('U'))
tfidf_x_train_train = tfidf_vectorizer.transform(x_train_train.values.astype('U'))
tfidf_x_train_test = tfidf_vectorizer.transform(x_train_test.values.astype('U'))# unicode
tfidf_x_test = tfidf_vectorizer.transform(x_test.values.astype('U'))
labels = LabelEncoder()
labels_y_train_bow = labels.fit(y_train)
# labels_y_test_bow = labels.fit(y_test)
labels_y_train_tfidf = labels.transform(y_train_train)
labels_y_test_tfidf = labels.transform(y_train_test)
svc_classifier = SVC(probability=True, kernel='rbf', C=10, gamma=10)
# C : regularization
svc_classifier.fit(tfidf_x_train_train, labels_y_train_tfidf)
prediction = svc_classifier.predict_proba(tfidf_x_train_test)
if( len(np.unique(labels_y_test_tfidf)) > 1 and len(labels.classes_) < 3) :
print('\n ROC-AUC yields : ' + str(roc_auc_score(labels_y_test_tfidf, prediction[:, 1])))
else:
print('\n ROC-AUC yields : 1; all the labels were the same thing')
predicted_df = pd.DataFrame(svc_classifier.predict_proba(tfidf_x_test) * 100, columns=labels.classes_)
# predicted_df['id'] = x_test_id['id'].astype(float)
x_test_id.reset_index(drop=True, inplace=True)
predicted_df.reset_index(drop=True, inplace=True)
predicted_df2 = pd.concat([predicted_df, x_test_id], axis=1)
predicted_df2 = predicted_df2.loc[:, col_list].fillna(0)
answers = pd.concat([answers, predicted_df2])
else:
# print('all the labels were the same')
list_input = [100, 0, 0, 0, 0, problem_id]
df_input = []
if len(list_of_y) == 1:
max_occurance = list_of_y[0]
elif(len(y_train)>0):
max_occurance = y_train.value_counts().argmax()
else:
max_occurance = 0.00
if not (max_occurance == 0.00 or max_occurance == 0.25):
list_input = [0, 0, 0, 0, 100, problem_id]
for i in x_test:
df_input.append(list_input)
if len(df_input) > 0:
predicted_df = pd.DataFrame(df_input, columns=[0.0, 0.25, 0.5, 0.75, 1.0, 'problem_id'])
x_test_id.reset_index(drop=True, inplace=True)
predicted_df.reset_index(drop=True, inplace=True)
predicted_df2 = pd.concat([predicted_df, x_test_id], axis=1)
answers = pd.concat([answers, predicted_df2])
print('\n----------------------------------------\n')
print(x_test.value_counts())
print('\n----------------------------------------\n')
answers.to_csv('result.csv')
make_prediction()