0% found this document useful (0 votes)
3 views6 pages

ml-5_31

The document outlines a series of Python scripts using the SVM classifier from the sklearn library to analyze the Iris dataset. It demonstrates training and testing the SVM model, calculating hinge loss and accuracy, and visualizing decision boundaries for different regularization parameters. The results indicate perfect accuracy on the test set and provide visual insights into the model's performance with varying regularization values.

Uploaded by

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

ml-5_31

The document outlines a series of Python scripts using the SVM classifier from the sklearn library to analyze the Iris dataset. It demonstrates training and testing the SVM model, calculating hinge loss and accuracy, and visualizing decision boundaries for different regularization parameters. The results indicate perfect accuracy on the test set and provide visual insights into the model's performance with varying regularization values.

Uploaded by

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

Ml- EXP 5

[ ]: import numpy as np
from sklearn import datasets
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import hinge_loss

iris = datasets.load_iris()
X = iris.data
y = iris.target
X = X[y != 2]
y = y[y != 2]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,␣
↪random_state=42)

svm = SVC(kernel='linear', C=1, random_state=42)


svm.fit(X_train, y_train)
y_pred = svm.predict(X_test)
decision_values = svm.decision_function(X_test)
hinge_loss_value = hinge_loss(y_test, decision_values)
print(f'Hinge Loss on Test Set: {hinge_loss_value}')
new_data_points = np.array([[5.1, 3.5, 1.4, 0.2],
[6.7, 3.1, 4.7, 1.5]])
new_predictions = svm.predict(new_data_points)
print(f'Predictions for new data points: {new_predictions}')

Hinge Loss on Test Set: 0.0


Predictions for new data points: [0 1]

[ ]: import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
data = datasets.load_iris()
X = data.data
y = data.target
X = X[y != 2]
y = y[y != 2]

1
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,␣
↪random_state=42)
svm = SVC(kernel='linear', C=1.0)
svm.fit(X_train, y_train)
y_pred = svm.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.4f}')
plt.figure(figsize=(8, 6))
X_train_2d = X_train[:, :2]
X_test_2d = X_test[:, :2]
svm_2d = SVC(kernel='linear', C=1.0)
svm_2d.fit(X_train_2d, y_train)
xx, yy = np.meshgrid(np.linspace(X_train_2d[:, 0].min() - 1, X_train_2d[:, 0]. ↪max() + 1,
100),
np.linspace(X_train_2d[:, 1].min() - 1, X_train_2d[:, 1].
↪max() + 1, 100))

Z = svm_2d.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.75)
plt.scatter(X_train_2d[:, 0], X_train_2d[:, 1], c=y_train, edgecolors='k',␣ ↪marker='o',
s=100, label='Train Data')
plt.scatter(X_test_2d[:, 0], X_test_2d[:, 1], c=y_test, edgecolors='r',␣ ↪marker='^',
s=100, label='Test Data')
plt.title('SVM Decision Boundary (2D)')
plt.xlabel('Feature 1 (Sepal Length)')
plt.ylabel('Feature 2 (Sepal Width)')
plt.legend()
plt.show()

Accuracy: 1.0000
2

[ ]: import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt

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

X = X[y != 2]
y = y[y != 2]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,␣


↪random_state=42)

def plot_svm_decision_boundary(C_values):
plt.figure(figsize=(12, 8))

3
accuracies = []

for C in C_values:
svm = SVC(kernel='linear', C=C)
svm.fit(X_train[:, :2], y_train)

y_pred = svm.predict(X_test[:, :2])


accuracy = accuracy_score(y_test, y_pred)
accuracies.append(accuracy)

xx, yy = np.meshgrid(np.linspace(X_train[:, 0].min() - 1, X_train[:, 0]. ↪max() + 1, 100),


np.linspace(X_train[:, 1].min() - 1, X_train[:, 1].
↪max() + 1, 100))

Z = svm.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

plt.subplot(2, 3, C_values.index(C) + 1)
plt.contourf(xx, yy, Z, alpha=0.75)
plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, edgecolors='k',␣ ↪marker='o',
s=100, label='Train Data')
plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, edgecolors='r',␣ ↪marker='^',
s=100, label='Test Data')
plt.title(f'SVM Decision Boundary for C={C}')
plt.xlabel('Feature 1 (Sepal Length)')
plt.ylabel('Feature 2 (Sepal Width)')
plt.legend()

plt.figure(figsize=(8, 6))
plt.plot(C_values, accuracies, marker='o', linestyle='-', color='b') plt.xscale('log')
plt.xlabel('Regularization Parameter (C)')
plt.ylabel('Test Accuracy')
plt.title('Effect of Regularization (C) on SVM Accuracy')
plt.grid(True)
plt.show()

C_values = [0.01, 0.1, 1, 10, 100]

plot_svm_decision_boundary(C_values)

5
6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy