Commit | Line | Data |
---|---|---|
54eb4963 JB |
1 | #!/usr/bin/env python3 |
2 | ||
3 | # -*- coding: utf-8 -*- | |
4 | from sklearn.cross_validation import KFold | |
5 | from sklearn import neighbors | |
6 | from sklearn.datasets import load_iris | |
7 | ||
8 | irisData = load_iris() | |
9 | X = irisData.data | |
10 | Y = irisData.target | |
11 | ||
12 | kf = KFold(len(X), n_folds=10, shuffle=True) | |
13 | ||
14 | scores = [] | |
15 | for 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 | ||
27 | print(scores) | |
28 | print("meilleure valeur pour k : ", scores.index(max(scores)) + 1) |