text_plain_output_1.png
import os

import pandas as pd

import scipy.stats as stat

dataset_path = '/kaggle/input/diabetes-readmission-prediction-i43/'
df = pd.read_csv(os.path.join(dataset_path, 'train.csv'))

columns_to_drop = ['weight', 'payer_code', 'medical_specialty', 'max_glu_serum', 'A1Cresult']
df = df.drop(columns=columns_to_drop)
df = df.dropna()

missing_values = round(100 * df.isnull().sum() / df.shape[0], 1)
missing_values

def chi_square(df, output, significance_error):
    for col in df.columns:
        data_crosstab = pd.crosstab(df[col], df[output], margins=True, margins_name='Total')
        chi_square = 0
        rows = df[col].unique()
        columns = df[output].unique()
        for i in columns:
            for j in rows:
                O = data_crosstab[i][j]
                E = data_crosstab[i]['Total'] * data_crosstab['Total'][j] / data_crosstab['Total']['Total']
                chi_square += (O - E) ** 2 / E
        p_value = stat.chi2.sf(chi_square, (len(rows) - 1) * (len(columns) - 1))
        dicision = 'rejected' if p_value <= significance_error else 'failed to reject'
        print(f'{col:<25}', f"'{dicision:^20}'", ' -> chisquare-score is:', f'{chi_square:<20}', ' and p value is:', p_value)
chi_square(df, 'readmitted', 0.001)