forked from pieroit/corso_ml_python_youtube_pollo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb03_save_sentiment.py
More file actions
32 lines (22 loc) · 798 Bytes
/
Copy pathweb03_save_sentiment.py
File metadata and controls
32 lines (22 loc) · 798 Bytes
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
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import BernoulliNB
from sklearn.metrics import accuracy_score
from joblib import dump
df = pd.read_csv('movie_review.csv')
print(df.head())
X = df['text']
y = df['tag']
vect = CountVectorizer()
X = vect.fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.4)
model = BernoulliNB()
model.fit(X_train, y_train)
p_train = model.predict(X_train)
p_test = model.predict(X_test)
acc_train = accuracy_score(y_train, p_train)
acc_test = accuracy_score(y_test, p_test)
print( f'Train acc. {acc_train}, test acc. {acc_test}' )
dump(vect, 'sentiment_vectorizer.joblib')
dump(model, 'sentiment_model.joblib')