Complete Works of Array Generating Functions in Python NumPy | by Christopher Tao | Towards Data Science

I believe you must ever use the 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 its typical usage scenarios 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.
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 Python in terms of 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 a 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 the 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
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.
Let’s start to generate NumPy arrays in a certain range.
The first one, of course, will be np.arange() which I believe you may know already.
To generate an array starting from a number and stopping at a number with a certain length of steps, we can easily do as follows. Please be aware that the stopping number is not included.
np.arange(start=1, stop=10, step=1)
Sometimes, if we want the array to start from zero, we don’t need to specify the start number, because it is zero by default. Also, the default step is 1. So, if we want to generate an array start from 0, stop at 10 (need to specify) and with step equals to 1, the following code will do.
np.arange(10)
It is probably worth mentioning that the step can also be a float type. This is comparing with the Python built-in function range() because the latter doesn’t support float numbers as step length.
np.arange(start=1, stop=5, step=0.5)
Although it might not be very commonly used, the step can also be a negative number. So, the array will be in descending order, where the start number is larger than the stopping number.
np.arange(start=10, stop=1, step=-2)
If you want to generate an array with certain types, you can explicitly specify it.
arr = np.arange(10, dtype=float)
This function is a bit more powerful than np.arange(). Its name “linspace” stands for “linear space”. Therefore, it is used to generate arrays with numbers having linear relationships.
The major difference is that linspace() doesn’t rely on the step length. Instead, we need to specify how many numbers we are in between the start and stop numbers.
np.linspace(start=1, stop=10, num=10)
In the above two examples, the start and stop numbers are the same, but the arrays generated are different because we asked for one more number for the second array. Therefore, the function generates an array that is strictly an arithmetic sequence.
You may also be noticed that this time the stop number is inclusive, but we can explicitly exclude it.
np.linspace(1, 10, 11, endpoint=False)
We can also explicitly specify the data type we want for the array. However, be careful when specifying integers as the data type because the numbers will be floored rather than rounded.
np.linspace(1, 10, 11, dtype=int)
You might be curious that we have got “linear space”, but is there any other “spaces”? Yes, we have “log space” as well, which is not linear. I believe this function is not known by most developers but it is a very elegant way to generate an array as a geometric sequence.
np.logspace(start=1, stop=10, num=4, base=2)
Look at this example, similarly, we have start number and stop number. The third parameter “num” is how many numbers we want in the array. The last parameter “base” is the base number we want.
In fact, this function generated the following 4 numbers:
They are also a strictly geometric sequence as follows:
It is needed to mention that, the “base” number is 10 by default. Therefore, if we want to have the “base” number equals to 10, we don’t need to specify it explicitly.
np.logspace(1, 5, 3)
Because NumPy is powerful in computing multi-dimensional arrays (matrices), we also need to generate multi-dimensional arrays.
It is very common that we need to generate an array with certain dimensions with all its values are zeros. For example, an image is represented in a 1024 * 768 * 3 array, where axis 3 represents the RGB channels. If we want to filter the image with only one colour channel, we need to have both the other two channels with all values are zero. In such use cases, the np.zeros() function is very helpful.
If we want to generate a 2D array which is 2 (rows) by 3 (columns), the code as follows will do.
np.zeros((2,3))
Not only for zero, but we can also generate arrays with ones.
np.ones((2,3))
It is important to be aware that the arrays we generated using np.zeros() and np.ones() are of float type, it can be seen from the dot 0. (float)which is not 0 (int). This is sometimes not convenient if we need them to be int. Fortunately, we can explicitly specify the data type.
np.zeros((2,3), dtype=int)
What if we want neither zeros nor ones but other numbers? In such scenarios, we can use np.full(). For example, if we want to generate an array with certain dimensions and all its values should be 255, just do the following.
np.full((2,3), 255)
It is also worth mentioning that np.full() can not only be used to generate numeric arrays, but also other data types such as strings.
np.full((2,3), 'abc')
For convenience, NumPy also allows us to generate arrays from other arrays. That is, to copy the shape of another array, as well as its data type.
For example, let’s create a sample array explicitly.
sample_arr = np.array([    [1,2,3],    [4,5,6]])
This sample array has dimensions 2 * 3, and the data type is an integer.
If we want to generate an array having the same dimensions and data type, but the values are all zeros, we can use np.zeros_like().
np.zeros_like(sample_arr)
Similarly, we can also generate all ones.
np.ones_like(sample_arr)
And customised numbers.
np.full_like(sample_arr, 255)
Of course, if you want, you could use np.zeros() to implement the same thing, but don't forget that by default it creates an array of float types. So we need to explicitly specify that we want integers.
np.zeros_like(sample_arr)# is equivalent to np.zeros(sample_arr.shape, dtype=sample_arr.dtype)
What if we don’t know the type of the sample array? For example, this is in a loop of another function. Then, we have to use np.zero_like() in those cases.
In some scenarios, we also need to generate some special arrays, such as identity and diagonal matrices.
To generate identity matrices, we use np.eye(), just specify the number of rows/columns we need for the matrix.
np.eye(5)
When we want to generate a diagonal matrix, we can use np.diag(). We need to pass in a list with values that are supposed to be in the diagonal.
np.diag([1,2,3,4,5])
Last but not least, I want to introduce how to generate a multi-dimensional array with a customised function.
The function np.fromfunction() takes two required arguments:
The user-defined function that takes a tuple with indexes for all the dimension axis
The dimensions of the array
Let’s see an example. Suppose we want to generate a 6 by 5 matrix. Each number will have its row number as tens place and column number as ones place. We can define the function as follows.
def gen(row, col):    return (row+1)*10 + (col+1)
Then, we can pass the function into np.fromfunction() to generate the array we want.
A = np.fromfunction(gen, (6,5))
In this article, I have introduced why NumPy is preferred and why it is so fast. Then, we focused on how to generate multi-dimensional arrays using NumPy. I believe that these are all the functions you need to know to facilitate your data analytics, data mining, machine learning and data visualisation jobs in Python. Hope it helped.
All the code used in this article can be found in my Google Colab Notebook.
colab.research.google.com
Life is short, use Python!
medium.com
If you feel my articles are helpful, please consider joining Medium Membership to support me and thousands of other writers! (Click the link above)