-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_idf_models.py
74 lines (55 loc) · 2.63 KB
/
tf_idf_models.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
from sklearn.feature_extraction.text import TfidfTransformer
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
import numpy as np
import pickle
train = pd.read_csv(r'/Users/umeshkethepalli/Desktop/Hate Speech/HateSpeech-5/data_files/tfidf_vectored_train.csv')
test = pd.read_csv(r'/Users/umeshkethepalli/Desktop/Hate Speech/HateSpeech-5/data_files/tfidf_vectored_test.csv')
train.dropna(inplace=True)
test.dropna(inplace=True)
X_train = train.drop(columns = ['class','Hate','Offensive','Neither'])
y_train = train['class']
X_test = test.drop(columns = ['class','Hate','Offensive','Neither'])
y_test = test['class']
tfidf = TfidfTransformer()
x_train_tfidf = tfidf.fit_transform(X_train)
x_train_tfidf.toarray()
x_test_tfidf = tfidf.transform(X_test)
train.dropna(inplace=True)
test.dropna(inplace=True)
X_train = train.drop(columns = ['class','Hate','Offensive','Neither'])
y_train = train['class']
X_test = test.drop(columns = ['class','Hate','Offensive','Neither'])
y_test = test['class']
# Model building for each number of neighbors
for i in range(3,20):
neigh = KNeighborsClassifier(n_neighbors=i)
neigh.fit(X_train, y_train)
y_pred = neigh.predict(X_test)
C = confusion_matrix(y_test,y_pred)
print(i,np.trace(np.matrix(C)))
pickle.dump(neigh,open(r'/Users/umeshkethepalli/Desktop/Hate Speech/HateSpeech-5/models/tfidfmodels/Neigh_{}.sav'.format(i),'wb'))
from sklearn import svm
clf = svm.SVC()
clf.fit(X_train, y_train)
y_Pred_svm = clf.predict(X_test)
C_svm = confusion_matrix(y_test,y_Pred_svm)
print(np.trace(np.matrix(C_svm)))
pickle.dump(clf,open(r'/Users/umeshkethepalli/Desktop/Hate Speech/HateSpeech-5/models/tfidfmodels/SVM_SVC.sav','wb'))
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
MNB= MultinomialNB().fit(X_train, train['class'])
MNB_preds = MNB.predict(X_test)
print(confusion_matrix(test['class'],MNB_preds))
print (classification_report(test['class'], MNB_preds))
pickle.dump(MNB, open(r'/Users/umeshkethepalli/Desktop/Hate Speech/HateSpeech-5/models/tfidfmodels/MNB_counts.sav','wb'))
from sklearn.ensemble import RandomForestClassifier
for N in range(100,1000,100):
rfc = RandomForestClassifier(n_estimators=N,min_samples_split=5,random_state=42,class_weight="balanced")
rfc.fit(X_train, y_train)
rfc_preds = rfc.predict(X_test)
C_rfc = confusion_matrix(y_test,rfc_preds)
print(N,np.trace(np.matrix(C_rfc)))
pickle.dump(rfc, open(r'/Users/umeshkethepalli/Desktop/Hate Speech/HateSpeech-5/models/tfidfmodels/RFC_{}.sav'.format(N),'wb'))