# -*- coding: utf-8 -*-
"""
Created on Sun Nov 9 22:47:35 2025

@author: Márton Ispány

Multiclass classification by three categories for 20newsgroups
"""

import matplotlib.pyplot as plt;
from sklearn.datasets import fetch_20newsgroups;
from sklearn.feature_extraction.text import TfidfVectorizer;
from sklearn.linear_model import LogisticRegression;

from sklearn.metrics import confusion_matrix, classification_report, roc_curve, auc, RocCurveDisplay;

from sklearn.naive_bayes import MultinomialNB;
from sklearn.neural_network import MLPClassifier;


import numpy as np;

categories = [
# 'talk.politics.misc', 'talk.religion.misc'];
'comp.sys.ibm.pc.hardware', 'comp.sys.mac.hardware'];
ds_train = fetch_20newsgroups(subset='train',categories=categories); # data_home='C:/scikit_learn_data',
ds_test = fetch_20newsgroups(subset='test',categories=categories);
n_train = len(ds_train.data);
n_test = len(ds_test.data);
        
        
analyze = TfidfVectorizer(stop_words='english',max_df=0.8,min_df=0.01); 
DT_train = analyze.fit_transform(ds_train.data); 
vocabulary = analyze.vocabulary_;
feature_names = analyze.get_feature_names_out();
n_words = DT_train.shape[1];

# document-term matrix in dense form 
doc_term_train = DT_train.toarray();

# Fitting Logistic Regression
clf_logreg = LogisticRegression();
clf_logreg.fit(DT_train,ds_train.target);
acc_logreg = clf_logreg.score(DT_train,ds_train.target);
pred_target = clf_logreg.predict(DT_train);
cm_logreg = confusion_matrix(pred_target,ds_train.target);

# Classification report for Logistic Regression
report_logreg = classification_report(ds_train.target, pred_target,
                                    target_names=ds_train.target_names, output_dict=True);                              
print(classification_report(ds_train.target, pred_target,
                                    target_names=ds_train.target_names));     

# Drawing ROC curve
fpr, tpr, thresholds = roc_curve(ds_train.target, pred_target);
roc_auc = auc(fpr, tpr);
display = RocCurveDisplay(fpr=fpr, tpr=tpr, roc_auc=roc_auc,
                                  name='Logistic regression');
display.plot();
plt.show();

# Analyzing the test data
# transforming the test dataset            
DT_test = analyze.transform(ds_test.data);
acc_logreg_test = clf_logreg.score(DT_test,ds_test.target);
pred_target_test = clf_logreg.predict(DT_test);
cm_logreg_test = confusion_matrix(pred_target_test,ds_test.target);


# Drawing ROC curve
fpr, tpr, thresholds = roc_curve(ds_test.target, pred_target_test);
roc_auc = auc(fpr, tpr);
display = RocCurveDisplay(fpr=fpr, tpr=tpr, roc_auc=roc_auc,
                                  name='Logistic regression');
display.plot();
plt.show();

# Fitting Multinomial Naive Bayes model
alpha = 1;
clf_MNB = MultinomialNB(alpha=alpha);
clf_MNB.fit(DT_train,ds_train.target);
category_train_pred_MNB = clf_MNB.predict(DT_train);
train_accuracy_MNB = clf_MNB.score(DT_train,ds_train.target);
train_accuracy1_MNB = np.mean(category_train_pred_MNB == ds_train.target);
prob_train_MNB = clf_MNB.predict_proba(DT_train);

# Assessing MNB 
train_conf_mat = confusion_matrix(ds_train.target, 
                category_train_pred_MNB); #, normalize = 'all');
train_mcm = multilabel_confusion_matrix(ds_train.target, 
                category_train_pred_MNB);

print(classification_report(ds_train.target, category_train_pred_MNB,
                                    target_names=ds_train.target_names));
                                    
cf_report = classification_report(ds_train.target, category_train_pred_MNB,
                                    target_names=ds_train.target_names, output_dict=True);                              
                                    



# document-term matrix in dense form 
doc_term_test = DT_test.toarray();
                                    
ds_test_pred = clf_MNB.predict(DT_test);
test_accuracy = np.mean(ds_test_pred == ds_test.target);

print(classification_report(ds_test.target, ds_test_pred,
                                    target_names=ds_test.target_names));

test_conf_mat = confusion_matrix(ds_test.target, 
            ds_test_pred) #, normalize = 'all');

test_proba = clf_MNB.predict_proba(DT_test);

import sklearn.neighbors as ng; 
n_ng = 10;
clf_KNN = ng.KNeighborsClassifier(n_neighbors=n_ng);
clf_KNN.fit(DT_train,ds_train.target);
ds_train_pred = clf_KNN.predict(DT_train);
train_accuracy = np.mean(ds_train_pred == ds_train.target);


from sklearn.linear_model import SGDClassifier;

clf_SGD = SGDClassifier(loss='hinge', penalty='l2',alpha=1e-3, 
                        random_state=42,max_iter=5, tol=None);
clf_SGD.fit(DT_train,ds_train.target);   
ds_train_pred = clf_SGD.predict(DT_train);
train_accuracy = np.mean(ds_train_pred == ds_train.target);

print(classification_report(ds_train.target, ds_train_pred,
                                    target_names=ds_train.target_names));   
                                    
ds_test_pred = clf_SGD.predict(DT_test);
test_accuracy = np.mean(ds_test_pred == ds_test.target);

print(classification_report(ds_test.target, ds_test_pred,
                                    target_names=ds_test.target_names));

test_conf_mat = confusion_matrix(ds_test.target, ds_test_pred);     

neural = MLPClassifier(hidden_layer_sizes=(2),max_iter=400);
neural.fit(DT_train,ds_train.target);
train_accuracy_neural = neural.score(DT_train,ds_train.target);

DT_test = vectorizer.transform(ds_test.data);
test_accuracy_neural = neural.score(DT_test,ds_test.target);                           