DEV Community

Cover image for Day 18-22: Numpy in One Week: A Learner's Journey
John Enad
John Enad

Posted on

Day 18-22: Numpy in One Week: A Learner's Journey

Diving into the world of Numpy, a powerful and versatile library for numerical computing in a short amount of time and having rusty math skills was quite daunting. In this blog post, I'm going to share my experiences and insights.

To kick it off, I started by learning the fundamentals of Numpy using the W3 Schools Numpy Tutorial and some articles from Real Python. I learned that Numpy provides the ability to work with arrays which are a far better alternative to Python lists when dealing with numbers. I used PyCharm and discovered that you can create Jupyter notebooks in a Pycharm project. That's cool.

The first few lessons were a breeze and learned about creating and manipulating arrays, slicing and indexing, shaping, joining, splitting, search, sorting and filtering - Pretty much most of the common operations one could use on arrays of data. The array type in Numpy is called an ndarray and is used by other popular libraries like Pandas, Matplotlib, SciPay and many others.

Some methods used to create arrays: np.array(), np.zeros(), np.ones(), and np.arange()

Example:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])
arr0 = np.zeros((3, 4))
arr1 = np.ones((3, 4))
arr2 = np.arange(1, 10, 2)
print(arr)
print(arr0)
print(arr1)
print(arr2)

Result:
[1 2 3 4 5]
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]
[1 3 5 7 9]
Enter fullscreen mode Exit fullscreen mode

Array index is used to access array elements. For example:
arr = np.array([1, 2, 3, 4, 5, 6, 7])

To access the first element of arr, you would use index 0 like this: arr[0] and the third element using: arr[2]

Slicing is very similar to slicing in lists using [start:end] so for example to get the 3rd through 5th elements in arr above use: arr[2:5] which returns [3 4 5]

Next, I explored various array attributes like shape, size, and dtype, which provide information about an array's structure and data type. The array shape determines the number of elements for each dimension. The example below shows that arr has 2 dimensions. The first dimension has 3 elements and the second dimension has 4 elements. An ndarray has a property called dtype that returns the data type of the array. In this case, it returns int32

import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(arr.shape)
print(arr.dtype)

Result:
(3, 4)
int32
Enter fullscreen mode Exit fullscreen mode

I really appreciate the simplicity of the reshape command. It allows for changing the number of dimensions or changing the number of elements of each dimension. Below, the code changes the one-dimensional array with 12 elements into a two-dimensional array with the first dimension having 4 elements and the second dimension having 3 elements. The ndim property gives us the number of dimensions.

import numpy as np

arr = np.array([11, 22, 33, 44, 51, 61, 71, 81, 92, 109, 101, 102])
newarr = arr.reshape(4, 3)
print(newarr)
print(newarr.ndim)

Result:
[[ 11  22  33]
 [ 44  51  61]
 [ 71  81  92]
 [109 101 102]]
2
Enter fullscreen mode Exit fullscreen mode

There are so many useful built-in functions in NumPy to mention in one blog post. Just a few of them I tried were np.sum(), np.mean(), np.max() and np.min() which help simplify the process of performing aggregate operations on arrays. Additionally, I explored various techniques for combining arrays, such as np.concatenate(), np.vstack(), and np.hstack().

The w3schools tutorial had a number of pages dedicated to Random Number Generation and Statistical Functions so I delved into those, I experimented with generating random integers, floats, and samples from various probability distributions like Normal, Binomial and Poisson distributions. I had to lookup the various distributions as I have not dealt with those in ages. I will probably spend some time reviewing and learning about them in the near future.

On the final day of my one-week Numpy journey, I explored Numpy's built-in statistical functions, such as np.mean(), np.median(), np.var(), and np.std(), which facilitate the computation of essential summary statistics for data analysis.

For me, learning NumPy in a week was no easy feat and it was just enough to get my feet wet. Hopefully, I will get to learn more about it as I continue on my Python journey. I will definitely have to re-learn Math, Statistics and Probability and Linear Algebra in order to be able to make better use of NumPy in the future.

Top comments (0)