text_html_output_1.png
import pandas as pd

data = pd.read_csv('../input/mental-health-dataset/Mental Health Questionnaire 2.0.csv')

data.isna().sum()

data.isna().mean()

data = data.drop(['Timestamp', 'Email address', 'Name', 'Employment Status', 'Prediction'], axis=1)

data = data.drop(['City'], axis=1)

{column: len(data[column].unique()) for column in data.select_dtypes('object').columns}

{column: list(data[column].unique()) for column in data.select_dtypes('object').columns}

{column: len(data[column].unique()) for column in data.select_dtypes('float64').columns}

{column: len(data[column].unique()) for column in data.select_dtypes('int64').columns}

target = 'Prediction_status'
ordinal_features = ['How are you feeling today?', 'Is your sadness momentarily or has it been constant for a long time?', 'At what time of the day are you extremely low?', 'How frequently have you had little pleasure or interest in the activities you usually enjoy?', 'Describe how ‘supported’ you feel by others around you – your friends, family, or otherwise.', 'How frequently have you been doing things that mean something to you or your life?', 'How easy is it for you to take medical leave for a mental health condition?', 'How often do you make use of substance abuse(e.g. smoking, alcohol)?', 'How many hours do you spend per day on watching mobile phone, laptop, computer, television, etc.?', 'How often do you get offended or angry or start crying ?']
nominal_features = ['eating and sleeping', '(If sad)have you been in the same mental state for the past few days?', 'Has there been a sudden and huge change in your life?', 'Your stress is related to which of the following areas?', 'If you have a mental health condition, do you feel that it interferes with your work?', 'Have you taken any therapy or medication in the near past for mental health?', 'Having trouble concentrating on things, such as reading the newspaper or watching television, or studying?', 'Do you feel bad about yourself — or that you are a failure or have let yourself or your family down?', 'Has the COVID-19 pandemic affected your mental well being?']

def binary_encode(df, columns, positive_values):
    df = df.copy()
    for column, positive_value in zip(columns, positive_values):
        df[column] = df[column].apply(lambda x: 1 if x == positive_value else 0)
    return df
def ordinal_encode(df, columns, orderings):
    df = df.copy()
    for column, ordering in zip(columns, orderings):
        df[column] = df[column].apply(lambda x: ordering.index(x))
    return df
def nominal_encode(df, columns, prefixes):
    df = df.copy()
    for column, prefix in zip(columns, prefixes):
        dummies = pd.get_dummies(df[column], prefix)
        df = pd.concat([df, dummies], axis=1)
        df = df.drop(column, axis=1)
    return df

ordinal_orderings = [['Good', 'Fine', 'Sad', 'Depressed'], ['Not sad', 'For some time', 'Significant time', 'Long time'], ['Morning', 'Afternoon', 'Evening'], ['Never', 'Sometimes', 'Often', 'Very Often'], ['Highly supportive', 'Satisfactory', 'Little bit', 'Not at all'], ['Very Often', 'Often', 'Sometimes', 'Never'], ['Very easy', 'Easy', 'Not so easy', 'Difficult'], ['Never', 'Sometimes', 'Often', 'Very Often'], ['1-2 hours', '2-5 hours', '5-10 hours', 'More than 10 hours'], ['Never', 'Sometimes', 'Often', 'Very often']]
nominal_prefixes = ['es', 'smen', 'change', 'stress', 'inter', 'ther', 'conc', 'fbad', 'cov']

data = nominal_encode(data, columns=nominal_features, prefixes=nominal_prefixes)
data = ordinal_encode(data, columns=ordinal_features, orderings=ordinal_orderings)

data.select_dtypes('object')

data = binary_encode(data, columns=['Are you above 30 years of age?'], positive_values=['Yes'])

data = binary_encode(data, columns=['Prediction_status'], positive_values=['True'])

data.select_dtypes('object')

data.tail()