-
Notifications
You must be signed in to change notification settings - Fork 0
/
TF-IDF and Classifier.py
36 lines (30 loc) · 1.21 KB
/
TF-IDF and Classifier.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
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
import pickle
from sklearn.feature_extraction.text import TfidfVectorizer
df = pd.read_csv('traindata/trained_data.csv')
# print(df['text'])
vectorizer = TfidfVectorizer(analyzer='word',
ngram_range=(1, 1),
min_df=1,
max_df=0.95,
sublinear_tf=True,
use_idf=True)
train_vectors_tfidf = vectorizer.fit_transform(df['text'])
save_vec = open("tfidf.p", "wb")
pickle.dump(train_vectors_tfidf, save_vec, pickle.HIGHEST_PROTOCOL)
save_vec.close()
neigh = KNeighborsClassifier(n_neighbors=6, metric="cosine")
neigh.fit(train_vectors_tfidf, df['sentiment'])
save_clf2 = open("6nnsent.p", "wb")
pickle.dump(neigh, save_clf2, pickle.HIGHEST_PROTOCOL)
save_clf2.close()
def svmclassifier(train_vectors_tfidf, train_label):
clf = SVC(kernel="linear")
linear_clf = clf.fit(train_vectors_tfidf, train_label)
save_clf = open("svmsent.p", "wb")
pickle.dump(clf, save_clf, pickle.HIGHEST_PROTOCOL)
save_clf.close()
return linear_clf
svmclassifier(train_vectors_tfidf, df['sentiment'])