Expand numpy array dimension and reshaping

Related searches

So i have a numpy array containing:

arr =  [1 2 3 4 5 6]

and when i execute:

print(arr.shape)

it gives me:

(6,)

I'm trying to add in the constant value 3

 const_val = 3

into the dimension of the array so i would obtain:

(6,3)

First, i tried expanding the dimension of the array by:

arr = np.expand_dims(arr, axis = -1)

where now:

print(arr.shape)

returns me:

(6,1)

However, when i try reshaping the array dimension to replace 1 with the constant value 3,

arr = np.reshape(arr, (arr.shape[0], const_val))

I get the error saying:

ValueError: cannot reshape array of size 6 into shape (6,3)

May i know why did this happen?

Do you want to add two empty columns to your array? In that case you can concatenate a matrix of size (6,2):

arr = np.array([1,2,3,4,5,6]).reshape(6,1)
to_add = np.zeros((6,2))
res = np.concatenate((arr, to_add), axis=1)

numpy.expand_dims — NumPy v1.19 Manual, Expand the shape of an array. reshape. Insert, remove, and combine dimensions, and resize existing x = np.array([1, 2]) >>> x.shape (2,). Reshaping arrays. Reshaping means changing the shape of an array. The shape of an array is the number of elements in each dimension. By reshaping we can add or remove dimensions or change number of elements in each dimension.

When reshaping from (6,1) to (6,3), you need to 'imput some values' because you go from 6 entries to 18. Do you want the two new columns to be identical to the first one? In that case use numpy.tile as:

arr_6_3 = np.tile(arr, (1,3))

numpy.expand_dims — NumPy v1.13 Manual, Insert a new axis that will appear at the axis position in the expanded array shape . dimensions; reshape: Insert, remove, and combine dimensions, and resize� numpy.expand_dims¶ numpy.expand_dims (a, axis) [source] ¶ Expand the shape of an array. Insert a new axis that will appear at the axis position in the expanded array shape.

If I understand your requirement correctly, the following solution would be the one you need:

In [55]: arr =  np.array([1, 2, 3, 4, 5, 6])

# our new desired shape
In [56]: new_shape = (6, 3)

# broadcast to `new_shape` and make it writable 
# since `broadcast_to` returns a non-writable and referenced array
In [57]: arr_new = np.array(np.broadcast_to(arr[:, None], new_shape))

# copy the constant value
In [58]: arr_new[:, 1:] = const_val

In [59]: arr_new
Out[59]: 
array([[1, 3, 3],
       [2, 3, 3],
       [3, 3, 3],
       [4, 3, 3],
       [5, 3, 3],
       [6, 3, 3]])

If, on the other hand, you just want to replicate the same array to the new shape, then use:

In [63]: const_val = 3

In [64]: new_shape = (arr.shape[0], const_val)

In [65]: arr_new = np.array(np.broadcast_to(arr[:, None], new_shape))

In [66]: arr_new
Out[66]: 
array([[1, 1, 1],
       [2, 2, 2],
       [3, 3, 3],
       [4, 4, 4],
       [5, 5, 5],
       [6, 6, 6]])

In [67]: arr_new.shape
Out[67]: (6, 3)

Adding dimensions to numpy.arrays: newaxis v.s. reshape v.s. , This post demonstrates 3 ways to add new dimensions to numpy.arrays using numpy.newaxis, reshape, or expand_dim. It covers these cases� In python, reshaping numpy array can be very critical while creating a matrix or tensor from vectors. In order to reshape numpy array of one dimension to n dimensions one can use np.reshape() method. Let’s check out some simple examples. It is very important to reshape you numpy array, especially you are training with some deep learning network.

NumPy Array manipulation: expand_dims() function, The expand_dims() function is used to expand the shape of an array. Insert a new axis that will appear at the axis position in the expanded array� numpy.reshape() function. The reshape() function is used to give a new shape to an array without changing its data. Syntax: numpy.reshape(a, newshape, order='C')

Reshaping and three-dimensional arrays — Functional MRI methods, import numpy as np >>> arr_1d = np.arange(6) >>> arr_1d array([0, 1, 2, 3, 4, 5]) >>> arr_1d.shape (6,). We can reshape this array to two dimensions using the� numpy.reshape() ndarray.reshape() reshape() Function/Method Shared Memory numpy.resize() NumPy has two functions (and also methods) to change array shapes - reshape and resize. They have a significant difference that will our focus in this chapter. numpy.reshape() Let’s start with the function to change the shape of array - reshape().

The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.

Comments
  • Well how are you going to reshape an ndarray with 6 values, into one of 18? What values do you want the additional rows to have?
  • Reshaping only works if the total amount of elements in the array stays the same.