<jupyter_start><jupyter_text>Kidney Disease Status  Prediction Md <NAME> <EMAIL> https://github.com/ruhulamin005/ Project Link: https://github.com/ruhulamin005/Predict-status-of-Chronic-kidney-disease<jupyter_code>#Preprocessing
# preparing for modeling 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv('D:\Python\pyLearning\Dataset\kidney\kidney_disease.csv')
df.head()
#changing column name with a smarter approach
columns = pd.read_csv('D:/Python/pyLearning/Dataset/kidney/data_description.txt',sep='-')
columns = columns.reset_index()
columns.columns = ['cols','abb_col_names']
columns
#renaming columns names
df.columns = columns['abb_col_names'].values
df.head()
df.dtypes
#coverting data_types
def convert_dtype(df,feature):
    df[feature] = pd.to_numeric(df[feature],errors='coerce')
features = ['packed cell volume','white blood cell count','red blood cell count']

for feature in features:
    convert_dtype(df,feature)
df.dtypes
df.drop('id',axis=1, inplace=True)
#Data Cleaning for analysing data
df.head()
def extract_cat_num(df):
    cat_col=[col for col in df.columns if df[col].dtype=='object']
    num_col=[col for col in df.columns if df[col].dtype!='object']
    return cat_col,num_col
cat_col,num_col=extract_cat_num(df)
cat_col
num_col
for col in cat_col:
    print('{} has {} values '.format(col,df[col].unique()))
    print('\n')
#Here we have some unwanted situation
#Cleaning 
#Replace incorrect values

df['diabetes mellitus'].replace(to_replace = {'\tno':'no','\tyes':'yes',' yes':'yes'},inplace=True)

df['coronary artery disease'] = df['coronary artery disease'].replace(to_replace = '\tno', value='no')

df['class'] = df['class'].replace(to_replace = 'ckd\t', value = 'ckd')

#Checiking Values
for col in cat_col:
    print('{} has {} values  '.format(col, df[col].unique()))
    print('\n')
#Distribution Of Data
plt.figure(figsize=(30,20))

for i, feature in enumerate(num_col):
    plt.subplot(5,3,i+1)
    df[feature].hist()
    plt.title(feature)
#Label Disribution of categorial data
## ckd, notckd
len(cat_col)
plt.figure(figsize=(20,20))
for i,feature in enumerate(cat_col):
    plt.subplot(4,3,i+1)
    sns.countplot(df[feature])
    
sns.countplot(df['class'])
#How columns are co related with each other
df.corr()
plt.figure(figsize=(10,8))
df.corr()
sns.heatmap(df.corr(),annot=True)
df.groupby(['red blood cells','class'])['red blood cell count'].agg(['count','mean','median','max'])
#Impact on our target feature
#!pip install plotly
import plotly.express as px
df.columns
px.violin(df,y= 'red blood cell count', x='class',color='class')
#relationship between hemo & packed cell
df.columns
px.scatter(df,x='haemoglobin', y =  'packed cell volume')
grid = sns.FacetGrid(df,hue='class',aspect = 2)
grid.map(sns.kdeplot,'red blood cell count')
grid.add_legend()
#Automate this analysis
def violin(col):
    fig=px.violin(df,y=col,x='class',color = 'class',box=True)
    return fig.show()
def scatters(col1,col2):
    fig = px.scatter(df,x=col1,y=col2,color='class')
    return fig.show()
 def kde_plot(feature):   
    grid = sns.FacetGrid(df,hue='class',aspect = 2)
    grid.map(sns.kdeplot,feature)
    grid.add_legend()
    
kde_plot('red blood cell count')
kde_plot('haemoglobin')
#Explotary data analysis on Data
#Perform EDA on Data
df.columns
scatters('red blood cell count','packed cell volume')
scatters('packed cell volume','haemoglobin')
violin('red blood cell count')
violin('packed cell volume')
scatters('red blood cell count','albumin')
#Data cleaning for missing values
df.isna().sum().sort_values(ascending = False)
#Fill missing values with mean, median, std dev
sns.countplot(df['red blood cells'])
data = df.copy()
data.head()
data['red blood cells'].dropna().sample()
data['red blood cells'].isnull().sum()
random_sample = data['red blood cells'].dropna().sample(152)
random_sample
data[data['red blood cells'].isnull()].index
random_sample.index
random_sample.index =  data[data['red blood cells'].isnull()].index
random_sample

data.loc[data['red blood cells'].isnull(),'red blood cells'] = random_sample
data['red blood cells'].isnull().sum()
sns.countplot(data['red blood cells'])
def Random_value_Imputation(feature):
    random_sample = data[feature].dropna().sample(data[feature].isnull().sum())
    random_sample.index =  data[data[feature].isnull()].index
    data.loc[data[feature].isnull(),feature] = random_sample
data[num_col].isnull().sum()
data[cat_col].isnull().sum()
for col in num_col:
    Random_value_Imputation(col)
data[num_col].isnull().sum()
Random_value_Imputation(' pus cell')
data['pus cell clumps'].mode(0)
def impute_mode(feature):
    mode = data[feature].mode()[0]
    data[feature] = data[feature].fillna(mode)
for col in cat_col:
    impute_mode(col)
data[cat_col].isnull().sum()
#fully cleaned
#feature Encoding
#coverting all categorical data into numeric data
for col in cat_col:
    print('{} has {} categories'.format(col,data[col].nunique()))
#label Encoding
#Assigning label
#Normal --0
# Abnormal --1
#
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
for col in cat_col:
    data[col]=le.fit_transform(data[col])
data.head()
#Select features for machine learning model
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
ind_col =[col for col in data.columns if col!='class']
dep_col='class'
X= data[ind_col]
y = data[dep_col]
X.head()
y
ordered_rank_features = SelectKBest(score_func=chi2,k=20)
ordered_feature = ordered_rank_features.fit(X,y)
ordered_feature
ordered_feature.scores_
datascores = pd.DataFrame(ordered_feature.scores_,columns=['Score'])
datascores
dfcols = pd.DataFrame(X.columns)
dfcols
features_rank = pd.concat([dfcols,datascores],axis=1)
features_rank
features_rank.columns=['feature','Score']
features_rank
features_rank.nlargest(10,'Score')
selected_columns = features_rank.nlargest(10,'Score')['feature'].values
selected_columns
X_new = data[selected_columns]
X_new.head()
len(X_new)
X_new.shape
from sklearn.model_selection import train_test_split
X_train,X_test, y_train,y_test =  train_test_split(X_new,y,random_state=0,test_size=0.25)
print(X_test.shape)
print(X_train.shape)
y_train.value_counts()
!pip install xgboost
from xgboost import XGBClassifier
XGBClassifier()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup