generated from subhalingamd/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
112 lines (85 loc) · 3.37 KB
/
main.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
import sys
from utils import read_train_file, read_test_file, write_predictions
from preprocessing import preprocess
from constants import LENGTH_THRESH, RANDOM_SEED
from sklearn.utils import shuffle
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
# from sklearn.svm import SVC, LinearSVC
# from sklearn.decomposition import TruncatedSVD
# from sklearn.pipeline import FeatureUnion
# from sklearn.metrics import f1_score, accuracy_score
from sklearn.base import BaseEstimator, TransformerMixin
# from sklearn.model_selection import GridSearchCV
# from sklearn.model_selection import PredefinedSplit
import pickle
class ExclamationCount(BaseEstimator, TransformerMixin):
def transform(self, X, **transform_params):
count = [[min(str(x).count("!"), LENGTH_THRESH)] for x in X]
return count
def fit(self, X, y=None, **fit_params):
return self
class QuestionMarkCount(BaseEstimator, TransformerMixin):
def transform(self, X, **transform_params):
count = [[min(str(x).count("?"), LENGTH_THRESH)] for x in X]
return count
def fit(self, X, y=None, **fit_params):
return self
def test(model_dir, in_path, out_path):
X_test = read_test_file(in_path)
try:
model = None
with open(model_dir+"/model.pkl", 'rb') as f:
model = pickle.load(f)
y_test = model.predict(X_test)
y_test = [y*4 for y in y_test]
write_predictions(out_path, y_test)
except Exception as e: # noqa: E722
print("Could not load model. Generating random output.")
print(e)
write_predictions(out_path, [4]*len(X_test))
def train(data_dir, model_dir):
data_path = "training.csv"
if data_dir != "":
data_path = data_dir + '/' + data_path
X_train, y_train = read_train_file(data_path)
X_train, y_train = shuffle(X_train, y_train, random_state=RANDOM_SEED)
model = Pipeline(steps=[
# ("features", FeatureUnion([
("tfidf", TfidfVectorizer(
preprocessor=preprocess,
ngram_range=(1, 2),
max_df=0.75,
min_df=5,
)),
# # ("excl", ExclamationCount()),
# # ("qmark", QuestionMarkCount()),
# ])),
# ("classifier", LinearSVC(max_iter=10000, dual=False, random_state=RANDOM_SEED))
("classifier", LogisticRegression(
solver="saga",
penalty="l2",
C=1.5,
max_iter=10000,
random_state=RANDOM_SEED,
n_jobs=-1
))
], verbose=3)
model = model.fit(X_train, y_train)
with open(model_dir+"/model.pkl", 'wb') as f:
pickle.dump(model, f)
if __name__ == '__main__':
if len(sys.argv) < 2: raise ValueError("Missing mode (arg 1). Expecting [train|test]")
mode = sys.argv[1]
if mode == "train":
data_dir = sys.argv[2]
model_dir = sys.argv[3]
train(data_dir=data_dir, model_dir=model_dir)
elif mode == "test":
model_dir = sys.argv[2]
in_path = sys.argv[3]
out_path = sys.argv[4]
test(model_dir=model_dir, in_path=in_path, out_path=out_path)
else:
raise ValueError("Invalid mode (arg 1): '%s'. Expecting [train|test]" % mode)