<jupyter_start><jupyter_text>This module runs the classical models on the linguistic features of the dataset<jupyter_code>#make necessary imports
import numpy as np
import pandas as pd
import itertools
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
#Read the data
df=pd.read_csv('../../datasets/liar_tweaked/trainfeaturedata.csv')
testdf=pd.read_csv('../../datasets/liar_tweaked/testfeaturedata.csv')
validdf=pd.read_csv('../../datasets/liar_tweaked/validfeaturedata.csv')
#see the data
df
#Feature Scaling
sc=StandardScaler()
df.loc[:,'SentimentScore':'CountWord']=sc.fit_transform(df.loc[:,'SentimentScore':'CountWord'])
testdf.loc[:,'SentimentScore':'CountWord']=sc.fit_transform(testdf.loc[:,'SentimentScore':'CountWord'])
validdf.loc[:,'SentimentScore':'CountWord']=sc.fit_transform(validdf.loc[:,'SentimentScore':'CountWord'])
df
#initialize ML models
LogicalRegressionclassifier = LogisticRegression(random_state = 0)
SVMclassifier = SVC()
KNNclassifier = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
Randomforestclassifier = RandomForestClassifier(n_estimators = 300, criterion = 'entropy', random_state = 0)
#All Features
LogicalRegressionclassifier.fit(df.loc[:,'SentimentScore':'CountWord'], df['label'])
SVMclassifier.fit(df.loc[:,'SentimentScore':'CountWord'], df['label'])
KNNclassifier.fit(df.loc[:,'SentimentScore':'CountWord'], df['label'])
Randomforestclassifier.fit(df.loc[:,'SentimentScore':'CountWord'], df['label'])

LogicalRegression_y_test_predict = LogicalRegressionclassifier.predict(testdf.loc[:,'SentimentScore':'CountWord'])

LogicalRegression_y_valid_predict = LogicalRegressionclassifier.predict(validdf.loc[:,'SentimentScore':'CountWord'])

SVM_y_test_predict = SVMclassifier.predict(testdf.loc[:,'SentimentScore':'CountWord'])

SVM_y_valid_predict = SVMclassifier.predict(validdf.loc[:,'SentimentScore':'CountWord'])

KNN_y_test_predict = KNNclassifier.predict(testdf.loc[:,'SentimentScore':'CountWord'])

KNN_y_valid_predict = KNNclassifier.predict(validdf.loc[:,'SentimentScore':'CountWord'])

Randomforest_y_test_predict = Randomforestclassifier.predict(testdf.loc[:,'SentimentScore':'CountWord'])

Randomforest_y_valid_predict = Randomforestclassifier.predict(validdf.loc[:,'SentimentScore':'CountWord'])

print('algorithm - test dataset accuracy - valid dataset accuracy')
print('KNN - ' ,round(accuracy_score(testdf['label'], KNN_y_test_predict),4),'- '
      ,round(accuracy_score(validdf['label'], KNN_y_valid_predict),4))
print('SVM - ' ,round(accuracy_score(testdf['label'], SVM_y_test_predict),4), '- '
     ,round(accuracy_score(validdf['label'], SVM_y_valid_predict),4))
print('Random Forest - ', round(accuracy_score(testdf['label'], Randomforest_y_test_predict),4), '- '
     ,round(accuracy_score(validdf['label'], Randomforest_y_valid_predict),4))
print('Logical Regression - ', round(accuracy_score(testdf['label'], LogicalRegression_y_test_predict),4), '- '
     ,round(accuracy_score(validdf['label'], LogicalRegression_y_valid_predict),4))
#Only Sentiment
LogicalRegressionclassifier.fit(df['SentimentScore'].values.reshape(-1,1), df['label'])
SVMclassifier.fit(df['SentimentScore'].values.reshape(-1,1), df['label'])
KNNclassifier.fit(df['SentimentScore'].values.reshape(-1,1), df['label'])
Randomforestclassifier.fit(df['SentimentScore'].values.reshape(-1,1), df['label'])

LogicalRegression_y_test_predict = LogicalRegressionclassifier.predict(testdf['SentimentScore'].values.reshape(-1,1))

LogicalRegression_y_valid_predict = LogicalRegressionclassifier.predict(validdf['SentimentScore'].values.reshape(-1,1))

SVM_y_test_predict = SVMclassifier.predict(testdf['SentimentScore'].values.reshape(-1,1))

