Last active
March 9, 2021 15:34
-
-
Save gmyrianthous/247043a67773ecf075ea9b0d0d4a368a to your computer and use it in GitHub Desktop.
fit_example.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| scikit-learn example to fit a SVC model for recognizing images of hand-written digits. | |
| The images attribute of the dataset stores 8x8 arrays of grayscale values for each image. | |
| We will use these arrays to visualize the first 4 images. To apply a classifier on this data, | |
| we need to flatten the images, turning each 2-D array of grayscale values from | |
| shape (8, 8) into shape (64,). | |
| Reference: https://scikit-learn.org/stable/auto_examples/classification/plot_digits_classification.html | |
| """ | |
| from sklearn import datasets, svm | |
| from sklearn.model_selection import train_test_split | |
| # Load data | |
| digits_data = datasets.load_digits() | |
| # Flatten images (see the comment on the top of the file to undestand why) | |
| data_flattened = digits_data.images.reshape((len(digits_data.images), -1)) | |
| # Split data into training and testing sets (ratio 80:20) | |
| X_train, X_test, y_train, y_test = train_test_split( | |
| data_flattened, | |
| digits_data.target, | |
| test_size=0.2, | |
| shuffle=False | |
| ) | |
| # Create a Support Vector Classifier | |
| clf = svm.SVC(gamma=0.001) | |
| # Fit the classifier to the input training data | |
| clf.fit(X_train, y_train) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment