<jupyter_start><jupyter_text>FEATURE EXTRACTION<jupyter_code>import pandas as pd
from textblob import TextBlob
d = pd.read_csv("processed_tweets.csv")
d = d.drop(["Unnamed: 0","index"],axis=1)
d = d.rename(columns={"0":"Text"})
d.head(10)
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)
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer

cv = CountVectorizer()
tf = TfidfVectorizer()
x1 = cv.fit_transform(df["Text"])
x2 = tf.fit_transform(df["Text"])
y = df["Sentiment"]
y=y.astype('int')<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_train,x_test, y_train, y_test = train_test_split(x1,y,test_size=0.2,random_state=84)
X_train, X_test, Y_train, Y_test = train_test_split(x2,y,test_size=0.2,random_state=84)
from sklearn import metrics

accuracy = {}
model = {}
vectorizer = {"CountVectorizer":cv,"TfidfVectorizer":tf}<jupyter_output><empty_output><jupyter_text>Support Vector Machine  Count Vectorizer<jupyter_code>from sklearn import svm

classifier=svm.SVC()
classifier.fit(x_train,y_train)
y_predict1=classifier.predict(x_test)
x = "Support Vector Machine with CountVectorizer"
model[x]=classifier
b1=metrics.accuracy_score(y_test, y_predict1)
recall = metrics.recall_score(y_test, y_predict1)
accuracy[x] = float("{0:.4f}".format(b1))
print(x)
print("Accuracy: ",accuracy[x])
print("Recall  : {0:.4f}".format(recall))<jupyter_output>Support Vector Machine with CountVectorizer
Accuracy:  0.8679
Recall  : 0.7814<jupyter_text>TfidfVectorizer<jupyter_code>classifier=svm.SVC()
classifier.fit(X_train,Y_train)
Y_predict1=classifier.predict(X_test)
x = "Support Vector Machine with TfidfVectorizer"
model[x]=classifier
b2=metrics.accuracy_score(Y_test, Y_predict1)
recall = metrics.recall_score(y_test, y_predict1)
accuracy[x] = float("{0:.4f}".format(b2))
print(x)
print("Accuracy: ",accuracy[x])
print("Recall  : {0:.4f}".format(recall))<jupyter_output>Support Vector Machine with TfidfVectorizer
Accuracy:  0.8786
Recall  : 0.7814<jupyter_text>Multinomial Naive Bayes  Count Vectorizer<jupyter_code>from sklearn.naive_bayes import MultinomialNB

nb = MultinomialNB()
nb.fit(x_train,y_train)
y_predict2=classifier.predict(x_test)
x = "Multinomial Naive Bayes with CountVectorizer"
model[x]=nb
b3=metrics.accuracy_score(y_test, y_predict2)
recall = metrics.recall_score(y_test, y_predict1)
accuracy[x] = float("{0:.4f}".format(b3))
print(x)
print("Accuracy: ",accuracy[x])
print("Recall  : {0:.4f}".format(recall))<jupyter_output>Multinomial Naive Bayes with CountVectorizer
Accuracy:  0.5499
Recall  : 0.7814<jupyter_text>TfidfVectorizer<jupyter_code>nb = MultinomialNB()
nb.fit(X_train,Y_train)
Y_predict2=classifier.predict(X_test)
x = "Multinomial Naive Bayes with TfidfVectorizer"
model[x]=nb
b4=metrics.accuracy_score(Y_test,Y_predict2)
recall = metrics.recall_score(y_test, y_predict1)
accuracy[x] = float("{0:.4f}".format(b4))
print(x)
print("Accuracy: ",accuracy[x])
print("Recall  : {0:.4f}".format(recall))
ad={}
for i in accuracy:
    ad[i] = {"Accuracy":accuracy[i]}
ad = pd.DataFrame(ad)
ad<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15679824561403508, 'fraction_numerical': 0.025219298245614034, 'mean_word_length': 8.1225, 'pattern_counts': {'":': 4, '<': 22, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '643715', 'license': 'MIT', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1481, 'n_words': 233, 'path': 'NB and  SVM.ipynb', 'repo_name': 'SubethaSiva/Sentiment-Analysis-for-Depression-Detection', 'size': '3648'}