Basic types of classifiers:
Types of learning:
# scikit-learn
%pylab inline --no-import-all
from sklearn import datasets
import numpy as np
import sklearn.model_selection
Populating the interactive namespace from numpy and matplotlib
iris = datasets.load_iris()
# cílové třídy
# rozměry dat
print("data ", iris.data.shape)
print(iris.data[-10:,:])
print("")
print("target", iris.target.shape)
print(np.unique(iris.target))
print(iris.target[-10:])
data (150, 4) [[6.7 3.1 5.6 2.4] [6.9 3.1 5.1 2.3] [5.8 2.7 5.1 1.9] [6.8 3.2 5.9 2.3] [6.7 3.3 5.7 2.5] [6.7 3. 5.2 2.3] [6.3 2.5 5. 1.9] [6.5 3. 5.2 2. ] [6.2 3.4 5.4 2.3] [5.9 3. 5.1 1.8]] target (150,) [0 1 2] [2 2 2 2 2 2 2 2 2 2]
from sklearn import neighbors
knn = neighbors.KNeighborsClassifier()
knn.fit(iris.data, iris.target)
#KNeighborsClassifier(...)
predikce = knn.predict([[0.1, 0.2, 0.3, 0.4]])
print(predikce)
#array([0])
[0]
perm = np.random.permutation(iris.target.size)
iris.data = iris.data[perm]
iris.target = iris.target[perm]
train_data = iris.data[:100]
train_target = iris.target[:100]
test_data = iris.data[100:]
test_target = iris.target[100:]
knn.fit(train_data, train_target)
knn.score(test_data, test_target)
0.94
import sklearn.naive_bayes
gnb = sklearn.naive_bayes.GaussianNB()
gnb.fit(train_data, train_target)
y_pred = gnb.predict(test_data)
print("Number of mislabeled points : %d" % (test_target != y_pred).sum())
Number of mislabeled points : 5
from sklearn import svm
svc = svm.SVC()
svc.fit(train_data, train_target)
y_pred = svc.predict(test_data)
print("Number of mislabeled points : %d" % (test_target != y_pred).sum())
Number of mislabeled points : 3
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/01.jpg
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/02.jpg
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/03.jpg
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/04.jpg
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/05.jpg
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/06.jpg
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/07.jpg
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/08.jpg
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/09.jpg
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/10.jpg
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/11.jpg
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/12.jpg
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/13.jpg
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/14.jpg
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/15.jpg
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/16.jpg
https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/17.jpg
import scipy
import urllib
import skimage
import skimage.color
import skimage.measure
import skimage.io
from sklearn import svm
# URL = "http://uc452cam01-kky.fav.zcu.cz/snapshot.jpg"
URL = "https://raw.githubusercontent.com/mjirik/ZDO/master/objekty/ctverce_hvezdy_kolecka.jpg"
img = skimage.io.imread(URL, as_gray=True)
plt.imshow(img)
# doporučený klasifikátor ...
# pozor na labeling a "+1 problém"
<matplotlib.image.AxesImage at 0x208dcbaa208>