gradio/Test Sklearn.ipynb
2019-04-09 21:06:02 -07:00

7.8 KiB

In [1]:
%load_ext autoreload
%autoreload 2

from sklearn import datasets, svm
import gradio
import matplotlib.pyplot as plt

# The digits dataset
digits = datasets.load_digits()
In [2]:
# To apply a classifier on this data, we need to flatten the image, to
# turn the data in a (samples, feature) matrix:
n_samples = len(digits.images)
data = digits.images.reshape((n_samples, -1))

# Create a classifier: a support vector classifier
classifier = svm.SVC(gamma=0.001)

# We learn the digits on the first half of the digits
classifier.fit(data[:n_samples // 2], digits.target[:n_samples // 2])
Out[2]:
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma=0.001, kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)
In [3]:
data.max()
Out[3]:
16.0
In [4]:
images_and_labels = list(zip(digits.images, digits.target))
for index, (image, label) in enumerate(images_and_labels[:4]):
    plt.subplot(2, 4, index + 1)
    plt.axis('off')
    plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
    plt.title('Training: %i' % label)
No description has been provided for this image
In [5]:
classifier.predict()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-043f82e0ca32> in <module>
----> 1 classifier.predict()

TypeError: predict() missing 1 required positional argument: 'X'
In [ ]:
expected = digits.target[n_samples // 2:]
predicted = classifier.predict(data[n_samples // 2:])
In [ ]:
inp = gradio.inputs.Sketchpad(shape=(8, 8), flatten=True, scale=16/255, invert_colors=False)
io = gradio.Interface(inputs=inp, outputs="label", model_type="sklearn", model=classifier)
In [ ]:
io.launch()
In [ ]: