From 54eb4963833e5baad01ef07110cab68647f38fcd Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 10 Dec 2018 22:05:15 +0100 Subject: [PATCH] Add TP1prog4.py. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP1/exo2/TP1prog4.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 TP1/exo2/TP1prog4.py 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) -- 2.34.1