Add TP1prog4.py.
[TP_AA.git] / TP1 / exo2 / TP1prog4.py
CommitLineData
54eb4963
JB
1#!/usr/bin/env python3
2
3# -*- coding: utf-8 -*-
4from sklearn.cross_validation import KFold
5from sklearn import neighbors
6from sklearn.datasets import load_iris
7
8irisData = load_iris()
9X = irisData.data
10Y = irisData.target
11
12kf = KFold(len(X), n_folds=10, shuffle=True)
13
14scores = []
15for k in range(1, 30):
16 score = 0
17 clf = neighbors.KNeighborsClassifier(k)
18 for learn, test in kf:
19 X_train = [X[i] for i in learn]
20 Y_train = [Y[i] for i in learn]
21 clf.fit(X_train, Y_train)
22 X_test = [X[i] for i in test]
23 Y_test = [Y[i] for i in test]
24 score = score + clf.score(X_test, Y_test)
25 scores.append(score)
26
27print(scores)
28print("meilleure valeur pour k : ", scores.index(max(scores)) + 1)