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

import keras 


from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np


import xgboost as xgb

data= pd.read_csv("C://Users/<NAME>//Desktop//.ipynb_checkpoints//Training Data.csv")

data.head()

data.info()

from scipy import stats


# +
# df.drop('EtCO2',axis=1,inplace=True)

# +
#df.drop('bekaar',axis=1,inplace=True)
# -

F,p=stats.f_oneway(df)
### anova

from pylab import rcParams

missing = (data.isnull().sum() / data.shape[0]) * 100
missing

# +
rcParams['figure.figsize'] = 14, 5
_ = plt.bar(missing.index, missing)
_ = plt.xticks(missing.index, rotation=90)


plt.title("missing data percentage")

# +

selected_cols = list(missing[missing < 90].index)
selected_cols

# +

fdata = data[selected_cols]
fdata.head()
# -

categorical = fdata.dtypes == object
categorical['Unit1'] = True
categorical['Unit2'] = True
categorical['Gender'] = True

cat_vars = list(categorical[categorical].index)
cont_vars = list(categorical[~categorical].index)
cont_vars.pop(-1)

import seaborn as sns

rcParams['figure.figsize'] = 5, 5
for i,var in enumerate(cont_vars):
    plt.figure(i)
    plt.title("Density plot of " + str(var))
    sns.distplot(fdata[var].dropna(), axlabel=var);

x = np.linspace(start=-10, stop=10, num=1000)
y = stats.norm.pdf(x, loc=0, scale=1.5) 
plt.plot(x, y)

data1=data.HR

sns.distplot(data1, bins=20, kde=False)


data1=data.Bilirubin_direct

sns.distplot(data1, bins=20, kde=False)

data1=data.O2Sat
sns.distplot(data1, bins=20, kde=False)


class Gaussian:
    "Model univariate Gaussian"
    def __init__(self, mu, sigma):
        
        self.mu = mu
        self.sigma = sigma
    def pdf(self, datum):
        "Probability of a data point given the current parameters"
        u = (datum - self.mu) / abs(self.sigma)
        y = (1 / (sqrt(2 * pi) * abs(self.sigma))) * exp(-u * u / 2)
        return y
    def __repr__(self):
        return 'Gaussian({0:4.6}, {1:4.6})'.format(self.mu, self.sigma)


data1=data.HR
best_single = Gaussian(np.mean(data1), np.std(data1))
print('Best single Gaussian: μ = {:.2}, σ = {:.2}'.format(best_single.mu, best_single.sigma))

x = np.linspace(-6, 8, 200)
g_single = stats.norm(best_single.mu, best_single.sigma).pdf(x)
sns.distplot(data1, bins=20, kde=False, norm_hist=True)
plt.plot(x, g_single, label='single gaussian')
plt.legend();

fdata.loc[(fdata['Temp'] >= 36.4) & (fdata['Temp'] < 37.6),'data'] = 'sahi'
fdata.loc[(fdata['Temp'] < 36.4) | (fdata['Temp'] >= 37.6),'tapmaan'] = 'kharabh'
fdata['tapmaan'].fillna('ptanhi', inplace=True)

# +

fdata.loc[(fdata['HR'] >= 100) & (fdata['Age'] >= 10 ),'dhadhkan'] = 'kharabh'
fdata.loc[(data['HR'] < 100) & (fdata['HR'] > 60) & (fdata['Age'] >= 10 ),'dhadhkan'] = 'sahi'
fdata.loc[(data['HR'] >= 70) & (fdata['HR'] < 190) & (fdata['Age'] < 10 ),'dhadhkan'] = 'sahi'
fdata.loc[((fdata['HR'] < 70) | (fdata['HR'] >= 190)) & (fdata['Age'] < 10 ),'dhadhkan'] = 'kharabh'
fdata['dhadhkan'].fillna('ptanhi', inplace=True)
# -

fdata.head()

fdata.loc[(fdata['O2Sat'] >= 90) & (fdata['O2Sat'] < 100),'o2stat'] = 'sahi'
fdata.loc[(fdata['O2Sat'] < 90) & (fdata['O2Sat'] >= 0),'o2stat'] = 'kharabh'
fdata['o2stat'].fillna('ptanhi', inplace=True)

# +

fdata.head()
# -

