In [1]:
import pandas as pd
import numpy as np
from matplotlib.pyplot import subplots
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import r2_score, mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
In [2]:
ckd = pd.read_csv('ckd.csv')
ckd = ckd[['Age', 'Blood Pressure', 'Blood Glucose Random', 'White Blood Cell Count', 'Red Blood Cell Count', 'Class']]
ckd
Out[2]:
| Age | Blood Pressure | Blood Glucose Random | White Blood Cell Count | Red Blood Cell Count | Class | |
|---|---|---|---|---|---|---|
| 0 | 48 | 70 | 117 | 6700 | 3.9 | 1 |
| 1 | 53 | 90 | 70 | 12100 | 3.7 | 1 |
| 2 | 63 | 70 | 380 | 4500 | 3.8 | 1 |
| 3 | 68 | 80 | 157 | 11000 | 2.6 | 1 |
| 4 | 61 | 80 | 173 | 9200 | 3.2 | 1 |
| ... | ... | ... | ... | ... | ... | ... |
| 153 | 55 | 80 | 140 | 6700 | 4.9 | 0 |
| 154 | 42 | 70 | 75 | 7800 | 6.2 | 0 |
| 155 | 12 | 80 | 100 | 6600 | 5.4 | 0 |
| 156 | 17 | 60 | 114 | 7200 | 5.9 | 0 |
| 157 | 58 | 80 | 131 | 6800 | 6.1 | 0 |
158 rows × 6 columns
In [3]:
# Logistic Regression
X = ckd[['Age', 'Blood Pressure', 'Blood Glucose Random', 'White Blood Cell Count', 'Red Blood Cell Count']]
y = ckd['Class']
# Create a train:test split of the data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.30, random_state=42, stratify=y
)
# We shall use the StandardScaler to scale the training dataset
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
# The transform method is then used to scale the test data using parameters
# obtained from the training data.
X_test_scaled = scaler.transform(X_test)
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression(max_iter=1000)
logreg.fit(X_train, y_train)
y_pred = logreg.predict(X_test)
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
# Evaluate
print('Evaluation of Logistic Regression classification')
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nConfusion Matrix:")
print(confusion_matrix(y_test, y_pred))
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
Evaluation of Logistic Regression classification
Accuracy: 0.9583333333333334
Confusion Matrix:
[[34 1]
[ 1 12]]
Classification Report:
precision recall f1-score support
0 0.97 0.97 0.97 35
1 0.92 0.92 0.92 13
accuracy 0.96 48
macro avg 0.95 0.95 0.95 48
weighted avg 0.96 0.96 0.96 48
In [4]:
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=9)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
# Evaluate
print('Evaluation of KNN classification')
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nConfusion Matrix:")
print(confusion_matrix(y_test, y_pred))
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
Evaluation of KNN classification
Accuracy: 0.8541666666666666
Confusion Matrix:
[[35 0]
[ 7 6]]
Classification Report:
precision recall f1-score support
0 0.83 1.00 0.91 35
1 1.00 0.46 0.63 13
accuracy 0.85 48
macro avg 0.92 0.73 0.77 48
weighted avg 0.88 0.85 0.83 48
In [5]:
X = ckd[['Age', 'Blood Pressure', 'Blood Glucose Random', 'White Blood Cell Count', 'Red Blood Cell Count']]
y = ckd['Class']
# Create a train:test split of the data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.30, random_state=42, stratify=y
)
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
# Create and train the classifier
gnb_model = GaussianNB()
gnb_model.fit(X_train, y_train)
# Make predictions
y_pred = gnb_model.predict(X_test)
# Evaluate
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nConfusion Matrix:")
print(confusion_matrix(y_test, y_pred))
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
Accuracy: 0.9791666666666666
Confusion Matrix:
[[35 0]
[ 1 12]]
Classification Report:
precision recall f1-score support
0 0.97 1.00 0.99 35
1 1.00 0.92 0.96 13
accuracy 0.98 48
macro avg 0.99 0.96 0.97 48
weighted avg 0.98 0.98 0.98 48
In [ ]: