Member-only story
Hi everyone, welcome back. NumPy is a library for the Python programming language. NumPy is short for “Numerical Python” and is a popular library that is used in data science. NumPy is used specifically to work with arrays as it provides various functions and support. In this tutorial, we will go over how we can shape our array, so our arrays will know how many dimensions and how many elements it will have. With this introduction out of the way, let’s get into it.
Shape Property
First let’s take a look at the shape property. The shape property returns a tuple which will show how many elements are in each dimension. Let’s see an example:
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]])
print(array.shape)Output:
(2, 3)
We can see that our tuple shows that we have 2 elements in the first dimension which would be the array containing 1, 2, and 3, and the array containing 4, 5, and 6. Then our second dimension has 3 elements which would be the individual values in the arrays, as each array has 3 elements.
ndmin Argument
The ndmin argument is an optional parameter that can be used when creating arrays to define the number of dimensions. Let’s see an example:
import numpy as np…