Commit dc7d63a6 authored by Gavin Lee's avatar Gavin Lee
Browse files

add classification example

parent 569f2547
Pipeline #307960 passed with stage
in 4 minutes and 21 seconds
%% Cell type:code id:b8fa1017-812e-4a6b-9cf5-d69be1a718cb tags:
``` python
import numpy as np
```
%% Cell type:markdown id:e1a3af63-e023-4d31-b550-aede5d359569 tags:
# FROM
https://scikit-learn.org/stable/auto_examples/classification/plot_digits_classification.html#sphx-glr-auto-examples-classification-plot-digits-classification-py
%% Cell type:code id:1bc291cd-0192-4611-b77e-7e941238e7fc tags:
``` python
# Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org>
# License: BSD 3 clause
# Standard scientific Python imports
import matplotlib.pyplot as plt
# Import datasets, classifiers and performance metrics
from sklearn import datasets, svm, metrics
from sklearn.model_selection import train_test_split
```
%% Cell type:code id:c521b6ba-f363-4444-a929-915b3962ec9c tags:
``` python
digits = datasets.load_digits()
_, axes = plt.subplots(nrows=1, ncols=4, figsize=(10, 3))
for ax, image, label in zip(axes, digits.images, digits.target):
ax.set_axis_off()
ax.imshow(image, cmap=plt.cm.gray_r, interpolation="nearest")
ax.set_title("Training: %i" % label)
```
%%%% Output: display_data
![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjwAAACXCAYAAAARS4GeAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAALBUlEQVR4nO3dX2yd510H8O+vi8ooW2tnE0wU1sSdBAK0mqZTmZBQqjnSuJgcMRJNDDRXmhJxA5G4cG5gjsZQghByxYYWEGoZMFgjIJ2QCmq0uqMXgGLhTipsF2lamNikQp1uHfsjwcvFcUbUpmnzvufkxE8+HymSz+n5vs9j95dzvnlfH7u6rgsAQMtumvYGAAAmTeEBAJqn8AAAzVN4AIDmKTwAQPMUHgCgeU0Xnqp6tKo+NO7HcmMxRwxlhhgHczRMXW8/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fn++d3djYGLT2kPzKykrv7JD527VrV+9sMuzvTatmZ2cH5Q8dOjSmnVydgwcP9s6ePHlyjDsZD2d4AIDmKTwAQPMUHgCgeQoPANA8hQcAaJ7CAwA0T+EBAJqn8AAAzVN4AIDmKTwAQPMUHgCgeQoPANA8hQcAaJ7CAwA0r7qum/YeAAAmyhkeAKB5Cg8A0DyFBwBonsIDADRP4QEAmqfwAADN+z+hHt0iyNm/ygAAAABJRU5ErkJggg==)
%% Cell type:code id:95670bd9-85b5-4f46-a8bb-68b9d8a7f210 tags:
``` python
# flatten the images
n_samples = len(digits.images)
data = digits.images.reshape((n_samples, -1))
# Create a classifier: a support vector classifier
clf = svm.SVC(gamma=0.001)
# Split data into 50% train and 50% test subsets
X_train, X_test, y_train, y_test = train_test_split(
data, digits.target, test_size=0.5, shuffle=False
)
# Learn the digits on the train subset
clf.fit(X_train, y_train)
# Predict the value of the digit on the test subset
predicted = clf.predict(X_test)
```
%% Cell type:code id:b7e1ce4d-5b4b-43ba-97e4-f2c9e61339b4 tags:
``` python
_, axes = plt.subplots(nrows=1, ncols=4, figsize=(10, 3))
for ax, image, prediction in zip(axes, X_test, predicted):
ax.set_axis_off()
image = image.reshape(8, 8)
ax.imshow(image, cmap=plt.cm.gray_r, interpolation="nearest")
ax.set_title(f"Prediction: {prediction}")
```
%%%% Output: display_data
![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjwAAACXCAYAAAARS4GeAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAL5UlEQVR4nO3dX4xjZR3G8eeHw4KyOl2MUXZdpojBCAY6iiJR3Ak3JgiZBoKaqNnWhLiJ/8Ybw4W4TVj8d+NIEDBEqdEEkEg6xERjEKYRIYiy7YUxBOJ0ZBd3g0AnrhB18fXiHLSsM7Oz73tKZ37z/SSTzLR9znnb/v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yg8AADAPQoPAABwj8IDAADco/AAAAD3KDwAAMA9Cg8AAHCPwgMAANyj8AAAAPcoPAAAwD0KDwAAcI/CAwAA3KPwAAAA9yg8AADAvbFRL2AYut3uqJdwwvr9/qiXsC6VSqXo7O7du5P23Wg0kvKxxsfHo7NTU1PFLWQdGdUc9Hq96Kwklcvl6Gy73Y7OptznSqUSnfVsZmYmOlutVpP23el0orO1Wi06u7S0FJ2dmJiIzg4LZ3gAAIB7FB4AAOAehQcAALhH4QEAAO5ReAAAgHsUHgAA4B6FBwAAuEfhAQAA7lF4AACAexQeAADgHoUHAAC4R+EBAADuUXgAAIB7FB4AAOAehQcAALhnIYTVrl/1yvWqWq1GZ3u9XnS2VCpFZ1utVnQ2dd+SLGnnxzeSOep0Okn5lDlaXFyMzt5+++3R2VqtFp0twDDnaEMei5rNZnS2Xq9HZxcWFqKz5XI5OlsAl8eiVJVKJTrb7Xajs3v37o3ONhqN6GwBlp0jzvAAAAD3KDwAAMA9Cg8AAHCPwgMAANyj8AAAAPcoPAAAwD0KDwAAcI/CAwAA3KPwAAAA9yg8AADAPQoPAABwj8IDAADco/AAAAD3KDwAAMA9CyGMeg0AAABDxRkeAADgHoUHAAC4R+EBAADuUXgAAIB7FB4AAOAehQcAALj3H7xpoGkHOBS+AAAAAElFTkSuQmCC)
%% Cell type:code id:13fe2601-b30e-4115-b783-7aef14622d6b tags:
``` python
from sklearn.metrics import accuracy_score
```
%% Cell type:code id:5170f2c1-d181-4b17-9d9f-cbbf25edfa3d tags:
``` python
from mlsconverters import export
```
%% Cell type:code id:21b21aec-5b1c-4b5c-8a2f-13ad9ff62ed0 tags:
``` python
## MLS to schema
acc = accuracy_score(y_test, predicted)
export(clf, evaluation_measure=(accuracy_score, acc))
```
%% Cell type:code id:196c78ba-3f86-429d-914e-19be240023c6 tags:
``` python
!cd ../; renku mls leaderboard
```
%%%% Output: stream
Error: Project version is outdated and a migration is required.
Run `renku migrate` command to fix the issue.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment