TypeError only integer scalar arrays can be converted to a scalar index

+1 vote
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.ml.KNearest_create()
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()
Dec 18, 2019 in Python by sandeep
• 130 points
• 8,410 views

Which line is giving you this error? 

My guess is 

red=trainData[responses.ravel()==0]

I don't think ravel() can be used here

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Python

0 votes
0 answers
0 votes
0 answers

TypeError: only size-1 arrays can be converted to Python scalars

This is my code for key in bboxes: bbox ...READ MORE

May 19, 2020 in Python by Rumia
• 120 points

edited May 19, 2020 by Gitika • 2,698 views
0 votes
1 answer

Pyplot error "only size-1 arrays can be converted to Python scalars"

You can plot the chart by taking ...READ MORE

answered Jan 28, 2019 in Python by Omkar
• 69,180 points
• 10,672 views
0 votes
0 answers

Python error "only size-1 arrays can be converted to Python scalars"

I'm trying to plot the exponential and ...READ MORE

Jul 9, 2019 in Python by JTM
• 2,723 views
0 votes
0 answers

Pyplot error "only size-1 arrays can be converted to Python scalars"

I am getting the following error while ...READ MORE

Nov 28, 2019 in Python by Sumit
• 130 points
• 4,544 views
0 votes
1 answer

How can I test if a string can be converted to another type dynamically?

You can use isinstance() function to check ...READ MORE

answered Nov 15, 2020 in Python by Gitika
• 65,730 points
• 1,330 views
0 votes
1 answer
0 votes
3 answers

TypeError: Index(...) must be called with a collection of some kind, 'Email' was passed

It is not working because your code ...READ MORE

answered May 13, 2020 in Python by anonymous

edited May 13, 2020 by Gitika • 60,954 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP