<jupyter_start><jupyter_text>FEATURE EXTRACTION<jupyter_code>import pandas as pd
from textblob import TextBlob

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
d = pd.read_csv("Processed_tweets.csv")
d = d.drop(["Unnamed: 0"],axis=1)
d.head(10)
d.drop_duplicates(inplace=True)
d.isna().sum()
d.dropna(inplace=True)
d.info()
n = len(d)
d["Sentiment"]=[None for i in range(n)]
for i in range(n):
    
    s = TextBlob(d["Text"].iloc[i]).sentiment
    if(s[0]>=0):
        d["Sentiment"].iloc[i] = 0
    else:
        d["Sentiment"].iloc[i] = 1
len(d[d["Sentiment"]==0]), len(d[d["Sentiment"]==1])
no = len(d[d["Sentiment"]==1])
t = d[d["Sentiment"]==0][:no]
s = d[d["Sentiment"]==1]
df = pd.concat([s,t],ignore_index="True")
len(df)<jupyter_output><empty_output><jupyter_text>MODEL DEVELOPMENT AND EVALUATION  Splitting into Train and Test Data<jupyter_code>from sklearn.model_selection import train_test_split

x= df["Text"]
y = df["Sentiment"].astype("int")
x_train,x_test, y_train, y_test = train_test_split(x,y,test_size=0.2,random_state=42)
cv = CountVectorizer(ngram_range=(1,3))
tf = TfidfVectorizer(ngram_range=(1,3))
x1 = cv.fit_transform(x_train)
x2 = tf.fit_transform(x_train)
from sklearn import metrics

accuracy = {}
model = {}
vectorizer = {"CountVectorizer":cv,"TfidfVectorizer":tf}<jupyter_output><empty_output><jupyter_text>Decision Tree Classifier  CountVectorizer<jupyter_code>from sklearn.tree import DecisionTreeClassifier

dtc = DecisionTreeClassifier()
dtc.fit(x1,y_train)
y_pred1 = dtc.predict(cv.transform(x_test))
k = "Decision Tree Classifier with CountVectorizer"
model[k]=dtc
a1 = metrics.accuracy_score(y_test, y_pred1)
recall = metrics.recall_score(y_test, y_pred1)
accuracy[k] = a1
print(k)
print("Accuracy: {0:.4f}".format(a1))
print("Recall  : {0:.4f}".format(recall))<jupyter_output>Decision Tree Classifier with CountVectorizer
Accuracy: 0.9008
Recall  : 0.8942<jupyter_text>TfidfVectorizer<jupyter_code>dtc = DecisionTreeClassifier()
dtc.fit(x2,y_train)
y_pred2 = dtc.predict(tf.transform(x_test))
k = "Decision Tree Classifier with TfidfVectorizer"
model[k]=dtc
a2 = metrics.accuracy_score(y_test, y_pred2)
recall = metrics.recall_score(y_test, y_pred2)
accuracy[k] = a2
print(k)
print("Accuracy: {0:.4f}".format(a2))
print("Recall  : {0:.4f}".format(recall))<jupyter_output>Decision Tree Classifier with TfidfVectorizer
Accuracy: 0.8963
Recall  : 0.8963<jupyter_text>Random Forest Classifier  CountVectorizer<jupyter_code>from sklearn.ensemble import RandomForestClassifier

rfc = RandomForestClassifier()
rfc.fit(x1,y_train)
y_pred3 = rfc.predict(cv.transform(x_test))
k = "Random Forest Classifier with CountVectorizer"
model[k]=rfc
a3 = metrics.accuracy_score(y_test, y_pred3)
recall = metrics.recall_score(y_test, y_pred3)
accuracy[k] = a3
print(k)
print("Accuracy: {0:.4f}".format(a3))
print("Recall  : {0:.4f}".format(recall))<jupyter_output>Random Forest Classifier with CountVectorizer
Accuracy: 0.8579
Recall  : 0.7754<jupyter_text>TfidfVectorizer<jupyter_code>rfc = RandomForestClassifier()
rfc.fit(x2,y_train)
y_pred4 = rfc.predict(tf.transform(x_test))
k = "Random Forest Classifier with TfidfVectorizer"
model[k]=rfc
a4 = metrics.accuracy_score(y_test, y_pred4)
recall = metrics.recall_score(y_test, y_pred4)
accuracy[k] = a4
print(k)
print("Accuracy: {0:.4f}".format(a4))
print("Recall  : {0:.4f}".format(recall))
sorted(accuracy)
ad = pd.DataFrame({"Accuracy":accuracy})
ad<jupyter_output><empty_output>