-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmodel-1.py
49 lines (35 loc) · 1.47 KB
/
model-1.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
# This model is only by title
import pandas as pd
import regex as re
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import SGDClassifier
from nltk.stem.porter import *
train_df = pd.read_csv('data/train_v2.csv')
test_df = pd.read_csv('data/test_v2.csv')
print('train_df size: {}, test_df size: {}'.format(train_df.shape, test_df.shape))
stemmer = PorterStemmer()
# treat obamacar as stopword!
manual_stopwords = ['obamacar']
def my_tokenizer(s):
words = re.findall(r'[A-Za-z]+', s)
words = [word.lower() for word in words]
words = [stemmer.stem(word) for word in words]
words = [word for word in words if (len(word) > 1 and word not in manual_stopwords)]
return words
X = train_df['title']
y = train_df['category']
X_test = test_df['title']
text_clf_svm = Pipeline([('vect', CountVectorizer(ngram_range=(1, 2), tokenizer=my_tokenizer, stop_words='english')),
('tfidf', TfidfTransformer(use_idf=True)),
('clf-svm', SGDClassifier(alpha=0.001, loss='hinge', penalty='l2', warm_start=True, n_iter=6, random_state=42))])
text_clf_svm = text_clf_svm.fit(X.values.astype('U'), y.values)
print('fit done')
result = text_clf_svm.predict(X_test)
write_file = open('result-model-1.txt', 'w+')
write_file.write('article_id,category\n')
i = 1
for item in result:
write_file.write('{},{}\n'.format(i, item))
i += 1
write_file.close()