# ---
# 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
# ---

# ## Mental disorder binary classifications 

# Based on the students details we predict the mental disorder classification yes or no. In here we handle class imbalance problem. Its binary classification problem. In this notebook we did these following steps. 

# ### 1. Importing Libraries 

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sklearn.model_selection import train_test_split
import warnings
warnings.filterwarnings('ignore')
# %matplotlib inline
pd.set_option("display.max_columns", 100)

# ### 2. Loading Datasets

# Load the datasets
file = "datasets/clean_data.csv"
df = pd.read_csv(file)

# ### 3. Explore Datasets

# +

df.head()
# -

df.shape

df.info()

df.columns

# ### 4. Data preprocessing

# +
# Data cleaning
# Drop unwanted columns

df.drop(['Unnamed: 0'], axis=1, inplace=True)
# -

# Checking missing values
df.isna().sum().sum()

df.columns

# ### 5. Exploratory Data Analysis

plt.title('Mental Disorder of the student', fontsize=22)
sns.countplot(x=df.MentalDisorder);

df['MentalDisorder'].value_counts(normalize=True)

plt.title('Gender of the student', fontsize=22)
sns.countplot(x=df.Gender);

plt.title('City of the student', fontsize=22)
sns.countplot(x=df.City);

df['Age'].hist();

# label 0 and 1
df['MentalDisorder'].replace(['Yes','No'],[1, 0],inplace=True)

# ### 6. Input and Target Columns

# Sperate the data, features and label
features_df = df.drop('MentalDisorder', axis='columns')
labels_df = df.MentalDisorder

# for predict one row
sample_predict_data = features_df.sample(1)

# ### 7. Encode categorical columns to one-hot vectors

# Encoder
numeric_cols = features_df.columns[features_df.dtypes != "object"].values
print(numeric_cols)

categori_cols = features_df.columns[features_df.dtypes == "object"].values
print(categori_cols)

features_df[categori_cols].nunique()

from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder(sparse=False, handle_unknown='ignore')
encoder.fit(features_df[categori_cols])

encoder.categories_

encoded_cols = list(encoder.get_feature_names(categori_cols))
print(encoded_cols)

features_df[encoded_cols] = encoder.transform(features_df[categori_cols])

# +
# features_df.head()
# -

features_df.shape

# ### 8. Train Test Split

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(features_df, labels_df, stratify = labels_df, test_size=0.2, random_state=42)

print(X_train.shape)
print(X_test.shape)

y_train.value_counts(normalize=True)

y_test.value_counts(normalize=True)

numeric_cols = X_train.select_dtypes(include=np.number).columns.tolist()
categorical_cols = X_train.select_dtypes('category').columns.tolist()

X_train = X_train[numeric_cols]
X_test = X_test[numeric_cols]
print(X_train.shape)

# ### 9. Model training & Evaluation

from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import plot_confusion_matrix

# define class weights for class imblance problem. 70 percent in 0 label and 30 in 1
w = {0:0.7, 1:0.3}

model = LogisticRegression(solver='liblinear',random_state = 22, class_weight=w)
model.fit(X_train, y_train)
train_pred = model.predict(X_train)
test_pred = model.predict(X_test)

# +
train_acc = accuracy_score(y_train, train_pred)
test_acc = accuracy_score(y_test, test_pred)


print(f"Train Accuracy = {train_acc} ")
print(f"Test Accuracy = {test_acc} ")
# -

print(classification_report(y_train, train_pred, labels=[0, 1]))

print(classification_report(y_test, test_pred, labels=[0, 1]))

# +
from sklearn.metrics import confusion_matrix, accuracy_score

cm = confusion_matrix(y_test, test_pred)
print(cm)
accuracy_score(y_test, test_pred)
# -

plot_confusion_matrix(model, X_test, y_test)  
plt.show()

# ### 10. Model store and load the model

import pickle
filename = 'saved_model/final_mental_model.pkl'
pickle.dump(model, open(filename, 'wb'))

# load the model from disk
train_model = pickle.load(open(filename, 'rb'))

# ### 11. Prediction of input data

sample_predict_data

sample_predict_data.shape

# +
from sklearn.preprocessing import OneHotEncoder

def predict_the_result(input_data, model_name):
    
    """ Predict the result based on the input values"""
    numeric_cols = input_data.columns[input_data.dtypes != "object"].values
    categori_cols = input_data.columns[input_data.dtypes == "object"].values
    
    
    encoder = OneHotEncoder(sparse=False, handle_unknown='ignore')
    encoder.fit(features_df[categori_cols])
    
    encoded_cols = list(encoder.get_feature_names(categori_cols))
    input_data[encoded_cols] = encoder.transform(input_data[categori_cols])
    
    numeric_cols = input_data.select_dtypes(include=np.number).columns.tolist()
    categorical_cols = input_data.select_dtypes('category').columns.tolist()
    
    input_data = input_data[numeric_cols]
    
    return model_name.predict(input_data)
# -

predict_the_result(sample_predict_data, train_model)

StarcoderdataJupyterScriptsDedupFiltered