<jupyter_start><jupyter_text>An introduction to machine learning with scikit-learn¶see: https://sklearn.org/tutorial/basic/tutorial.html datasets in sklearn:scikit-learn comes with a few standard datasets, for instance the iris and digits datasets for classification and the boston house prices dataset for regression.<jupyter_code>from sklearn import datasets
iris = datasets.load_iris()
digits = datasets.load_digits()

digits.keys()

digits.target_names
print(digits.target)
digits.images[0]
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt

plt.imshow(digits.images[0], cmap=matplotlib.cm.binary,
           interpolation="nearest")
plt.axis("off")
plt.show()<jupyter_output><empty_output><jupyter_text>Learning and predictingIn scikit-learn, an estimator for classification is a Python object that implements the methods 'fit(X, y)' and 'predict(T)'.<jupyter_code>from sklearn import svm
clf = svm.SVC(gamma=0.001, C=100.)<jupyter_output><empty_output><jupyter_text>We call our estimator instance clf, as it is a classifier. It now must be fitted to the model, that is, it must learn from the model. This is done by passing our training set to the fit method.<jupyter_code>clf.fit(digits.data[:-1], digits.target[:-1])
clf.predict(digits.data[-1:])<jupyter_output><empty_output><jupyter_text>for more info on how to recognize hand written digits see: https://sklearn.org/auto_examples/classification/plot_digits_classification.htmlsphx-glr-auto-examples-classification-plot-digits-classification-py  Model persistenceIt is possible to save a model in the scikit by using Python’s built-in persistence model, namely pickle:<jupyter_code>clf = svm.SVC(gamma='auto')
iris = datasets.load_iris()
X, y = iris.data, iris.target
clf.fit(X, y)
import pickle
s = pickle.dumps(clf)
clf2 = pickle.loads(s)
clf2.predict(X[0:1])

y[0]<jupyter_output><empty_output><jupyter_text>In the specific case of the scikit, it may be more interesting to use joblib’s replacement of pickle (joblib.dump & joblib.load), which is more efficient on big data, but can only pickle to the disk and not to a string:<jupyter_code>from sklearn.externals import joblib
joblib.dump(clf, 'filename.pkl') 
clf = joblib.load('filename.pkl')<jupyter_output><empty_output><jupyter_text>Conventionsscikit-learn estimators follow certain rules to make their behavior more predictive. Type castingUnless otherwise specified, input will be cast to float64:<jupyter_code>import numpy as np
from sklearn import random_projection

rng = np.random.RandomState(0)
X = rng.rand(10, 2000)
X = np.array(X, dtype='float32')
X.dtype

transformer = random_projection.GaussianRandomProjection()
X_new = transformer.fit_transform(X)
X_new.dtype
from sklearn import datasets
from sklearn.svm import SVC
iris = datasets.load_iris()
clf = SVC(gamma='auto')
clf.fit(iris.data, iris.target)
list(clf.predict(iris.data[:3]))
clf.fit(iris.data, iris.target_names[iris.target])   

list(clf.predict(iris.data[:3]))<jupyter_output><empty_output><jupyter_text>Here, the first predict() returns an integer array, since iris.target (an integer array) was used in fit. The second predict() returns a string array, since iris.target_names was for fitting.  Refitting and updating parametersHyper-parameters of an estimator can be updated after it has been constructed via the sklearn.pipeline.Pipeline.set_params method. Calling fit() more than once will overwrite what was learned by any previous fit():<jupyter_code>rng = np.random.RandomState(0)
X = rng.rand(100, 10)
y = rng.binomial(1, 0.5, 100)
X_test = rng.rand(5, 10)
clf = SVC()
clf.set_params(kernel='linear').fit(X, y)  

clf.predict(X_test)
clf.set_params(gamma='auto', kernel='rbf').fit(X, y)  
clf.predict(X_test)<jupyter_output><empty_output><jupyter_text>Multiclass vs. multilabel fitting<jupyter_code>from sklearn.svm import SVC
from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import LabelBinarizer

X = [[1, 2], [2, 4], [4, 5], [3, 2], [3, 1]]
y = [0, 0, 1, 1, 2]

classif = OneVsRestClassifier(estimator=SVC(gamma='auto', random_state=0))
classif.fit(X, y).predict(X)

y = LabelBinarizer().fit_transform(y)
classif.fit(X, y).predict(X)
from sklearn.preprocessing import MultiLabelBinarizer
y = [[0, 1], [0, 2], [1, 3], [0, 2, 3], [2, 4]]
y = MultiLabelBinarizer().fit_transform(y)
classif.fit(X, y).predict(X)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup