From: Jérôme Benoit Date: Mon, 10 Dec 2018 21:05:15 +0000 (+0100) Subject: Add TP1prog4.py. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=TP_AA.git Add TP1prog4.py. Signed-off-by: Jérôme Benoit --- diff --git a/TP1/exo2/TP1prog4.py b/TP1/exo2/TP1prog4.py new file mode 100755 index 0000000..93dca63 --- /dev/null +++ b/TP1/exo2/TP1prog4.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +# -*- coding: utf-8 -*- +from sklearn.cross_validation import KFold +from sklearn import neighbors +from sklearn.datasets import load_iris + +irisData = load_iris() +X = irisData.data +Y = irisData.target + +kf = KFold(len(X), n_folds=10, shuffle=True) + +scores = [] +for k in range(1, 30): + score = 0 + clf = neighbors.KNeighborsClassifier(k) + for learn, test in kf: + X_train = [X[i] for i in learn] + Y_train = [Y[i] for i in learn] + clf.fit(X_train, Y_train) + X_test = [X[i] for i in test] + Y_test = [Y[i] for i in test] + score = score + clf.score(X_test, Y_test) + scores.append(score) + +print(scores) +print("meilleure valeur pour k : ", scores.index(max(scores)) + 1)