text_plain_output_1.png
from sklearn.decomposition import TruncatedSVD

from sklearn.feature_extraction.text import TfidfVectorizer

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_curve, auc, confusion_matrix

from sklearn.model_selection import GridSearchCV, cross_val_score, train_test_split

from sklearn.naive_bayes import MultinomialNB

from sklearn.neural_network import MLPClassifier

from sklearn.pipeline import Pipeline

from sklearn.svm import SVC

import numpy as np

import numpy as np

import pandas as pd

datasets_to_combine = [('/kaggle/input/uci-bag-of-words/docword.enron.txt', '/kaggle/input/uci-bag-of-words/vocab.enron.txt'), ('/kaggle/input/uci-bag-of-words/docword.kos.txt', '/kaggle/input/uci-bag-of-words/vocab.kos.txt'), ('/kaggle/input/uci-bag-of-words/docword.nips.txt', '/kaggle/input/uci-bag-of-words/vocab.nips.txt')]
dataset_column_names = ['documentID', 'wordID', 'count']
merged_docwords = []
for dataset_tuple in datasets_to_combine:
    docword = pd.read_csv(dataset_tuple[0], delim_whitespace=True, header=None, skiprows=3, names=dataset_column_names, nrows=100000)
    vocab = pd.read_csv(dataset_tuple[1], delim_whitespace=True, header=None, names=['word'])
    merged = pd.merge(docword, vocab.reset_index(), how='inner', left_on='wordID', right_on='index').drop('index', axis=1)
    merged_docwords.append(merged)

corpus = pd.concat(merged_docwords, axis=0, ignore_index=True)

wdm = pd.pivot_table(corpus, values='count', index='word', columns='documentID', fill_value=0)

svd = TruncatedSVD(n_components=100, random_state=7)
svd.fit(wdm)

wdm_transformed = pd.DataFrame(svd.transform())

wdm_enron = pd.pivot_table(merged_docwords[0], values='count', index='word', columns='documentID', fill_value=0)
svd = TruncatedSVD(n_components=100, random_state=7)
wdm_enron_transformed = svd.fit_transform(wdm_enron)

wdm_kos = pd.pivot_table(merged_docwords[1], values='count', index='word', columns='documentID', fill_value=0)
svd = TruncatedSVD(n_components=100, random_state=7)
wdm_kos_transformed = svd.fit_transform(wdm_kos)

wdm_nips = pd.pivot_table(merged_docwords[2], values='count', index='word', columns='documentID', fill_value=0)
svd = TruncatedSVD(n_components=100, random_state=7)
wdm_nips_transformed = svd.fit_transform(wdm_nips)

dataset = pd.read_csv('/kaggle/input/sentiment-labelled-sentences-data-set/sentiment labelled sentences/amazon_cells_labelled.txt', delimiter='\t', header=None, names=['text', 'sentiment'])
X_train, X_test, y_train, y_test = train_test_split(dataset.text, dataset.sentiment, test_size=0.2, random_state=42)

maxent_pipeline = Pipeline([('tfidf', TfidfVectorizer()), ('classifier', LogisticRegression())])
nb_pipeline = Pipeline([('tfidf', TfidfVectorizer()), ('classifier', MultinomialNB())])
svm_pipeline = Pipeline([('tfidf', TfidfVectorizer()), ('classifier', SVC())])
nn_pipeline = Pipeline([('tfidf', TfidfVectorizer()), ('classifier', MLPClassifier())])

maxent_params = {'tfidf__ngram_range': [(1, 1), (1, 2)], 'classifier__C': [0.1, 1, 10]}
maxent_grid = GridSearchCV(maxent_pipeline, maxent_params, cv=10, scoring='accuracy')
maxent_grid.fit(X_train, y_train)
best_maxent_clf = maxent_grid.best_estimator_

nb_params = {'tfidf__ngram_range': [(1, 1), (1, 2)], 'classifier__alpha': [0.1, 1, 10]}
nb_grid = GridSearchCV(nb_pipeline, nb_params, cv=10, scoring='accuracy')
nb_grid.fit(X_train, y_train)
best_nb_clf = nb_grid.best_estimator_

svm_params = {'tfidf__ngram_range': [(1, 1), (1, 2)], 'classifier__C': [0.1, 1, 10]}
svm_grid = GridSearchCV(svm_pipeline, svm_params, cv=10, scoring='accuracy')
svm_grid.fit(X_train, y_train)
best_svm_clf = svm_grid.best_estimator_

nn_params = {'tfidf__ngram_range': [(1, 1), (1, 2)], 'classifier__hidden_layer_sizes': [(100,), (200,), (300,)]}
nn_grid = GridSearchCV(nn_pipeline, nn_params, cv=10, scoring='accuracy')
nn_grid.fit(X_train, y_train)
best_nn_clf = nn_grid.best_estimator_

classifiers = [best_maxent_clf, best_nb_clf, best_svm_clf, best_nn_clf]
classifier_names = ['MaxEnt', 'Naïve Bayes', 'SVM', 'Neural Network']
for clf, name in zip(classifiers, classifier_names):
    scores = cross_val_score(clf, dataset.text, dataset.sentiment, cv=10)
    print(f'{name}: Accuracy = {np.mean(scores):.4f}')
def generate_contingency_table(clf, name):
    y_pred = clf.predict(X_test)
    cm = confusion_matrix(y_test, y_pred)
    cm_avg = cm / cm.sum(axis=1)[:, np.newaxis]
    print(f'\n{name} - Contingency Table (Averaged):')
    print(cm_avg)
for clf, name in zip(classifiers, classifier_names):
    generate_contingency_table(clf, name)