0

I want to create a n-dimentional numpy array. Following is my code

import numpy as np
random_weights = np.empty(3)
random_weights[0] = np.array([0,1,2])
random_weights[1] = np.array([3,4,5])

Above code gives me ValueError: setting an array element with a sequence. error. I am trying to create multi-dimentional array. What is the reason for this issue?

2
  • How about random_weights = np.array([[0,1,2], [3,4,5]]) ? Commented Feb 28, 2018 at 7:27
  • it is not the way i need to do it, i want to assign values on the fly Commented Feb 28, 2018 at 7:27

1 Answer 1

2

If you want a 2d array, you need to define it.

import numpy as np

random_weights = np.empty((2, 3))  # 2 rows, 3 columns
random_weights[0] = np.array([0,1,2])
random_weights[1] = np.array([3,4,5])
Sign up to request clarification or add additional context in comments.

1 Comment

This indexing could also be written as random_weights[0,:] = [0,1,2], emphasizing that it is filling in the 0'th row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.