SVM_y_valid_predict = SVMclassifier.predict(validdf['SentimentScore'].values.reshape(-1,1))

KNN_y_test_predict = KNNclassifier.predict(testdf['SentimentScore'].values.reshape(-1,1))

KNN_y_valid_predict = KNNclassifier.predict(validdf['SentimentScore'].values.reshape(-1,1))

Randomforest_y_test_predict = Randomforestclassifier.predict(testdf['SentimentScore'].values.reshape(-1,1))

Randomforest_y_valid_predict = Randomforestclassifier.predict(validdf['SentimentScore'].values.reshape(-1,1))

print('algorithm - test dataset accuracy - valid dataset accuracy')
print('KNN - ' ,round(accuracy_score(testdf['label'], KNN_y_test_predict),4), '- '
      ,round(accuracy_score(validdf['label'], KNN_y_valid_predict),4))
print('SVM - ' ,round(accuracy_score(testdf['label'], SVM_y_test_predict),4), '- '
      ,round(accuracy_score(validdf['label'], SVM_y_valid_predict),4))
print('Random Forest - ', round(accuracy_score(testdf['label'], Randomforest_y_test_predict),4), '- '
      ,round(accuracy_score(validdf['label'], Randomforest_y_valid_predict),4))
print('Logical Regression - ', round(accuracy_score(testdf['label'], LogicalRegression_y_test_predict),4), '- '
      ,round(accuracy_score(validdf['label'], LogicalRegression_y_valid_predict),4))
#Only CountPunc
LogicalRegressionclassifier.fit(df['CountPunc'].values.reshape(-1,1), df['label'])
SVMclassifier.fit(df['CountPunc'].values.reshape(-1,1), df['label'])
KNNclassifier.fit(df['CountPunc'].values.reshape(-1,1), df['label'])
Randomforestclassifier.fit(df['CountPunc'].values.reshape(-1,1), df['label'])

LogicalRegression_y_test_predict = LogicalRegressionclassifier.predict(testdf['CountPunc'].values.reshape(-1,1))

LogicalRegression_y_valid_predict = LogicalRegressionclassifier.predict(validdf['CountPunc'].values.reshape(-1,1))

SVM_y_test_predict = SVMclassifier.predict(testdf['CountPunc'].values.reshape(-1,1))

SVM_y_valid_predict = SVMclassifier.predict(validdf['CountPunc'].values.reshape(-1,1))

KNN_y_test_predict = KNNclassifier.predict(testdf['CountPunc'].values.reshape(-1,1))

KNN_y_valid_predict = KNNclassifier.predict(validdf['CountPunc'].values.reshape(-1,1))

Randomforest_y_test_predict = Randomforestclassifier.predict(testdf['CountPunc'].values.reshape(-1,1))

Randomforest_y_valid_predict = Randomforestclassifier.predict(validdf['CountPunc'].values.reshape(-1,1))

print('algorithm - test dataset accuracy - valid dataset accuracy')
print('KNN - ' ,round(accuracy_score(testdf['label'], KNN_y_test_predict),4), '- '
      ,round(accuracy_score(validdf['label'], KNN_y_valid_predict),4))
print('SVM - ' ,round(accuracy_score(testdf['label'], SVM_y_test_predict),4), '- '
      ,round(accuracy_score(validdf['label'], SVM_y_valid_predict),4))
print('Random Forest - ', round(accuracy_score(testdf['label'], Randomforest_y_test_predict),4), '- '
      ,round(accuracy_score(validdf['label'], Randomforest_y_valid_predict),4))
print('Logical Regression - ', round(accuracy_score(testdf['label'], LogicalRegression_y_test_predict),4), '- '
      ,round(accuracy_score(validdf['label'], LogicalRegression_y_valid_predict),4))
#Only Readability
LogicalRegressionclassifier.fit(df['Readability'].values.reshape(-1,1), df['label'])
SVMclassifier.fit(df['Readability'].values.reshape(-1,1), df['label'])
KNNclassifier.fit(df['Readability'].values.reshape(-1,1), df['label'])
Randomforestclassifier.fit(df['Readability'].values.reshape(-1,1), df['label'])

LogicalRegression_y_test_predict = LogicalRegressionclassifier.predict(testdf['Readability'].values.reshape(-1,1))

LogicalRegression_y_valid_predict = LogicalRegressionclassifier.predict(validdf['Readability'].values.reshape(-1,1))

SVM_y_test_predict = SVMclassifier.predict(testdf['Readability'].values.reshape(-1,1))

