<jupyter_start><jupyter_text>KNN<jupyter_code># try K=1 through K=15 and record testing accuracy
k_range = list(range(1, 16))
scores = []
for k in k_range:
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(X_train, Y_train)
    y_pred = knn.predict(X_test)
    scores.append(metrics.accuracy_score(Y_test, y_pred))
plt.plot(k_range, scores)
plt.xlabel('Value of K')
plt.ylabel('Testing Accuracy')
#With k = 2
kn = KNeighborsClassifier(n_neighbors=2)
model_knn = kn.fit(X_train,Y_train)
k_pred = model_knn.predict(X_test)
print(metrics.accuracy_score(Y_test,k_pred))
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(Y_test,k_pred)
print(cm)<jupyter_output>[[1310  522   38]
 [ 259  326   29]
 [ 152  135  157]]<jupyter_text>Logistic Regression<jupyter_code>from sklearn.linear_model import LogisticRegression
X3_train, X3_test, Y3_train, Y3_test = train_test_split(data['tidy_text'],data['sentiment'], test_size = 0.2, random_state=0, shuffle = True, stratify=data['sentiment'])

from sklearn.pipeline import Pipeline
from sklearn.metrics import confusion_matrix,classification_report
#Training and testing the randomforest model
rf = Pipeline([('tfidf', TfidfVectorizer()), ('rf', LogisticRegression())])
rf.fit(X3_train, Y3_train)
y_pred = rf.predict(X3_test)
print(pd.crosstab(Y3_test,y_pred,rownames=['Actual'],colnames=['Predicted']))
print(classification_report(Y3_test, y_pred))
#Logistic Regression using 10 fold - cross validation
lr = LogisticRegression()
scores = cross_val_score(lr, X, Y, cv=10, scoring='accuracy')
print(scores)
s0 = scores.mean()

#Logistic Regression using 5 fold - cross validation

lr = LogisticRegression()
scores = cross_val_score(lr, X, Y, cv=5, scoring='accuracy')
print(scores)
s1 = scores.mean()

#Logistic Regression using 15 fold - cross validation

lr = LogisticRegression()
scores = cross_val_score(lr, X, Y, cv=15, scoring='accuracy')
print(scores)
s2 = scores.mean()

#Logistic Regression using 20 fold - cross validation

lr = LogisticRegression()
scores = cross_val_score(lr, X, Y, cv=20, scoring='accuracy')
print(scores)
s3 = scores.mean()

#Taking the average of 4 cross validated accuracies
print('Average accuracy of Logistic Regression is '+ str((s0 + s1 + s2 + s3)/4))<jupyter_output>C:\Users\dcL\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
  FutureWarning)
C:\Users\dcL\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.py:469: FutureWarning: Default multi_class will be changed to 'auto' in 0.22. Specify the multi_class option to silence this warning.
  "this warning.", FutureWarning)
C:\Users\dcL\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
  FutureWarning)
C:\Users\dcL\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.py:469: FutureWarning: Default multi_class will be changed to 'auto' in 0.22. Specify the multi_class option to silence this warning.
  "this warning.", FutureWarning)
C:\Users\dcL\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.py:432: FutureWarning: Default solver will [...]<jupyter_text>Random Forest<jupyter_code>from sklearn.ensemble import RandomForestClassifier

#1 - RandomForest classification with Pipeline
X2_train, X2_test, Y2_train, Y2_test = train_test_split(data['tidy_text'],data['sentiment'], test_size = 0.2, random_state=0, shuffle = True, stratify=data['sentiment'])


#Training and testing the randomforest model
rf = Pipeline([('tfidf', TfidfVectorizer()), ('rf', RandomForestClassifier())])
rf.fit(X2_train, Y2_train)
y_pred = rf.predict(X2_test)
print(pd.crosstab(Y2_test,y_pred,rownames=['Actual'],colnames=['Predicted']))
print(classification_report(Y2_test, y_pred))
#Random Forest using 5 fold Cross validation with 
rf = RandomForestClassifier()
scores = cross_val_score(rf, X, Y, cv=5, scoring='accuracy')
print(scores)
s0 = scores.mean()

#Random Forest using 10 fold Cross validation with 
rf = RandomForestClassifier()
scores = cross_val_score(rf, X, Y, cv=10, scoring='accuracy')
print(scores)
s1 = scores.mean()

#Random Forest using 15 fold Cross validation with 
rf = RandomForestClassifier()
scores = cross_val_score(rf, X, Y, cv=15, scoring='accuracy')
print(scores)
s2 = scores.mean()

#Random Forest using 15 fold Cross validation with 
rf = RandomForestClassifier()
scores = cross_val_score(rf, X, Y, cv=20, scoring='accuracy')
print(scores)
s3 = scores.mean()

print('Average accuracy of Random Forest is '+ str((s0 + s1 + s2 + s3)/4))<jupyter_output>C:\Users\dcL\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
  "10 in version 0.20 to 100 in 0.22.", FutureWarning)
C:\Users\dcL\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
  "10 in version 0.20 to 100 in 0.22.", FutureWarning)
C:\Users\dcL\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
  "10 in version 0.20 to 100 in 0.22.", FutureWarning)
C:\Users\dcL\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
  "10 in version 0.20 to 100 in 0.22.", FutureWarning)
C:\Users\dcL\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py:245: FutureWarni[...]
{'fraction_non_alphanumeric': 0.12104708362614196, 'fraction_numerical': 0.04321855235418131, 'mean_word_length': 6.0283950617283955, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '324055', 'license': 'MIT', 'n_tokens_mistral': 2175, 'n_tokens_neox': 1980, 'n_words': 508, 'path': 'Twitter Sentiment Analysis.ipynb', 'repo_name': 'AbuKaisar24/Assignment-Task', 'size': '5692'}