0

In this tutorial, how can I plot Data x and y coordinates without using random numbers? Otherwise, how do I change the color of the shape?

import cv2
import numpy as np
import matplotlib.pyplot as plt

trainData = np.random.randint(0,100,(25,2)).astype(np.float32)

responses = np.random.randint(0,2,(25,1)).astype(np.float32)

red = trainData[responses.ravel()==0]
plt.scatter(red[:,0],red[:,1],80,'r','^')

blue = trainData[responses.ravel()==1]
plt.scatter(blue[:,0],blue[:,1],80,'b','s')

newcomer = np.random.randint(0,100,(1,2)).astype(np.float32)
plt.scatter(newcomer[:,0],newcomer[:,1],80,'g','o')

knn = cv2.KNearest()
knn.train(trainData,responses)
ret, results, neighbours ,dist = knn.find_nearest(newcomer, 3)

print "result: ", results,"\n"
print "neighbours: ", neighbours,"\n"
print "distance: ", dist

plt.show()
Cakkaphong Khangsri
  • 154
  • 1
  • 2
  • 10
  • 1
    Do you have a text file or something you want to use instead of the random numbers? And what format is it? Also, can you clarify what specifically you want to change the color of, maybe with a screenshot? (Please edit the question, don't respond to me in a comment) – Max von Hippel Jun 17 '18 at 16:19
  • 1
    The color and shape is defined by `'r','^'` `'b','s'` and `'g','o'` and you can find more colors in matplotlib (r is red, b is blue, g is green). As for changing to non random numbers... you can always do `np.array([1., 2., 3., 4., 5.])` or any other numbers you want. You can also load a text file... – api55 Jun 17 '18 at 17:01

0 Answers0