NumPY module

NumPY is a Python module that provides efficient operations on arrays. NumPY is the fundamental package for scientific computing with Python. This package is commonly used for Python data analysis. A NumPY array is a grid of multiple values.

Install NumPY by running the following command in your Terminal:

$ pip3 install numpy

We are going to use this numpy library to do operations on a numpy array. Now we are going to see how to create numpy arrays. For that, create a script called simple_array.py and write following code in it:

import numpy as np

my_list1 = [1,2,3,4]
my_array1 = np.array(my_list1)
print(my_list11, type(my_list1))
print(my_array1, type(my_array1))

Run the script and you will get the following output:

student@ubuntu:~$ python3 simple_array.py

The output is as follows:

[1, 2, 3, 4] <class 'list'>
[1 2 3 4] <class 'numpy.ndarray'>

In the preceding example, we have imported the numpy library as np to use numpy functionality. Then we created a simple list, which we converted into an array and for that we used the np.array() function. Finally, we printed the numpy array with type to easily understand a normal array and a numpy array .

The previous example was of a single dimensional array. Now we are going to look at an example of a multi-dimensional array. For that, we that we have to create another list. Let's  look at another example. Create a script called mult_dim_array.py and write the following content in it:

import numpy as np

my_list1 = [1,2,3,4]
my_list2 = [11,22,33,44]

my_lists = [my_list1, my_list2]
m
y_array = np.array(my_lists)
print(my_lists, type(my_lists))
print(my_array, type(my_array))

Run the script and you will get the following output:

student@ubuntu:~$ python3 mult_dim_array.py

The output is as follows:

[[1, 2, 3, 4], [11, 22, 33, 44]] <class 'list'>
[[ 1 2 3 4]
[11 22 33 44]] <class 'numpy.ndarray'>

In the preceding example, we imported the numpy module. After that, we created two lists: my_list1 and my_list2. Then we made another list of lists (my_list1 and my_list2) and applied the np.array() function on the list (my_lists) and stored it in an object called my_array. Finally, we printed the numpy array.

Now, we are going to look at more operations that can be done with an array. We are going to study how to know the size as well as the data type of our created array; that is, my_array. For that, we just have to apply the shape() function and we will get the size of the array and dtype() function to know the data type of the array on our created array. Let's look at an example of this. Create a script called size_and_dtype.py and write the following in it:

import numpy as np

my_list1 = [1,2,3,4]
my_list2 = [11,22,33,44]

my_lists = [my_list1,my_list2]
my_array = np.array(my_lists)
print(my_array)

size = my_array.shape
print(size)

data_type = my_array.dtype
print(data_type)

Run the script and you will get the following output:

student@ubuntu:~$ python3 size_and_dtype.py

The output is as follows:

[[ 1  2  3  4]
[11 22 33 44]]
(2, 4)
int64

In the preceding example, we applied the shape function as my_array.shape to get the size of our array. The output was (2, 4). Then we applied the dtype function as my_array.dtype on the array and the output was int64.

Now, we are going to look at some examples of special case arrays.

First, we will make an array with all zeros using the np.zeros() function, as shown here:

student@ubuntu:~$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.zeros(5)
array([0., 0., 0., 0., 0.])
>>>

After making the array with all zeros, we are going to make the array with all 1's using the np.ones() function of numpy, as shown here:

>>> np.ones((5,5))
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
>>>

np.ones((5,5)) creates an array of 5*5 with all values being 1.

Now, we are going to make an empty array using the np.empty() function of numpy, as shown here:

>>> np.empty([2,2])
array([[6.86506982e-317, 0.00000000e+000],
[6.89930557e-310, 2.49398949e-306]])
>>>

np.empty() does not set the array values to zero, like the np.zeros() function does. Therefore, it may be faster. Besides, it requires the user to enter all the values manually in the array and should therefore be used with caution.

Now, let's see how to make an identity array using the np.eye() function, which results in the array with its diagonal value 1, as shown here:

>>> np.eye(5)
array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
>>>

Now, we are going to see the range function, which is used to create an array using the np.arange() function of numpy, as shown here:

>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>>

The np.arange(10) function creates the array of range 0-9. We defined the range value 10, and because of that, the array index value starts with 0.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset