text_html_output_1.png text_plain_output_1.png
from sklearn.preprocessing import LabelEncoder

import matplotlib.pyplot as plt

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'
chi_square(df, 'readmitted', 0.001)

numerical_features = ['time_in_hospital', 'num_lab_procedures', 'num_procedures', 'num_medications', 'number_outpatient', 'number_emergency', 'number_inpatient', 'number_diagnoses']

n_df = df.loc[:, numerical_features].copy()
label_df = df.iloc[:, -1].copy()
id_df = df.iloc[:, :2].copy()

df = df.drop(columns=numerical_features + list(id_df.columns))

df = df.drop(columns='readmitted')

from sklearn.ensemble import RandomForestClassifier as RC
def feature_importance(df, col):
    test = pd.get_dummies(df[col])
    test = pd.concat([test, df['readmitted']], axis=1)
    model = RC(n_estimators=100)
    x = test.iloc[:, :-1]
    y = test.iloc[:, -1]
    model.fit(x, y)
    importance = model.feature_importances_

df2 = pd.concat([df, n_df, label_df], axis=1)
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
for i in list(df2.keys()):
    df2[i] = le.fit_transform(df2[i])

model = RC(n_estimators=100)
x = df2.iloc[:, :-1]
y = df2.iloc[:, -1]
model.fit(x, y)
importance = model.feature_importances_
columns = list(df2.columns[:-1])
pair = []
[pair.append((i, j)) for i, j in zip(importance, columns)]
sorted_pair = sorted(pair)
x = []
y = []
for i in sorted_pair:
    x.append(i[0])
    y.append(i[1])
plt.figure(figsize=(20, 20))
plt.barh(y, x)
plt.show()