fdata.loc[(fdata['Resp'].between(30,60)) & (fdata['Age'] <1),'sansein'] = 'sahi'
fdata.loc[((fdata['Resp'] < 30) | (fdata['Resp'] > 60)) & (fdata['Age'] <1) ,'sansein'] = 'kharabh'
fdata.loc[(fdata['Resp'].between(24,40)) & (fdata['Age'].between(1,3)),'sansein'] = 'sahi'
fdata.loc[((fdata['Resp'] < 24) | (fdata['Resp'] > 40)) & (fdata['Age'].between(1,3)) ,'sansein'] = 'kharabh'
fdata.loc[(fdata['Resp'].between(22,34)) & (fdata['Age'].between(3,6)),'sansein'] = 'sahi'
fdata.loc[((fdata['Resp'] < 22) | (fdata['Resp'] > 34)) & (fdata['Age'].between(3,6)) ,'sansein'] = 'kharabh'
fdata.loc[(fdata['Resp'].between(18,30)) & (fdata['Age'].between(6,12)),'sansein'] = 'sahi'
fdata.loc[((fdata['Resp'] < 18) | (fdata['Resp'] > 30)) & (fdata['Age'].between(6,12)) ,'sansein'] = 'kharabh'
fdata.loc[(fdata['Resp'].between(12,20)) & (fdata['Age'] >12),'sansein'] = 'sahi'
fdata.loc[((fdata['Resp'] < 12) | (fdata['Resp'] > 20)) & (fdata['Age'] >12),'sansein'] = 'kharabh'
fdata['sansein'].fillna('ptanhi', inplace=True)

fdata.head()

fdata.loc[fdata['Age'] >=65, 'umar'] = 'naman'
fdata.loc[fdata['Age'] <1, 'umar'] = 'mukul'
fdata.loc[(fdata['Age'] >=1) & (fdata['Age'] <65),'umar'] = 'jawani'

fdata.loc[(fdata['SBP'] <90) & (fdata['DBP'] <60), 'bp'] = 'low'
fdata.loc[(fdata['SBP'].between(90,120, inclusive=True)) & (fdata['DBP'].between(60,80, inclusive=True)),'bp'] = 'normal'
fdata.loc[(fdata['SBP'].between(120,140, inclusive=True)) & (fdata['DBP'].between(80,90, inclusive=True)),'bp'] = 'above normal'
fdata.loc[(fdata['SBP'] > 140 ) & (fdata['DBP'] > 90 ), 'bp'] = 'high'
fdata['bp'].fillna('ptanhi', inplace=True)

fdata.head()



df=pd.DataFrame(fdata)

df.head()

#df.drop('Resp',axis=1,inplace=True)
df.drop('HR',axis=1,inplace=True)
df.drop('O2Sat',axis=1,inplace=True)
df.drop('Temp',axis=1,inplace=True)
df.drop('SBP',axis=1,inplace=True)
df.drop('bekaar',axis=1,inplace=True)
df.drop('DBP',axis=1,inplace=True)
df.drop('data',axis=1,inplace=True)

df.head()

data.head(50)

df.describe()

df.head()

df.corr()

corr=df.corr().SepsisLabel
print(corr)


# +

def plot_corr(df,size=10):


    corr = df.corr()
    fig, ax = plt.subplots(figsize=(size, size))
    ax.matshow(corr)
    plt.xticks(range(len(corr.columns)), corr.columns);
    plt.yticks(range(len(corr.columns)), corr.columns);
# -

x=plot_corr(df,10)

from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

# +

y=df['Bilirubin_direct'].values
X=df['Bilirubin_total'].values





# +

X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.viridis)

plt.show()
# -

sns.heatmap(df)

# +

#pd.plotting.scatter_matrix(df, alpha=0.2, figsize=(10, 10))
#plt.show()

# + active=""
# corrmat = df.corr() 
#   
# cg = sns.clustermap(corrmat, cmap ="YlGnBu", linewidths = 0.1); 
# plt.setp(cg.ax_heatmap.yaxis.get_majorticklabels(), rotation = 0) 
#   
# cg 
# -

import pingouin as pg

corr=df.corr().SepsisLabel

df_corr=pd.DataFrame(corr)

df_corr.head()

df_corr.sort_values(ascending=False,by="SepsisLabel")

df=pd.get_dummies(df)


df.head()

df.drop('Resp',axis=1,inplace=True)


df.columns

df.drop('Age',axis=1,inplace=True)
df.drop('dhadhkan_ptanhi',axis=1,inplace=True)
df.drop('o2stat_ptanhi',axis=1,inplace=True)
df.drop('sansein_ptanhi',axis=1,inplace=True)
df.drop('bp_ptanhi',axis=1,inplace=True)
df.drop('umar_jawani',axis=1,inplace=True)


df.head()

df.columns

X_int=df.drop('SepsisLabel',axis=1).values
Y_int=df['SepsisLabel'].values

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_int,Y_int, test_size=0.2,random_state=42)

# +
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import Imputer 
steps = [ ('imp', Imputer(missing_values="NaN",strategy="mean",axis=0)),
          ('clf', xgb.XGBClassifier(objective='binary:logistic', n_estimators=1000, seed=1))]



pipeline = Pipeline(steps)


pipeline.fit(X_train, y_train)
pipeline.predict(X_test)
pipeline.score(X_test, y_test)
# -



StarcoderdataJupyterScriptsDedupFiltered