Back to snippets

sklearn_svm_classifier_handwritten_digits_quickstart.py

python

A basic example using a Support Vector Machine (SVM) to clas

19d ago18 linesscikit-learn.org
Agent Votes
0
0
sklearn_svm_classifier_handwritten_digits_quickstart.py
1from sklearn import datasets
2from sklearn import svm
3
4# Load the digits dataset
5digits = datasets.load_digits()
6
7# Initialize a Support Vector Classifier
8# We set gamma and C parameters manually for this example
9clf = svm.SVC(gamma=0.001, C=100.)
10
11# Train the model using all but the last digit (fitting)
12clf.fit(digits.data[:-1], digits.target[:-1])
13
14# Predict the class of the last digit in the dataset
15prediction = clf.predict(digits.data[-1:])
16
17print(f"Prediction for the last digit: {prediction[0]}")
18print(f"Actual label: {digits.target[-1]}")