

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics import f1_score
from sklearn.metrics import accuracy_score
import sklearn.metrics as metrics
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline




"""
**Explore the Data**
"""
"""
To read the required csv file
"""
diabete=pd.read_csv('/kaggle/input/pima-indians-diabetes-database/diabetes.csv')
diabete.head()
diabete.tail()
"""
There are 767 rows in the dataset
"""
diabete.describe()
diabete.info()
diabete.isna().sum()

print("Dataset shape is", diabete.shape)
"""
**Visualization of the dataset**
"""
sns.pairplot(diabete)
"""
**Finding the correlation**
"""
diabe_corr=diabete.corr()
sns.heatmap(diabe_corr)
#Replace the zeros
feature_col=['Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age']
for column in feature_col:
    diabete[column]=diabete[column].replace(0,np.NaN)
    mean=diabete[column].mean(skipna=True)
    diabete[column]=diabete[column].replace(np.NaN,mean)  
"""
**Spliting the dataset**
"""
x=diabete[feature_col]
y=diabete[['Outcome']]
#spliting the dataset into train and test
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=.2,random_state=0 )
#feature scaling
scalar=StandardScaler()
x_train=scalar.fit_transform(x_train)
x_test=scalar.transform(x_test) 
"""
**Define the model : init K-NN**
"""
clf=KNeighborsClassifier(n_neighbors=11,p=2,metric='euclidean')
clf.fit(x_train,y_train)
y_predict=clf.predict(x_test)

"""
**Evaluate the model**
"""

print(confusion_matrix(y_test,y_predict))


print(f1_score(y_test,y_predict))
print(accuracy_score(y_test,y_predict))
#Function to perform training with entropy
clf=DecisionTreeClassifier(criterion='entropy',max_depth=3,random_state=100,min_samples_split=4)
clf.fit(x_train,y_train)
y_predict=clf.predict(x_test)
y_predict
#Checking the accuracy
print("the accuracy is",metrics.accuracy_score(y_predict,y_test))
from sklearn import tree
plt.figure(figsize=(16,7),dpi=200)
tree.plot_tree(clf,feature_names=feature_col,class_names=["0","1"],filled=True,rounded=True);