# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# #  Null Value Computation

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.display import display
# %matplotlib inline

# set seed for reproducibility
SEED = 20
np.random.seed(SEED)

# Loading Data
df = pd.read_csv('diabetes.csv')
df.head()

print('Total zero Glucose values: ' + str(768-df['Glucose'].astype(bool).sum(axis=0)))
print('Total zero BloodPressure values: ' + str(768-df['BloodPressure'].astype(bool).sum(axis=0)))
print('Total zero SkinThickness values: ' + str(768-df['SkinThickness'].astype(bool).sum(axis=0)))
print('Total zero Insulin values: ' + str(768-df['Insulin'].astype(bool).sum(axis=0)))
print('Total zero BMI values: ' + str(768-df['BMI'].astype(bool).sum(axis=0)))
print('Total zero DiabetesPedigreeFunction values: ' + str(768-df['DiabetesPedigreeFunction'].astype(bool).sum(axis=0)))
print('Total zero Age values: ' + str(768-df['Age'].astype(bool).sum(axis=0)))
# These are all 0 values out of 768 in each field.
# We saw outliers during our data viz. Now we need to handle these
# Total zero values in DiabetesPedigreeFunction and Age variable is zero.
# Pregnancy field can be 0.

def replace_zero(df):
    df_nan=df.copy(deep=True)
    cols = ["Glucose","BloodPressure","SkinThickness","Insulin","BMI"]
    df_nan[cols] = df_nan[cols].replace({0:np.nan})  
replace_zero(df)

df_nan.isnull().sum()
# We have successfully replaced 0's with Null values
# for colums ["Glucose","BloodPressure","SkinThickness","Insulin","BMI"]

# Now we need to handle Nulls somehow
# to find the median for filling null values
# Function outputs median value for mentioned variable based on Outcome var
def replace_zero(df):
    df_nan=df.copy(deep=True)
    cols = ["Glucose","BloodPressure","SkinThickness","Insulin","BMI"]
    df_nan[cols] = df_nan[cols].replace({0:np.nan})
    return df_nan
df_nan=replace_zero(df)

find_median(df_nan,'Glucose')
# 107 is the median value for Glucose var for non-diab people
# 140 is the median value for Glucose var for diab people.

# Function to replace Null values with relevant median values
# returns number of Null values after computation (Should return 0 when called)
def replace_null(frame,var):
    median_df=find_median(frame,var)
    var_0=median_df[var].iloc[0]
    var_1=median_df[var].iloc[1]
    frame.loc[(frame['Outcome'] == 0) & (frame[var].isnull()), var] = var_0
    frame.loc[(frame['Outcome'] == 1) & (frame[var].isnull()), var] = var_1
    return frame[var].isnull().sum()


print(str(replace_null(df_nan,'Glucose'))+ ' Nulls for Glucose')
print(str(replace_null(df_nan,'SkinThickness'))+ ' Nulls for SkinThickness')
print(str(replace_null(df_nan,'Insulin'))+ ' Nulls for Insulin')
print(str(replace_null(df_nan,'BMI'))+ ' Nulls for BMI')
print(str(replace_null(df_nan,'BloodPressure'))+ ' Nulls for BloodPressure')
# We have successfully handled Nulls

df_nan.isnull().sum()
# Just a confirmation

plt.figure(dpi = 125,figsize= (5,4))
mask = np.triu(df.corr())
#np.triu returns lower triangle for our heatmap as we do not need upper map
sns.heatmap(df_nan.corr(),mask = mask, fmt = ".1f",annot=True,lw=0.1,cmap = 'YlGnBu')
plt.title('Correlation Map')
plt.show()
# New Correlation map has higher correlated values

#









StarcoderdataJupyterScriptsDedupFiltered