# ---
# 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 pandas as pd
import numpy as np
import seaborn as sns
import pickle
import matplotlib.pyplot as plt

df=pd.read_excel("Cleaned Data.xlsx")

df.shape

df.iloc[0]

df.head()

columns_to_drop=['Region','I have my regular access to the internet','I am currently employed at least part-time','I am on section 8 housing','I receive food stamps','Annual income from social welfare programs','I have a gap in my resume','Total length of any gaps in my resume in\xa0months.','Household Income','Device Type']


# +
fig = plt.figure(figsize=(22,20))

fig.add_axes([0,0,1,1])
ax = fig.get_axes()[0]
sns.heatmap(df.corr(), ax=ax, vmin=-1, vmax=1, annot=True)
plt.savefig("correlation.jpg", bbox_inches="tight")
# -

cor=df.corr().iloc[1]
cor

df.drop(columns=columns_to_drop,inplace=True)

df.head().iloc[0].shape

df.head(10)

df.corr()

df['Annual income (including any social welfare programs) in Rupee']=df['Annual income (including any social welfare programs) in USD']*70

df.head()

df.drop('Annual income (including any social welfare programs) in USD',axis=1,inplace=True)

df.head(20)

df.info()

df['How many days were you hospitalized for your mental illness'].fillna(df['How many days were you hospitalized for your mental illness'].median(),inplace=True)

df.info()

for i in df:
    if i=='Education' or i=='Age' or i=='Gender':
        df[i].dropna()
    else:
        df[i].fillna(df[i].median(),inplace=True)

df.info()

y=df['I identify as having a mental illness']

df.drop('I identify as having a mental illness',axis=1,inplace=True)

df.head()

from sklearn.preprocessing import LabelEncoder

le_educatio=LabelEncoder()
le_age=LabelEncoder()
le_gender=LabelEncoder()

df['Education']=le_educatio.fit_transform(df['Education'])

df.head()

df['Age']=le_age.fit_transform(df['Age'])
df['Gender']=le_gender.fit_transform(df['Gender'])

df.head()

from sklearn.preprocessing import StandardScaler

ss=StandardScaler()

df2=ss.fit_transform(df)

df2.shape

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(df2, y, test_size=0.2, random_state=42)

X_train.shape

y_train.shape

from sklearn.linear_model import LogisticRegression

lr=LogisticRegression()

lr.fit(X_train,y_train)

y_pred=lr.predict(X_test)

from sklearn.metrics import confusion_matrix

cf_matrix=confusion_matrix(y_test,y_pred)

import matplotlib.pyplot as plt

sns.heatmap(cf_matrix, annot=True)
plt.savefig("confusion_matrix.jpg", bbox_inches="tight")

from sklearn.metrics import precision_score,recall_score

precision_score(y_test,y_pred)

recall_score(y_test,y_pred)

y_test=y_test.values

s=0
for i in range(y_test.shape[0]):
    if y_pred[i]==y_test[i]:
        s+=1
print(s/y_pred.shape[0])

for i in df:
    print(i)

df.head(10)

le_age.transform(['30-44'])

from sklearn.metrics import f1_score

f1_score(y_test,y_pred)

from sklearn.tree import DecisionTreeClassifier

dt=DecisionTreeClassifier()

dt.fit(X_train,y_train)

y_pred2=dt.predict(X_test)

y_pred2

s=0
for i in range(y_test.shape[0]):
    if y_pred2[i]==y_test[i]:
        s+=1
print(s/y_pred.shape[0])

precision_score(y_pred2,y_test)

recall_score(y_pred2,y_test)

import pickle

filename = 'model.sav'
pickle.dump(lr, open(filename, 'wb'))

X_test[0]

filename = 'Standart_scale.sav'
pickle.dump(ss, open(filename, 'wb'))

filename1='education_labelencoder.sav'
filename2='age_labelencoder.sav'
filename3='gender_labelencoder.sav'

pickle.dump(le_educatio, open(filename1, 'wb'))

pickle.dump(le_age, open(filename2, 'wb'))

pickle.dump(le_gender, open(filename3, 'wb'))

StarcoderdataJupyterScriptsDedupFiltered