I believe you must ever use NumPy library if you have used Python, so I guess I don’t have to introduce this library.

Since NumPy is such a library created for numerical and dimensional array calculation, one of the typical usage scenario of it is to generate dimensional arrays based on defined rules.

In this article, I’ll introduce all of the excellent built-in functions in NumPy for us to generate n-dimensional arrays with certain rules. Please be advised that random arrays can be a separated topic so that they will not be included in this article. I’m sure that you must know some or even most of the functions I would introduce in later sections, but it is highly recommended that to skim and scan this paper. I’m pretty sure that you may found some functions you do not know but are potentially very helpful.

Why NumPy is so Fast?

Image for post

Photo by jingoba on Pixabay

Before introducing the functions, I would like to mention why NumPy is so fast. The short answer is that because it is not Python.

You might be surprised that why NumPy is not Python. In fact, NumPy is implemented in the C programming language, which outweighs to Python in terms of the performance in most of the scenarios. Therefore, we write Python code to use NumPy, but under the hood it is C.

We can do a simple experiment to compare the performance. Given that the “list” such as [1,2,3] is pure Python object, we can do the same thing with a list and NumPy array to compare the elapsed time.

Of course, we need to import NumPy library before everything.

import numpy as np

Then, let’s generate a Python list with 1 million integers and then calculate the squares of every number in the list.

ls = list(range(1000000))
ls_square = [i**2 for i in ls]

Similarly, let’s do the same thing using NumPy.

arr = np.arange(1000000)
arr_square = arr ** 2

Image for post

It can be seen that NumPy is almost 100 times faster than the Python list. This is also why most of the other libraries for data analytics (e.g SciPy, Pandas), machine learning (s.g. Sci-kit Learn) and data visualisation (e.g. Matplotlib) usually depends on NumPy.

#data-science #linear-algebra #python #programming #numpy

Complete Works of Array Generating Functions in Python NumPy
1.20 GEEK