What is the method of creating multi-dimensional arrays…

There are various methods to create a multi-dimensional array, you can use the following methods to create one:

  1. create an array using numpy
import numpy as np

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

Output:

[[1 2 3]
 [4 5 6]]
  1. This function in NumPy creates an array filled with zeros.
import numpy as np

arr = np.zeros((3, 4))
print(arr)

Output:

[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
  1. create an array filled with ones using NumPy.
import numpy as np

arr = np.ones((2, 3, 4))
print(arr)

Output:

[[[1. 1. 1. 1.]
  [1. 1. 1. 1.]
  [1. 1. 1. 1.]]

 [[1. 1. 1. 1.]
  [1. 1. 1. 1.]
  [1. 1. 1. 1.]]]
  1. Create an empty numpy array.
import numpy as np

arr = np.empty((2, 2))
print(arr)

Output:

[[8.68257868e-312 6.27463370e-322]
 [0.00000000e+000 0.00000000e+000]]
  1. generate a sequence of numbers using numpy
import numpy as np

arr = np.arange(0, 10).reshape(2, 5)
print(arr)

Output:

[[0 1 2 3 4]
 [5 6 7 8 9]]

These methods are just some of the ways to create multi-dimensional arrays, there are also other methods such as loading data from a file or copying data from other arrays.

bannerAds