numpy add 2d array to 3d array code example
Example 1: print column in 2d numpy array
import numpy as np
x=np.arange(25).reshape(5,5)
print(x)
#print(x[:,index_of_column_you_need])
print(x[:,3])
Example 2: numpy convert 1d array to 2d
import numpy as np
# 1D array
one_dim_arr = np.array([1, 2, 3, 4, 5, 6])
# To convert to 2D array
# we can use the np.newaxis to increase the dimesions in a array
# Using np.newaxis will increase the dimensions of your array by one dimension when used once.
two_dim_arr = one_dim_arr[np.newaxis, :]
print(two_dim_arr)
# inserting a axis at the first index creates a row vector
print()
# for column vector, insert axis at the second index
two_dim_arr = one_dim_arr[:,np.newaxis]
print(two_dim_arr)
print()
# we can also expand an array by inserting a new axis at a specified position with np.expand_dims.
two_dim_arr = np.expand_dims(one_dim_arr,axis = 0)
print(two_dim_arr)
print()
two_dim_arr = np.expand_dims(one_dim_arr,axis = 1)
print(two_dim_arr)