-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsupport.py
41 lines (26 loc) · 859 Bytes
/
support.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
import joblib
import pandas as pd
import scipy
import sys
_CLASSES = [
'negative',
'neutral',
'positive'
]
def load_model(path='models/support.pkl'):
return joblib.load(path)
def preprocess(data):
data['text'].fillna('', inplace=True)
return data
def predict(input_csv_path, output_csv_path):
model, char_vectorizer, word_vectorizer = load_model()
df_test = pd.read_csv(input_csv_path, index_col='id')
df_test = preprocess(df_test)
X_chars = char_vectorizer.transform(df_test['text'])
X_words = word_vectorizer.transform(df_test['text'])
X = scipy.sparse.hstack([X_chars, X_words])
predictions = model.predict(X)
df_test['label'] = [_CLASSES[class_index] for class_index in predictions]
df_test.to_csv(output_csv_path)
if __name__ == '__main__':
predict(sys.argv[1], sys.argv[2])