import time import requests import numpy as np import matplotlib.pyplot as plt import pandas as pd from sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import train_test_split GET = "https://pydefis.callicode.fr/defis/MachineLearning/intern/C0MfHAoFXBILF1IiEgUCC0BVX14VE1FfTU5RVV9DAgQAUVdMaWtqGkJRXUBaWR4QHABGT0BEWDEeEABqXEEoKEBVBx4ABUVNU0ZdQgAFbBtQUy5fTTdpHQUGXlJERkFMBwQfD0dULl9NX28cEWwrTEJHQ10ABR0XXVZCXV5bB20dF0Q8X1M2NwMBABlGU0ZfTUEGBh8PR1QuX01ZbxwRbCtMQkBDXwUFHRdIVl1BWDEeEANqXEEoKEBaHAUdF0NUXUVfWW8cEQQtPA4%3D/getpoint" GET2 = "https://pydefis.callicode.fr/defis/MachineLearning/intern/C0MfHAoFXBILF1IiEgUCC0BVX14VE1FfTU5RVV9DAgQAUVdMaWtqGkJRXUBaWR4QHABGT0BEWDEeEABqXEEoKEBVBx4ABUVNU0ZdQgAFbBtQUy5fTTdpHQUGXlJERkFMBwQfD0dULl9NX28cEWwrTEJHQ10ABR0XXVZCXV5bB20dF0Q8X1M2NwMBABlGU0ZfTUEGBh8PR1QuX01ZbxwRbCtMQkBDXwUFHRdIVl1BWDEeEANqXEEoKEBaHAUdF0NUXUVfWW8cEQQtPA4%3D/reponse/" solution = 999 while solution > 0: dataset = pd.read_csv("MachineLearning_data.txt", names=['a', 'b', 'c']) X = dataset.iloc[:, :-1].values y = dataset.iloc[:, 2].values X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.999) classifier = KNeighborsClassifier(n_neighbors=5) classifier.fit(X_train, y_train) # y_pred = classifier.predict(X_test) res = requests.get(GET) a,b = [float(v) for v in res.text.replace("[", "").replace("]", "").split(",")] X = [[a,b], ] y = classifier.predict(X) color = y[0] print(a, b, color, ": ", end="") res = requests.get(GET2 + str(color)) solution = int(res.text.strip()) if solution == color: print("OK") else: print("NOK ({})".format(solution)) datafile = open("MachineLearning_data.txt", "a") datafile.write("{},{},{}\n".format(a,b,solution)) datafile.close time.sleep(1)