<jupyter_start><jupyter_text>Data Analysis<jupyter_code>import numpy as np
import pandas as pd 

from sklearn.preprocessing import StandardScaler

from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split


import plotly.graph_objects as go
from scipy.stats import multivariate_normal as mn

import matplotlib.pyplot as plt
import seaborn as sns
db_dataset = pd.read_csv("C:\\Users\\SANAH\\Desktop\\diabetes.csv")
db_dataset.head()
db_dataset.shape
db_dataset.info()
db_dataset.isnull().sum()
#stats of the data
db_dataset.describe()
db_dataset['Outcome'].value_counts()
db_dataset.groupby('Outcome').mean()<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code># Replacing zero values with NaN
db_dataset_new = db_dataset
db_dataset_new[["Glucose", "BloodPressure", "SkinThickness", "Insulin", "BMI"]] = db_dataset_new[["Glucose", "BloodPressure", "SkinThickness", "Insulin", "BMI"]].replace(0, np.NaN) 
# Count of NaN
db_dataset_new.isnull().sum()
db_dataset_new = db_dataset_new.drop('Insulin',axis=1)
db_dataset_new = db_dataset_new.dropna()
db_dataset_new.isnull().sum()
db_dataset_new.shape
db_dataset_new.agg(['mean','median'])
db_dataset_new.describe()<jupyter_output><empty_output><jupyter_text>Data Visualization<jupyter_code>sns.countplot( x = 'Outcome', data = db_dataset_new)
histo_data =db_dataset_new.loc[:,'Pregnancies': 'Age']
histo_data.hist(bins = 20, figsize = (15,10))
sns.pairplot(data = db_dataset_new, hue = 'Outcome')
plt.show()
sns.heatmap(db_dataset_new.corr(), annot = True)
plt.show()<jupyter_output><empty_output><jupyter_text>Probablistic Analysis<jupyter_code>data = db_dataset_new[["Glucose","BMI","Age","Outcome"]]
#split -> df_y for diabetic and df_n for non diabetic
df_y = data.loc[data["Outcome"] ==1, ["Glucose","BMI", "Age"]]
df_n = data.loc[data["Outcome"] ==0, ["Glucose","BMI", "Age"]]

fig = go.Figure()
fig.add_trace(go.Scatter3d(x=df_y['Glucose'], y=df_y['BMI'], z=df_y['Age'],mode='markers',marker=dict(color="pink"),name='Diabetic'))
fig.add_trace(go.Scatter3d(x=df_n['Glucose'], y=df_n['BMI'], z=df_n['Age'],mode='markers',marker=dict(color="purple"),name='Non Diabetic'))

fig.show()
test_point = [147, 32, 38]
tx = test_point[0]
ty = test_point[1]
tz = test_point[2]
fig = go.Figure()
fig.add_trace(go.Scatter3d(x=df_y['Glucose'], y=df_y['BMI'], z=df_y['Age'],mode='markers',marker=dict(color="pink"),name='Diabetic'))
fig.add_trace(go.Scatter3d(x=df_n['Glucose'], y=df_n['BMI'], z=df_n['Age'],mode='markers',marker=dict(color="purple"),name='Non Diabetic'))
fig.add_trace(go.Scatter3d(x= [tx], y= [ty], z= [tz],mode='markers',marker=dict(color="cyan"),name='Non Diabetic'))

fig.show()
probability = []

for d in [df_y,df_n]:
    mean = np.mean(d)
    cov = np.cov(d, rowvar = 0)
    prob = mn.pdf(test_point, mean, cov)
    probability.append(prob)

rows_y = df_y.shape[0]
rows_n = df_n.shape[0]

f_probability = rows_n *probability[1] / (rows_n *probability[1] + rows_y *probability[0]) 
print(f"Chance of person being non-diabetic is {100 * f_probability:.2f}%")<jupyter_output>Chance of person being non-diabetic is 46.48%<jupyter_text>Data Modeling<jupyter_code>X,Y = data.values[:,:-1], data.values[:,-1]
X = np.array(X)
Y = np.array(Y)
Y = Y.reshape(-1,1)
X.shape
X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size = 0.2, random_state=42)
LR_model = LogisticRegression()
LR_model.fit(X_train,Y_train)
accuracy = LR_model.score(X_train,Y_train)
accuracy
accuracy = LR_model.score(X_test,Y_test)
accuracy
# Support Vector Classifier Algorithm
from sklearn.svm import SVC
svc = SVC(kernel = 'linear', random_state = 42)
svc.fit(X_train, Y_train)
Y_pred_svc = svc.predict(X_test)
accuracy_svc = accuracy_score(Y_test, Y_pred_svc)
print("Support Vector Classifier: " + str(accuracy_svc * 100))
from sklearn.tree import DecisionTreeClassifier
dectree = DecisionTreeClassifier(criterion = 'entropy', random_state = 42)
dectree.fit(X_train, Y_train)
Y_pred_dectree = dectree.predict(X_test)
accuracy_dectree = accuracy_score(Y_test, Y_pred_dectree)
accuracy_dectree
from sklearn.ensemble import RandomForestClassifier
ranfor = RandomForestClassifier(n_estimators = 11, criterion = 'entropy', random_state = 42)
ranfor.fit(X_train, Y_train)
Y_pred_ranfor = ranfor.predict(X_test)
accuracy_ranfor = accuracy_score(Y_test, Y_pred_ranfor)
accuracy_ranfor<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15657245401525346, 'fraction_numerical': 0.01301031852848811, 'mean_word_length': 8.212809917355372, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '621032', 'license': 'MIT', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1734, 'n_words': 295, 'path': 'model.ipynb', 'repo_name': 'SanahSidhu/Diabetes-Predictor', 'size': '4458'}