Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:56:12+00:00 2026-06-18T06:56:12+00:00

i have an error at this line:neigh.fit(X, y) : ValueError: setting an array element

  • 0

i have an error at this line:neigh.fit(X, y) :
ValueError: setting an array element with a sequence.

I checked fit function and X is: {array-like, sparse matrix, BallTree, cKDTree}
My X is a list of list with first element solidity number and second elemnt humoment list (7 cells).
If i change and i take only first humoment number for having a pure list of list
give this error: query data dimension must match BallTree data dimension.

My code:

listafeaturevector = list()
path = 'imgknn/'
for infile in glob.glob( os.path.join(path, '*.jpg') ):
    print("current file is: " + infile )
    gray = cv2.imread(infile,0)
    element = cv2.getStructuringElement(cv2.MORPH_CROSS,(6,6)) 
    graydilate = cv2.erode(gray, element)
    ret,thresh = cv2.threshold(graydilate,127,255,cv2.THRESH_BINARY_INV) 
    imgbnbin = thresh

    #CONTOURS
    contours, hierarchy = cv2.findContours(imgbnbin, cv2.RETR_TREE ,cv2.CHAIN_APPROX_SIMPLE)
    print(len(contours))

    for i in range (0, len(contours)):
        fv = list()  #1 feature vector

        #HUMOMENTS
        #print("humoments")
        mom = cv2.moments(contours[i], 1)  
        Humoments = cv2.HuMoments(mom)
        #print(Humoments) 
        fv.append(Humoments) #query data dimension must match BallTree data dimension

        #SOLIDITY

        area = cv2.contourArea(contours[i])
        hull = cv2.convexHull(contours[i]) #ha tanti valori
        hull_area = cv2.contourArea(hull)
        solidity = float(area)/hull_area
        fv.append(solidity)

        #fv.append(elongation)
        listafeaturevector.append(fv)

print("i have done")
print(len(listafeaturevector))
lenmatrice=len(listafeaturevector)

#KNN
X = listafeaturevector
y = [0,1,2,3]* (lenmatrice/4)

from sklearn.neighbors import KNeighborsClassifier
neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X, y)  #ValueError: setting an array element with a sequence.

print(neigh.predict([[1.1]]))
print(neigh.predict_proba([[0.9]]))

If i try to covert it in a numpy array:

listafv = np.dstack(listafeaturevector)
listafv=np.rollaxis(listafv,-1)
print(listafv.shape)
data = listafv.reshape((lenmatrice, -1))
print(data.shape)

#KNN

X = data

i got: setting an array element with a sequence

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:56:13+00:00Added an answer on June 18, 2026 at 6:56 am

    A couple of suggestions/questions:

    Humoments = cv2.HuMoments(mom)
    

    What is the class of the return value Humoments? a float or a list? If float, that is fine.

    for each image file
        for i in range (0, len(contours)):
           fv = list()  #1 feature vector
           ...
           fv.append(Humoments) 
           ...
           fv.append(solidity)
           listafeaturevector.append(fv)
    

    The above code does not seem correct. In your problem, I think you need to a construct a feature vector for each image. So anything that is related to image i should go to the same feature vector x_i. Then you combine all feature vectors to get a list of feature vectors X. However, your listafeaturevector (or X) presents in the inner-most loop, it’s obviously not correct.

    Second, you have a loop against the number of elements in the contours, are you sure the number of elements stays the same for each image? Otherwise, the number of features (|x_i|) is totally different across different images, that might cause the error of

    setting an array element with a sequence.
    

    Third, are you clear about how you want to classify the images? what are the target values/labels of different images? I see you just setting labels with [0,1,2,3]* (lenmatrice/4). Can you elaborate on what you are trying to do with those images? Are they containing different type of object? Are they showing different patterns? Are those images describe different topic/color? If yes, for each different type, you give a different label – either 0,1,2 or ‘red’,’white’,’black’ (assume you have only 3 types). The values of the label do not matter. What matters is how many values they have. I am trying to understand the difference of labels in your case.

    On the other hand, if you only want to retrieve similar images, you don’t need to use a classifier or specify a label for each image. Instead, try to use NearestNeighbors.

    print(neigh.predict([[1.1]]))
    print(neigh.predict_proba([[0.9]]))
    

    Fourth, the above two lines of test are not correct. You need to set an X-like object in order to get a prediction from the classifier. That is to say, you need a feature vector x with the identical structure as you constructed in your training examples (with all h,e,s in the same order).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this error: TypeError: $(element).slider is not a function with the following script:
I have an unexpected ; error on this line: $action = ($sessionMinus == $_SESSION['initial_count'])
I have this error message: Msg 8134, Level 16, State 1, Line 1 Divide
I have 1 compiler error. It is from this line in my code: cout
I have this line of code but it throws an error. What is the
I have a line of php code that looks like this: echo <script>$('#edit_errors').html('<h3><em>Please Correct
In this line of code <% var tmp = int.Parse(ViewData[numOfGroups].ToString()); %> I have error:
I have probably made some error in this line of code: $('#mbContentText [name^=mbItem]').css({color: #006699,
i have got an error in this line: new ServerSocket(2106, 50, InetAddress.getByName(83.4.200.1)); Error log:
I have an error somewhere in this line of code, if I take by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.