1

I have a list: s = [1,0,1,1,0,1,1,1,0,0] Length of the list is 10. I'd like to make a 10 x 10 numpy array with the same list repeated 10 times.

I've been unable to achieve this in python. I'd later like to turn this array to an image for an image classification problem.

How can I go about this?

I tried:

np.reshape(s,(10,10))

But it throws - ValueError: cannot reshape array of size 10 into shape (10,10)

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
a_jelly_fish
  • 478
  • 6
  • 21
  • "I'd later like to turn this array to an image" - sounds like it's not going to be a very interesting image, just a series of straight lines like a barcode. – Alex Hall Feb 29 '20 at 12:55

2 Answers2

2

Use numpy.tile as said in the post suggested in the comments.

If you want to go on with your approach, to get a 10x10 matrix s must contain 100 elements, not 10, so the solution is:

np.reshape(s*10, (10,10))
Gerardo Zinno
  • 1,518
  • 1
  • 13
  • 35
  • Yes, the actual size of the list is 200. I only made it 10 so it is readable int he body of the question. Thank you. :) – a_jelly_fish Feb 29 '20 at 13:06
  • @aneeshasc. OP isn't obligated to read your mind, only answer what you posted. Please select an answer even though your question was closed. – Mad Physicist Feb 29 '20 at 13:59
1

You can also use,

np.repeat(s,10).reshape(10,10)
Nik P
  • 224
  • 1
  • 5