# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
from sklearn.datasets import fetch_20newsgroups
from tqdm import tqdm
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import confusion_matrix
import itertools
import re
# %matplotlib inline

# # Multiple Linear logistic Regression code
# ##### Student name: <NAME>
# ##### Student ID: 2109853M-IM20-0015
# ##### This code includes multiple linear logistic regression code. (Question5)

newsgroups_train = fetch_20newsgroups(subset='train')
train_data = newsgroups_train.data  # getting all trainign examples
train_labels = newsgroups_train.target

len(train_data)

len(train_labels)

newsgroups_test = fetch_20newsgroups(subset='test')  # loading test data
test_data = newsgroups_test.data  # get test set examples
test_labels = newsgroups_test.target

len(test_data)

len(test_labels)

tfidf = TfidfVectorizer(max_df=0.97, min_df=2,
                                max_features=None,
                                stop_words='english')
fit_train = tfidf.fit_transform(train_data)
train_tf = tfidf.transform(train_data)
test_tf = tfidf.transform(test_data)

train_tf.shape

test_tf.shape

logistic = LogisticRegression(solver = "saga", multi_class="multinomial", penalty='l1', max_iter=1000, random_state=1)

logistic.fit(train_tf, train_labels)

predict = logistic.predict(test_tf)

accuracy = np.sum(predict == test_labels) / test_labels.shape[0]

# ## Test result(accuracy)

accuracy


# ## Confusion matrix

def plot_confusion_matrix(cm, classes,
                          title='Confusion matrix',
                          cmap=plt.cm.PuBu):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
    print(cm)
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=90)
    plt.yticks(tick_marks, classes)
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], '.2f'),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.ylabel('Ground Truth')
    plt.xlabel('Prediction')
    plt.tight_layout()


# +
# Compute confusion matrix
cma = confusion_matrix(test_labels, predict, labels=None, sample_weight=None)
# cnf_matrix = confusion_matrix(test_label, predict)
np.set_printoptions(precision=2)

# Plot non-normalized confusion matrix
plt.figure(figsize=(12, 10), facecolor='w', edgecolor='b')
plot_confusion_matrix(cma, classes=newsgroups_test.target_names,
                      title='Confusion Matrix on multiclass logistic regression classifie')
plt.savefig('LG.png')

StarcoderdataJupyterScriptsDedupFiltered