-
Notifications
You must be signed in to change notification settings - Fork 0
/
TfIdfVectorizer and SVC.py
36 lines (32 loc) · 1.17 KB
/
TfIdfVectorizer and SVC.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
import numpy as np
import nltk
from nltk.corpus import stopwords
%matplotlib inline
import string
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report
import pickle
from sklearn.svm import SVC
from sklearn.feature_extraction.text import TfidfVectorizer
def text_process(text):
'''
Takes in a string of text, then performs the following:
1. Remove all punctuation
2. Remove all stopwords
3. Return the cleaned text as a list of words
'''
nopunc = [char for char in text if char not in string.punctuation]
nopunc = ''.join(nopunc)
return [word for word in nopunc.split() if word.lower() not in stopwords.words('english')]
yelp = pd.read_csv('yelp.csv')
yelp_class = yelp[(yelp['stars'] == 1) | (yelp['stars'] == 5)]
X = yelp_class['text']
y = yelp_class['stars']
Tf_transfromer = TfidfVectorizer(analyzer=text_process).fit(X)
X = Tf_transfromer.transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)
nb = SVC(kernel='linear')
nb.fit(X_train, y_train)
preds = nb.predict(X_test)
print(classification_report(y_test, preds))