SVM_y_valid_predict = SVMclassifier.predict(validdf['Readability'].values.reshape(-1,1))

KNN_y_test_predict = KNNclassifier.predict(testdf['Readability'].values.reshape(-1,1))

KNN_y_valid_predict = KNNclassifier.predict(validdf['Readability'].values.reshape(-1,1))

Randomforest_y_test_predict = Randomforestclassifier.predict(testdf['Readability'].values.reshape(-1,1))

Randomforest_y_valid_predict = Randomforestclassifier.predict(validdf['Readability'].values.reshape(-1,1))

print('algorithm - test dataset accuracy - valid dataset accuracy')
print('KNN - ' ,round(accuracy_score(testdf['label'], KNN_y_test_predict),4), '- '
      ,round(accuracy_score(validdf['label'], KNN_y_valid_predict),4))
print('SVM - ' ,round(accuracy_score(testdf['label'], SVM_y_test_predict),4), '- '
      ,round(accuracy_score(validdf['label'], SVM_y_valid_predict),4))
print('Random Forest - ', round(accuracy_score(testdf['label'], Randomforest_y_test_predict),4), '- '
      ,round(accuracy_score(validdf['label'], Randomforest_y_valid_predict),4))
print('Logical Regression - ', round(accuracy_score(testdf['label'], LogicalRegression_y_test_predict),4), '- '
      ,round(accuracy_score(validdf['label'], LogicalRegression_y_valid_predict),4))
#Only CountWord
LogicalRegressionclassifier.fit(df['CountWord'].values.reshape(-1,1), df['label'])
SVMclassifier.fit(df['CountWord'].values.reshape(-1,1), df['label'])
KNNclassifier.fit(df['CountWord'].values.reshape(-1,1), df['label'])
Randomforestclassifier.fit(df['CountWord'].values.reshape(-1,1), df['label'])

LogicalRegression_y_test_predict = LogicalRegressionclassifier.predict(testdf['CountWord'].values.reshape(-1,1))

LogicalRegression_y_valid_predict = LogicalRegressionclassifier.predict(validdf['CountWord'].values.reshape(-1,1))

SVM_y_test_predict = SVMclassifier.predict(testdf['CountWord'].values.reshape(-1,1))

SVM_y_valid_predict = SVMclassifier.predict(validdf['CountWord'].values.reshape(-1,1))

KNN_y_test_predict = KNNclassifier.predict(testdf['CountWord'].values.reshape(-1,1))

KNN_y_valid_predict = KNNclassifier.predict(validdf['CountWord'].values.reshape(-1,1))

Randomforest_y_test_predict = Randomforestclassifier.predict(testdf['CountWord'].values.reshape(-1,1))

Randomforest_y_valid_predict = Randomforestclassifier.predict(validdf['CountWord'].values.reshape(-1,1))

print('algorithm - test dataset accuracy - valid dataset accuracy')
print('KNN - ' ,round(accuracy_score(testdf['label'], KNN_y_test_predict),4), '- '
      ,round(accuracy_score(validdf['label'], KNN_y_valid_predict),4))
print('SVM - ' ,round(accuracy_score(testdf['label'], SVM_y_test_predict),4), '- '
      ,round(accuracy_score(validdf['label'], SVM_y_valid_predict),4))
print('Random Forest - ', round(accuracy_score(testdf['label'], Randomforest_y_test_predict),4), '- '
      ,round(accuracy_score(validdf['label'], Randomforest_y_valid_predict),4))
print('Logical Regression - ', round(accuracy_score(testdf['label'], LogicalRegression_y_test_predict),4), '- '
      ,round(accuracy_score(validdf['label'], LogicalRegression_y_valid_predict),4))<jupyter_output>algorithm - test dataset accuracy - valid dataset accuracy
KNN -  0.4988 -  0.5288
SVM -  0.5635 -  0.5389
Random Forest -  0.5635 -  0.5397
Logical Regression -  0.5635 -  0.5218
{'fraction_non_alphanumeric': 0.15895771346136506, 'fraction_numerical': 0.016499864755206924, 'mean_word_length': 9.624521072796934, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '464300', 'license': 'MIT', 'n_tokens_mistral': 3818, 'n_tokens_neox': 3757, 'n_words': 392, 'path': 'models/feature/.ipynb_checkpoints/feature-checkpoint.ipynb', 'repo_name': 'singh-l/hybrrid_FN_dat_', 'size': '11091'}