-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
30 lines (24 loc) · 759 Bytes
/
model.py
File metadata and controls
30 lines (24 loc) · 759 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
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
# load dataset
def load_data():
try:
df = pd.read_csv("data/sentiment_data.csv")
except:
Exception("Error loading dataset")
exit(1)
return df["text"], df["label"]
# train model
def train_model():
x, y = load_data()
model = Pipeline([
("tfidf", TfidfVectorizer(ngram_range=(1,2))), # converts text into numerical form
("classifier", MultinomialNB()) # train model using a simple algorithm for text classification
])
# train model
model.fit(x, y)
return model
# analyze trained model
sentiment_model = train_model()