In [1]: import numpy as np
import pandas as pd
import [Link] as plt
from sklearn.model_selection import KFold
from [Link] import StandardScaler
from [Link] import SVC
from [Link] import accuracy_score, precision_score, recall_score, f1_score, classificat
In [3]: # Load the Pima Indians Diabetes dataset
df = pd.read_csv(r"C:\Users\hp\Downloads/[Link]") # Change path if needed
[Link]()
Out[3]: Pregnancies Glucose BloodPressure SkinThickness Insulin BMI DiabetesPedig
0 6 148 72 35 0 33.6
1 1 85 66 29 0 26.6
2 8 183 64 0 0 23.3
3 1 89 66 23 94 28.1
4 0 137 40 35 168 43.1
In [4]: [Link]().sum()
Out[4]: Pregnancies 0
Glucose 0
BloodPressure 0
SkinThickness 0
Insulin 0
BMI 0
DiabetesPedigreeFunction 0
Age 0
Outcome 0
dtype: int64
In [10]: columns_with_zero = ['Glucose','BloodPressure','SkinThickness','Insulin','BMI']
for col in columns_with_zero:
[Link][df[col] == 0, col] = df[col].median()
# Show first 5 rows to confirm values updated
[Link]()
Out[10]: Pregnancies Glucose BloodPressure SkinThickness Insulin BMI DiabetesPedig
0 6 148 72 35 30.5 33.6
1 1 85 66 29 30.5 26.6
2 8 183 64 23 30.5 23.3
3 1 89 66 23 94.0 28.1
4 0 137 40 35 168.0 43.1
In [11]: X = [Link]("Outcome", axis=1)
y = df["Outcome"]
In [12]: def evaluate_svc_model(model, X, y):
kf = KFold(n_splits=5, shuffle=True, random_state=42)
fold_accuracies = []
fold_metrics = []
confusion_matrices = []
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
fold = 1
for train_index, test_index in [Link](X):
X_train, X_test = X_scaled[train_index], X_scaled[test_index]
y_train, y_test = [Link][train_index], [Link][test_index]
[Link](X_train, y_train)
y_pred = [Link](X_test)
acc = accuracy_score(y_test, y_pred)
prec = precision_score(y_test, y_pred)
rec = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
fold_accuracies.append(acc)
fold_metrics.append({
"fold": fold,
"accuracy": acc,
"precision": prec,
"recall": rec,
"f1": f1,
"report": classification_report(y_test, y_pred, output_dict=True)
})
confusion_matrices.append(confusion_matrix(y_test, y_pred))
fold += 1
return fold_accuracies, fold_metrics, confusion_matrices
In [13]: model_linear = SVC(kernel='linear')
acc_lin, metrics_lin, conf_lin = evaluate_svc_model(model_linear, X, y)
acc_lin
Out[13]: [0.7662337662337663,
0.7792207792207793,
0.7467532467532467,
0.7908496732026143,
0.7581699346405228]
In [14]: model_rbf = SVC(kernel='rbf')
acc_rbf, metrics_rbf, conf_rbf = evaluate_svc_model(model_rbf, X, y)
acc_rbf
Out[14]: [0.7532467532467533,
0.7597402597402597,
0.7532467532467533,
0.7516339869281046,
0.7516339869281046]
In [15]: print("Linear SVC Average Accuracy:", [Link](acc_lin))
print("RBF SVC Average Accuracy:", [Link](acc_rbf))
Linear SVC Average Accuracy: 0.7682454800101859
RBF SVC Average Accuracy: 0.7539003480179951
In [16]: best_fold_linear = [Link](acc_lin)
print("Best fold:", best_fold_linear + 1)
conf_best_linear = conf_lin[best_fold_linear]
conf_best_linear
Best fold: 4
Out[16]: array([[96, 10],
[22, 25]])
In [17]: best_fold_rbf = [Link](acc_rbf)
print("Best fold:", best_fold_rbf + 1)
conf_best_rbf = conf_rbf[best_fold_rbf]
conf_best_rbf
Best fold: 2
Out[17]: array([[90, 17],
[20, 27]])
In [18]: print("----- Linear Kernel Classification Report -----")
print([Link](metrics_lin[best_fold_linear]['report']).T)
print("\n----- RBF Kernel Classification Report -----")
print([Link](metrics_rbf[best_fold_rbf]['report']).T)
----- Linear Kernel Classification Report -----
precision recall f1-score support
0 0.813559 0.905660 0.857143 106.00000
1 0.714286 0.531915 0.609756 47.00000
accuracy 0.790850 0.790850 0.790850 0.79085
macro avg 0.763923 0.718788 0.733449 153.00000
weighted avg 0.783064 0.790850 0.781148 153.00000
----- RBF Kernel Classification Report -----
precision recall f1-score support
0 0.818182 0.841121 0.829493 107.00000
1 0.613636 0.574468 0.593407 47.00000
accuracy 0.759740 0.759740 0.759740 0.75974
macro avg 0.715909 0.707795 0.711450 154.00000
weighted avg 0.755756 0.759740 0.757441 154.00000
In [19]: [Link](figsize=(10,6))
[Link](range(1,6), acc_lin, marker='o', label='Linear Kernel')
[Link](range(1,6), acc_rbf, marker='o', label='RBF Kernel')
[Link]("Fold")
[Link]("Accuracy")
[Link]("Accuracy Across 5 Folds for SVC Models")
[Link]()
[Link](True)
[Link]()