0% found this document useful (0 votes)
14 views3 pages

Experiment 7

The document outlines a Python script for training a Support Vector Machine (SVM) model using a dataset loaded from a CSV file. It includes steps for data preprocessing, splitting the dataset into training and testing sets, training the model, making predictions, and evaluating performance with a confusion matrix and classification report. The model achieved an accuracy of 100% on the test set.

Uploaded by

singhsatwick0
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)
14 views3 pages

Experiment 7

The document outlines a Python script for training a Support Vector Machine (SVM) model using a dataset loaded from a CSV file. It includes steps for data preprocessing, splitting the dataset into training and testing sets, training the model, making predictions, and evaluating performance with a confusion matrix and classification report. The model achieved an accuracy of 100% on the test set.

Uploaded by

singhsatwick0
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/ 3

# Import necessary libraries

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import classification_report,
confusion_matrix, accuracy_score
import matplotlib.pyplot as plt

# Step 1: Load the dataset from CSV


data = pd.read_csv(r"D:\Programming\Machine
Learning\Experiment 7\Sample_data.csv") # Replace
with your actual file name

# Step 2: Explore and preprocess the data


print("Columns:", data.columns)
print("First 5 rows:\n", data.head())

# Separate features (X) and target (y)


# Assuming last column is target; modify accordingly
if different
X = data.iloc[:, :-1]
y = data.iloc[:, -1]

# Step 3: Split into training and testing sets (80-20


split)
X_train, X_test, y_train, y_test =
train_test_split(X, y, test_size=0.2,
random_state=42)
# Step 4: Create and train SVM model
svm_model = SVC(kernel='linear') # Try 'rbf',
'poly', etc. for different kernels
svm_model.fit(X_train, y_train)

# Step 5: Predict on test set


y_pred = svm_model.predict(X_test)

# Step 6: Evaluate the model


print("\nConfusion Matrix:\n",
confusion_matrix(y_test, y_pred))
print("\nClassification Report:\n",
classification_report(y_test, y_pred))
print(f"\nAccuracy: {accuracy_score(y_test,
y_pred):.2f}")

# Optional: Visualize if 2D data


if X.shape[1] == 2:
plt.figure(figsize=(8, 6))
plt.scatter(X_test.iloc[:, 0], X_test.iloc[:, 1],
c=y_pred, cmap='coolwarm', edgecolors='k')
plt.title('SVM Classification Result')
plt.xlabel(X.columns[0])
plt.ylabel(X.columns[1])
plt.grid(True)
plt.show()
OUTPUT:-
Columns: Index(['Feature1', 'Feature2', 'Label'], dtype='object')
First 5 rows:
Feature1 Feature2 Label
0 2.5 1.7 0
1 3.0 2.0 0
2 1.5 1.2 0
3 3.1 1.8 0
4 2.9 1.6 0

Confusion Matrix:

[[2 0]

[0 2]]

Classification Report:

precision recall f1-score support

0 1.00 1.00 1.00 2

1 1.00 1.00 1.00 2

accuracy 1.00 4

macro avg 1.00 1.00 1.00 4

weighted avg 1.00 1.00 1.00 4

Accuracy: 1.00

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