-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearn.py
More file actions
40 lines (34 loc) · 1.42 KB
/
Copy pathlearn.py
File metadata and controls
40 lines (34 loc) · 1.42 KB
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
import os
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.linear_model import SGDClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.svm import SVC
from sklearn.pipeline import Pipeline
import numpy as np
from sklearn.model_selection import train_test_split
import cPickle
from sklearn.model_selection import GridSearchCV
X = []
Y = []
#Import articles
for f in os.listdir("articles/"):
if f[0] != "." and f!="rename.sh":
f = "articles/"+f
article = cPickle.load(open(f,"rb"))
X.append(article.article_text)
Y.append(article.link_flair_text)
X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size=0.2, random_state=42)
print "Train Size:%d\nTest Size:%d" % (len(Y_train),len(Y_test))
params = {'C': [0.1, 1, 10, 100, 1000], 'gamma': [0.1, 0.01, 0.001, 0.0001], 'kernel': ['rbf','linear']}
text_clf = Pipeline([('vect', CountVectorizer()),
('tfidf', TfidfTransformer()),
#('clf', GridSearchCV(SVC(),params)),])
('clf', GradientBoostingClassifier()),])
text_clf.fit(X_train,Y_train)
print "Models Trained"
predicted = text_clf.predict(X_test)
from sklearn import metrics
print(metrics.classification_report(Y_test, predicted))