04 | matplotlib.use( 'Agg' ) |
06 | import matplotlib.pyplot as plt |
07 | from sklearn.neighbors import KNeighborsClassifier |
09 | x = [ 4 , 5 , 10 , 4 , 3 , 11 , 14 , 8 , 10 , 12 ] |
10 | y = [ 21 , 19 , 24 , 17 , 16 , 25 , 24 , 22 , 21 , 21 ] |
11 | classes = [ 0 , 0 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 1 ] |
13 | data = list ( zip ( x, y ) ) |
14 | knn = KNeighborsClassifier( n_neighbors = 5 ) |
16 | knn.fit( data, classes ) |
20 | new_point = [( new_x, new_y )] |
22 | prediction = knn.predict( new_point ) |
24 | plt.scatter( x + [new_x], y + [new_y], c = classes + [prediction[ 0 ]] ) |
25 | plt.text( x = new_x - 1.7 , y = new_y - 0.7 , s = f "new point, class: {prediction[0]}" ) |
29 | plt.savefig( sys.stdout. buffer ) |
|
|