Sign Up

Have an account? Sign In Now

Sign In

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

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

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

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 323
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 0
Kenil Vasani
Asked: December 10, 20202020-12-10T22:48:56+00:00 2020-12-10T22:48:56+00:00In: Python

ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)

  • 0

I have a list say, temp_list with following properties :

len(temp_list) = 9260  
temp_list[0].shape = (224,224,3)  

Now, when I am converting into numpy array,

x = np.array(temp_list)  

I am getting the error :

ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)  

Can someone help me here?

numpypython
  • 1 1 Answer
  • 9 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-10T22:47:00+00:00Added an answer on December 10, 2020 at 10:47 pm

      At least one item in your list is either not three dimensional, or its second or third dimension does not match the other elements. If only the first dimension does not match, the arrays are still matched, but as individual objects, no attempt is made to reconcile them into a new (four dimensional) array. Some examples are below:

      That is, the offending element’s shape != (?, 224, 3),
      or ndim != 3 (with the ? being non-negative integer).
      That is what is giving you the error.

      You’ll need to fix that, to be able to turn your list into a four (or three) dimensional array. Without context, it is impossible to say if you want to lose a dimension from the 3D items or add one to the 2D items (in the first case), or change the second or third dimension (in the second case).


      Here’s an example of the error:

      >>> a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,224))]
      >>> np.array(a)
      ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)
      

      or, different type of input, but the same error:

      >>> a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,224,13))]
      >>> np.array(a)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)
      

      Alternatively, similar but with a different error message:

      >>> a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,100,3))]
      >>> np.array(a)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      ValueError: could not broadcast input array from shape (224,224,3) into shape (224)
      

      But the following will work, albeit with different results than (presumably) intended:

      >>> a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((10,224,3))]
      >>> np.array(a)
      # long output omitted
      >>> newa = np.array(a)
      >>> newa.shape
      3  # oops
      >>> newa.dtype
      dtype('O')
      >>> newa[0].shape
      (224, 224, 3)
      >>> newa[1].shape
      (224, 224, 3)
      >>> newa[2].shape
      (10, 224, 3)
      >>> 
      
      • 6
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      ERROR: torch has an invalid wheel, .dist-info directory not found

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com