Python

How to install NumPy python development environment on Ubuntu

Python is a modern programming language now for supporting a large number of libraries. Various types of tasks can be done by using these libraries. NumPy is one of the useful libraries of Python to perform scientific operations. This library can be used to create a multi-dimensional array of objects. Different types of mathematical tasks can be done quickly using this library, such as sorting the array, reshaping array, statistical operation, arithmetical operations, etc. It works faster because it is developed by using the C programming language.

NumPy installation on Ubuntu:

You have to check the installed python version of the system before installing the NumPy library. Python3 is used in this tutorial to show the way of installing the NumPy library in Python. Run the following command to check the installed python version.

$ python3 -V

The following output shows that python version 3.8.6 is installed in the system.

Run the following command to install the NumPy library for Python3.

$ sudo apt install python3-numpy

Check the NumPy version from the terminal:

You can check the installed version of the NumPy library in multiple ways. The following command will show the installed NumPy library version if installed correctly by the previous command.

$ python3 -c "import numpy; print(numpy.__version__)"

The following output shows that NumPy version 1.18.4 is installed in the system.

Import and check the NumPy version

You can find out the installed version of the NumPy library by executing the python script also. Run the following command to execute the python script.

$ python3

Run the following python script from the python command prompt to check the installed NumPy library version.

>>> import numpy as np
>>> np.version.version

The following output shows both the version of Python and NumPy library.

Enable the NumPy in PyCharm editor:

Many python IDEs exist to execute python scripts. Some of the popular python editors are PyCharm, Spyder, Eric, Pyzo, Atom, Pydev, etc. PyCharm IDE is used in this tutorial to show how to write and execute python script by importing the NumPy library. You can run the following command to install PyCharm on Ubuntu.

$ sudo snap install pycharm-community --classic

You have to set the NumPy library location in PyCharm IDE to import the library in the script. Open the Settings window by clicking the Settings menu item from the File menu. Click on the project folder that was created before to store the python script. Here, the project folder name is Python located in the folder, /home/fahmida/PycharmProjects. Find out the numpy folder that is located under /venv/lib/python3.8/site-packages. Select the folder and click on the OK button.

Work with the NumPy:

Write the following script in a python file to know how the NumPy library can be used in the python script. NumPy array works faster than the python list that is shown by the output of this script. NumPy library is imported at the beginning of the script to create the NumPy array. Time library is imported to calculate the time required by python lists and NumPy arrays to do the same task. The size of the array will be taken as input from the user. Two python lists will be created by using the range() function based on the input value. Next, the current system time will be stored in the variable, start_time. Another new list will be created by multiplying each value of both lists. Both lists’ values are equal because range values create the lists, and both lists contain the same number of values. The new list variable, p_calculate, will contain each element of the list’s square value. Again, the current system time is stored in the variable, end_time. The difference between end_time and start_time will show the python list’s time to do the calculation. In the next part of the script, arange() function of the NumPy library is used to create two one-dimensional NumPy arrays of range values. Both arrays are multiplied to get the same output generated by two python lists in the previous statements. The time required to calculate the task using the NumPy array will be printed to compare the time needed for the python list and NumPy array.

# Import the necessary packages
import numpy as np
import time

# Take array size from the user
array_size = int(input("Enter the size of the array: "))
# Create two Python  lists based on the array_size value
list1 = range(array_size)
list2 = range(array_size)

# Set the start time
start_time = time.time()
# Create a list by calculating the square root
p_calculate = [(a * b) for a, b in zip(list1, list2)]
# Print the result
print("The result of the list: \n", p_calculate)
# Set the end time
end_time = time.time()
# Print the time value required by the python list
print("The time required by python list: ", end_time - start_time)

# Create two NumPy arrays based on the array_size value
np_array1 = np.arange(array_size)
np_array2 = np.arange(array_size)

# Set the start time
start_time = time.time()
# Create an array by calculating the square root
np_calculate = np_array1 * np_array2
# Print the result
print("The result of the array: \n", np_calculate)
# Set the end time
end_time = time.time()
# Print the time value required by the NumPy array
print("The time required by numpy array: ", end_time - start_time)

Output:

The following output will appear after executing the above script. The output shows that the python list requires more time than the NumPy array to do the same task.

Conclusion:

Installing and using the Python NumPy library for python3 is explained in this tutorial to help the reader use this library in their python script to solve different types of mathematical and scientific problems.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.