import pandas as pd 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, confusion_matrix, classification_report import matplotlib.pyplot as plt import seaborn as sns # Step 1.1: Load the Iris dataset iris = datasets.load_iris() df = pd.DataFrame(data=iris.data, columns=iris.feature_names) df['target'] = iris.target df['species'] = df['target'].map({0: 'setosa', 1: 'versicolor', 2: 'virginica'}) print("Original Iris DataFrame:\n", df.head()) # Step 1.2: Select two classes ('setosa' and 'versicolor') df_binary = df[df['species'].isin(['setosa', 'versicolor'])] X = df_binary.iloc[:, :-2].values # Features y = df_binary['target'].values # Target labels # Step 1.3: Split the dataset into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # Step 1.4: Load the SVC Model svc_model = SVC(kernel='linear', random_state=42) svc_model.fit(X_train, y_train) # Step 1.5: Perform prediction y_pred = svc_model.predict(X_test) # Calculate accuracy accuracy = accuracy_score(y_test, y_pred) print("\nAccuracy of SVC Model:", accuracy) # Confusion Matrix conf_matrix = confusion_matrix(y_test, y_pred) print("\nConfusion Matrix:\n", conf_matrix) # Classification Report print("\nClassification Report:\n", classification_report(y_test, y_pred, target_names=['setosa', 'versicolor'])) # Plot the Confusion Matrix plt.figure(figsize=(6, 4)) sns.heatmap(conf_matrix, annot=True, cmap='Blues', fmt='d', xticklabels=['setosa', 'versicolor'], yticklabels=['setosa', 'versicolor']) plt.title('Confusion Matrix') plt.xlabel('Predicted Labels') plt.ylabel('True Labels') plt.show()