-
Notifications
You must be signed in to change notification settings - Fork 5
/
savejob.py
45 lines (44 loc) · 1.22 KB
/
savejob.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
from sklearn.externals import joblib
from sklearn.pipeline import Pipeline
from sklearn.svm import LinearSVC
from sklearn.multiclass import OneVsRestClassifier
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.preprocessing import MultiLabelBinarizer
tags={}
freq=[]
count=0
fh=open('Tags.txt','r')
fh2=open('cleaned.txt','r')
tagrows=fh.read().split('\n')[:500000]
X=fh2.read().split('\n')[:500000]
Y = [[] for i in range(len(X))]
classifier = Pipeline([
('vectorizer', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', OneVsRestClassifier(LinearSVC(), n_jobs = -2))])
for line in tagrows:
for tag in line.split():
if tag in tags:
tags[tag]+=1
else:
tags[tag]=1
count=0
for tag in sorted(tags,key=lambda tag:tags[tag], reverse=True):
if tags[tag] > 4000:
count += 1
freq.append(tag)
else:
break
print "Training..."
for x,tag in enumerate(freq):
i=0
for row in tagrows:
if tag in row.split():
Y[i].append(tag)
i=i+1
multibin=MultiLabelBinarizer()
Y=multibin.fit_transform(Y)
classifier.fit(X,Y)
job = joblib.dump(classifier, 'clf.txt', compress=9)
job = joblib.dump(multibin, 'multibin.txt', compress=9)