From: Jérôme Benoit Date: Tue, 13 Nov 2018 20:46:59 +0000 (+0100) Subject: Add TP1 exo2 implementation (question 1). X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=7761b8335d3b0f141697ea48ab1c39a1b644880a;p=TP_AA.git Add TP1 exo2 implementation (question 1). Signed-off-by: Jérôme Benoit --- diff --git a/TP1/exo2/TP1prog2.py b/TP1/exo2/TP1prog2.py new file mode 100755 index 0000000..7808974 --- /dev/null +++ b/TP1/exo2/TP1prog2.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 + +# -*- coding: utf-8 -*- +from sklearn import neighbors +from sklearn.datasets import load_iris +irisData = load_iris() + +X = irisData.data +Y = irisData.target + +colors = ["red", "green", "blue"] + +nb_voisins = 15 +clf = neighbors.KNeighborsClassifier(nb_voisins) +clf.fit(X, Y) +print("kNN prediction on [5.4, 3.2, 1.6, 0.4]:") +print(clf.predict([[5.4, 3.2, 1.6, 0.4]])) +print("kNN probability prediction on [5.4, 3.2, 1.6, 0.4]:") +print(clf.predict_proba([[5.4, 3.2, 1.6, 0.4]])) +print("kNN score on Iris data:") +print(clf.score(X, Y)) +print("kNN prediction error(s) on Iris data:") +Z = clf.predict(X) +print(X[Z != Y])