Tutorial 3: Creating an SVC Model with Scikit-Learn

Posted by


Welcome to Scikit-Learn Tutorial 3! In this tutorial, we will learn how to build a Support Vector Classifier (SVC) model using the scikit-learn library in Python. SVC is a powerful classification algorithm that works well for both linear and non-linear classification problems.

Before diving into building the SVC model, make sure you have scikit-learn installed in your Python environment. You can install it using pip:

pip install scikit-learn

Now, let’s get started with building the SVC model.

Step 1: Import Necessary Libraries
First, we need to import the necessary libraries. In this tutorial, we will also use numpy for numerical operations and matplotlib for visualization.

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

Step 2: Load Dataset
We will use the famous Iris dataset for this tutorial. The Iris dataset contains 150 samples of iris flowers, each with 4 features (sepal length, sepal width, petal length, petal width) and a target variable representing the iris species.

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

Step 3: Split Dataset
Next, we need to split the dataset into training and testing sets. We will use 80% of the data for training and 20% for testing.

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

Step 4: Build SVC Model
Now, let’s build the SVC model using scikit-learn. We will create an instance of the SVC class and fit the model to the training data.

svc = SVC(kernel='linear')
svc.fit(X_train, y_train)

Step 5: Make Predictions
Once the model is trained, we can make predictions on the test data.

y_pred = svc.predict(X_test)

Step 6: Evaluate Model
Finally, we will evaluate the model’s performance by calculating the accuracy score.

accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy}')

Step 7: Visualize Decision Boundary (Optional)
To visually inspect the Decision Boundary created by the SVC model, we can plot the decision boundaries along with the data points.

# Plot the decision boundary
plt.scatter(X[:, 0], X[:, 1], c=y, cmap='viridis')
ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()

xx = np.linspace(xlim[0], xlim[1], 30)
yy = np.linspace(ylim[0], ylim[1], 30)
YY, XX = np.meshgrid(yy, xx)
xy = np.vstack([XX.ravel(), YY.ravel()]).T
Z = svc.decision_function(xy).reshape(XX.shape)

ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,
           linestyles=['--', '-', '--'])
plt.show()

That’s it! You have successfully built an SVC model using scikit-learn. SVC is a powerful classification algorithm that can be applied to a wide range of classification problems. Experiment with different parameters and kernels to see how they affect the model’s performance.

I hope this tutorial was helpful. If you have any questions or feedback, feel free to leave a comment. Happy coding!

0 0 votes
Article Rating
12 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@yunicoardianpradana7288
2 months ago

why don't you use kfold instead of train test split?

@TAASCLUB
2 months ago

TypeError: float() argument must be a string or a number, not 'StandardScaler' i write same code like your but i am getting this error on there X_test = sc.transform(X_test)

will you help me

@kaiyurmaheshwari622
2 months ago

🤮🤮🤮🤮🤮 is it a good explanation???????? Have you ever seen your vedio after recording…worst worst explanation

@kaiyurmaheshwari622
2 months ago

Why the hell you can't use an ide like vscode ….. What is this running in cmd …!!!!!!!!!!?"(_(₹

@kamleshverma9724
2 months ago

Thanks!

@ninaddhumak5281
2 months ago

Here we just saw how accurate a model can be

…right ??

@sushanejoshi4512
2 months ago

its asking me for self parameter missing, what to do
?

@patrickmueller9658
2 months ago

Your tutorials are great – thank you! Subscribed and liked!

@mspmanjeet
2 months ago

Could you pls explain fit.transform vs transform in more details.

@nipnix360
2 months ago

when is gradient decent coming

@aravindrooswelt4734
2 months ago

next?

@DarshanSenTheComposer
2 months ago

3:20 – I think u forgot to put '()' after 'describe'