X-Git-Url: https://git.piment-noir.org/?p=TP_AA.git;a=blobdiff_plain;f=TP1%2Fexo2%2FTP1prog4.py;fp=TP1%2Fexo2%2FTP1prog4.py;h=93dca63cc57d5e970f07bc02a16737b5858621a8;hp=0000000000000000000000000000000000000000;hb=54eb4963833e5baad01ef07110cab68647f38fcd;hpb=9fa4e985cd669b39149628215e437b72a66e7b49 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)