Skip to content
Advertisement

Is there a way to create array with custom values using numpy array

Hello i want to ask is there a way to create an array with custom values without using loop and assign the values for example i want to create array with [2,1] shape and i want to fill the first column with value of (-1) and the second with value of (2) i have read the numpy docs “https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.array-creation.html” but i didn’t find this functionality. Thank you for any help.

theta = np.array([1, 2])  <-- how to assign diff values per column ?
theta.fill(-1)

I want to obtain the following result

   array([[-1, 2],
       [-1, 2]
       [-1, 2]
       [-1, 2]])


Advertisement

Answer

Without using the syntax of a full loop you can do the following.

For a simple array you can do this:

array = [[-1,2] for _ in range(3)]

Otherwise, for a numpy array, (documentation here here) you can simply ass np.array (as follows):

array = np.array([[-1,2] for _ in range(3)])
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement