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
6,288 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

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 579 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,110 points
8,465 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
982 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
2,776 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,870 points
45 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 12, 2020 in Python by anonymous

edited May 13, 2020 by Gitika 26,771 views
0 votes
1 answer

How can I write nested dictionaries to a csv file?

You can use the pandas library to ...READ MORE

answered Apr 17, 2018 in Python by anonymous
484 views
+1 vote
1 answer

How to append an integer to a string?

You can not directly concatenate two different ...READ MORE

answered Aug 16, 2018 in Python by Omkar
• 69,110 points
503 views