-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSENTIMENT ANALYSIS
61 lines (42 loc) · 1.93 KB
/
SENTIMENT ANALYSIS
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
# THE RAW PYTHON CODE
from google.colab import drive
drive.mount('/content/drive')
import chardet
import pandas as pd
# Read the training and test datasets
df_train = pd.read_csv("/content/drive/MyDrive/Colab Notebooks/train.csv", encoding=chardet.detect(open("/content/drive/MyDrive/Colab Notebooks/train.csv", "rb").read())["encoding"])
df_test = pd.read_csv("/content/drive/MyDrive/Colab Notebooks/test.csv", encoding=chardet.detect(open("/content/drive/MyDrive/Colab Notebooks/test.csv", "rb").read())["encoding"])
# Drop rows with missing values in the "text" or "sentiment" columns
df_train = df_train[["text", "sentiment"]].dropna()
df_test = df_test[["text", "sentiment"]].dropna()
# Split the training dataset into training and validation sets
train_text,train_labels=df_train["text"],df_train["sentiment"]
# ...
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import SVC
from sklearn.metrics import classification_report
# Create a TfidfVectorizer object
vectorizer = TfidfVectorizer()
svm = SVC(kernel="linear")
# Fit the vectorizer on the training data
vectorizer.fit(train_text)
# Transform the training and validation data using the fitted vectorizer
train_features = vectorizer.transform(train_text)
# Train an SVM model
svm.fit(train_features, train_labels)
# Transform the test data using the fitted vectorizer
test_features = vectorizer.transform(df_test["text"])
# Make predictions on the test data
test_predictions = svm.predict(test_features)
# Print the classification report for the test data
print(classification_report(df_test["sentiment"], test_predictions))4
!pip install joblib
import joblib
joblib.dump(svm,"sentiment.joblib")
svm_model=joblib.load("sentiment.joblib")
# Predict sentiment on new data
new_text = ["i am not happy"]
new_features = vectorizer.transform(new_text)
new_predictions = svm_model.predict(new_features)
print(new_predictions)