From: Jérôme Benoit Date: Tue, 13 Nov 2018 21:10:16 +0000 (+0100) Subject: Add for real the previous commit needed file. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=4e5e7ce89fa5c28e59588c06956be7517d232a11;p=TP_AA.git Add for real the previous commit needed file. Signed-off-by: Jérôme Benoit --- diff --git a/TP1/exo2/TP1prog3.py b/TP1/exo2/TP1prog3.py new file mode 100755 index 0000000..4daa73f --- /dev/null +++ b/TP1/exo2/TP1prog3.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +# -*- coding: utf-8 -*- +import random +from sklearn import neighbors +from sklearn.cross_validation import train_test_split +from sklearn.datasets import load_iris + +irisData = load_iris() + +X = irisData.data +Y = irisData.target + +# print(help(train_test_split)) +X_train, X_test, Y_train, Y_test = train_test_split( + X, Y, test_size=0.3, random_state=random.seed()) +# print(len(X_train)) +# print(len(X_test)) +# print(len(X_train[Y_train == 0])) +# print(len(X_train[Y_train == 1])) +# print(len(X_train[Y_train == 2])) + +nb_voisins = 15 +clf = neighbors.KNeighborsClassifier(nb_voisins) +clf.fit(X_train, Y_train) +# 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 test data:") +print(clf.score(X_test, Y_test)) +print("kNN prediction error(s) on Iris test data:") +Z = clf.predict(X_test) +print(X_test[Z != Y_test])