From Novice to Ninja: NumPy Arrays in Python — Day 03 (Mastering Essential NumPy Methods)

Data Science Delight
4 min readJul 7, 2023

--

Welcome back to another exciting NumPy series. In our previous article, we delved into the fascinating world of NumPy, where we explored the art of creating NumPy arrays and generating random numbers using NumPy functions. If you missed those articles, you can find them here:

In this article, we will expand our knowledge and dive deeper into the functionalities, methods & attributes that NumPy offers.

But before we dive into the exciting content, I would like to take a moment to express my gratitude for your support. Your likes, share, and engagement on our previous articles have been immensely inspiring.

I would like to encourage you to continue this incredible momentum by liking, sharing, and subscribing to our channel. By doing so, you not only support us but also help reach a wider audience of data enthusiasts who can benefit from our shared knowledge.

You can also follow my Medium profile for more in-depth articles and updates.

Let’s stay connected & continue this amazing data science adventure together.

Without further ado, let’s dive into the exploration of NumPy array’s methods. So, fasten your seatbelts, as we embark on this exhilarating journey through the realm of NumPy:

Finding the shape of the NumPy array:

The shape of an array refers to the dimensions or sizes of its axes. NumPy provides the following methods & attributes to find the shape of an array:

ndarray.shape:

The shape attribute of the NumPy array returns a tuple that represents the dimensions of the array.

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

The resultant output indicates that our array is a 2d array containing 2 rows and 3 columns.

ndarray.ndim:

The ndim attribute of the NumPy array represents the number of dimensions of an array.

arr = np.random.randn(3, 5)
arr.ndim

The resultant output shows the dimension of our array is 2. So, it is a 2d array.

Changing the shape of an array:

Suppose you have a 1d array containing 15 numbers and you want to change the shape of an array to a 2d array. To do that we can use the following methods or attributes:

np.reshape():

The np.reshape() function allows us to change the shape of an array without modifying its data.

The total number of elements in the original array should be same as that of the reshaped array.

# Original array:
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

# Reshaped array:
res_arr = np.reshape(arr, newshape = (3, 4))

The resultant 2d array, contains values without modifying the original data.

You can also write the above code in a single line:

arr = np.arange(1, 13).reshape(3, 4)
arr

np.resize():

The np.resize() function allows us to modify the shape of an array either by repeating or truncating elements.

  • It changes the shape of original array.
  • If the new shape is larger, the array will be filled with repeated values.
  • If the new shape is smaller, then the array will be truncated to a specified size.
# Original array:
arr = np.array([1, 2, 3, 4, 5])

# Resized Array:
res_arr = np.resize(arr, new_shape = (3, 4))

Since the new shape of array is larger than the original array, so the resultant array will be filled by repeated values.

Finding the actual data type of an array:

The .dtype attribute is used to get the actual data type of an array. This is how you can get it:

arr1 = np.arange(1, 11)
arr1.dtype

The resulatant output i.e. dtype(‘int32’), shows that the array is of “integer” data type.

Exercise Questions:

  1. Create a 1d NumPy array containing elements from 1 to 6. Reshape the array to have a shape of (2, 3). Print the reshaped array.
  2. Create a NumPy array of shape (3, 4) containing random integers ranging from 0 to 9. Resize the array to have a shape of (2, 6). Print the resized array and its shape.
  3. Create a NumPy array of shape (2, 3, 4) containing random floating-point values. Determine the dimensions of the array.

That’s it for Day — 03:

That’s it for today’s session. In the next session, we will be discussing about indexing & slicing used in NumPy arrays in detail along with the code.

Please Subscribe/Follow, Like, Share, and Clap as it would encourage me to write more such blog posts, especially on data analytics and data science field.

Stay Tuned!!

Thank you!

--

--

Data Science Delight

Content Creator | Sharing insights & tips on data science | Instagram: @datasciencedelight | YouTube: https://www.youtube.com/channel/UCpz2054mp5xfcBKUIctnhlw