0% found this document useful (0 votes)
8 views2 pages

Logistic Regression 2

VI

Uploaded by

s s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Logistic Regression 2

VI

Uploaded by

s s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import numpy as np

import matplotlib.pyplot as plt


from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report,
confusion_matrix, ConfusionMatrixDisplay

# Load the Iris dataset


iris = datasets.load_iris()
X = iris.data
y = iris.target

# Split the dataset into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)

# Define the kernels and their associated hyperparameters to try


kernels = ['linear', 'rbf', 'poly', 'sigmoid']
C_values = [0.01, 0.1, 1, 10]
gamma_values = ['scale', 'auto', 0.1, 1]

best_accuracy = 0
best_model = None
best_params = {}

# Iterate over each kernel and hyperparameter combination


for kernel in kernels:
for C in C_values:
for gamma in gamma_values:
# Initialize the SVM classifier with current parameters
model = SVC(kernel=kernel, C=C, gamma=gamma, random_state=42)

# Train the model


model.fit(X_train, y_train)

# Predict on the test set


y_pred = model.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)

# Print the current parameters and accuracy


print(f"Kernel={kernel}, C={C}, gamma={gamma},
Accuracy={accuracy:.4f}")

# Check if current model is better than previous best


if accuracy > best_accuracy:
best_accuracy = accuracy
best_model = model
best_params = {'kernel': kernel, 'C': C, 'gamma': gamma}

# Print the best model details


print(f"\nBest model hyperparameters: {best_params}")
print(f"Best classification accuracy: {best_accuracy:.4f}")

# Generate and display the classification report


y_pred_best = best_model.predict(X_test)
report = classification_report(y_test, y_pred_best, target_names=iris.target_names)
print("Classification Report:\n", report)

# Generate and display the confusion matrix


cm = confusion_matrix(y_test, y_pred_best)
cmd = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=iris.target_names)
cmd.plot(cmap=plt.cm.Blues)
plt.title('Confusion Matrix')
plt.show()

You might also like