This is a step by step tutorial of the Numpy module in python for Beginners. Learn about Numpy arrays and setting up Numpy on our system to get started.
What is an Array?
An array is a variable that can store multiple values in itself. An array can hold a large number of values so they can be accessed using index numbers. The index always begins with 0 being the first element of the array and moving to the nth element, which will be the last. These values are of the same data type, which can be one of these strings, integer or float and also many others. Arrays can be of three types such as indexed arrays, associative arrays and multidimensional arrays.
Definition of NumPy:
NumPy is a python based library that helps in the manipulation of the multidimensional arrays with regards to various factors. As a result, this helps in maintaining large and multidimensional arrays and matrics. NumPy stands for Numerical Python. So this open-source help in performing various mathematical functions because it is capable of solving complex problems in arrays. A NumPy array is a group of values with the same data type, and these are given their index number with only non-negative integer numbers. The dimension form up the rank of the array and also integer which gives the size of the array along with dimension form the shape of the array.
History of NumPy:
In the early years, the Python was not supposed to be used for the numerical reason; as a result, it was not so common among the developers. In the year of 2005, Travis Oliphant was trying to develop a unified array package, and the next year it came in to use known with the name of NumPy 1.0 in the year 2006. Everyone can use it as it is open source and free to use.
I am going to discuss a few reasons why we should use NumPy:
- NumPy helps in the faster processing of the array.
- NumPy is preferred because it can perform operations like Searching and Sorting.
- It can perform all sorts of mathematical and logical operations.
- Arrays can operate fast. As a result, they are better than lists.
- It is good for data analysis.
- It is a good replacement for MATLAB and OCTAVE.
- Supports the use of vector operations.
- It is convenient to perform operations on arrays.
- NumPy is very much helpful in machine learning and data science.
- It helps in high-performance computing and simulations.
Key Features of NumPy:
- It can handle on multidimensional arrays.
- NumPy refers to array objects as
ndarrayas a result, it provides various supporting functions.
- It can work with all the latest CPU architectures.
- It has tools for using C/C++ and Fortran Code.
Limitations of NumPy:
- NumPy deals with the problem of missing values, but it supports ‘NAN’ which creates confusion among the users.
- Also, it creates issues while comparing values with the help of a python interpreter.
- It requires the allocation of memory for performing functions like addition and insertion, which makes it difficult to manage the space.
- This memory allocation also makes it costly to use.
- Also, it requires shifting in order to use various memory allocations.
- It focuses on working with only numerical data.
NumPy Codebase :
The location for the source code of the NumPy is as follows: https://github.com/numpy/numpy
And also GitHub allows any number of users to use their codebase and they can work on the same codebase as it is enabled by the GitHub.
Why we use NumPy rather than Python lists?
In order to understand the difference between the two, we need to what is a python list?
The list is a collection of data that is changeable and can also repeat itself. In Python, the list is always written in square brackets. Let’s go through an example of lists:
list = ["cars","scooters","buses"] print(list)
So here we are trying to access the element with the index number .
As we learnt about what a python list now lets us understand the difference between Using NumPy or Python lists. We prefer using NumPy because of following reasons:
- The major difference is in the performance; as a result, the data structures perform better in numPy.
- They take up less data space.
- Their processing time is much faster than the lists processing time.
- NumPy has many enhanced functions that help in performing various operations like linear or algebra.
- It has better runtime behaviour.
- The whole compilation process is pretty quick and much faster than lists.
- The storage of arrays takes place at one continuous place in the memory.
Installation of NumPy:
In order to install NumPy on our system, we need to have Python and Pip already installed on our systems. Now let’s install NumPy step by step :
Step:1 Checking the version of Python installed
Before installing NumPy in your system, you need to know the version of Python installed. For most of the operating, we use like mac and Ubuntu the Python comes initially installed except for the windows which we can install by going here. We can check the version of the Python installed by using these commands:
To check whether if we have Python, run the following code in your CMD or Terminal(Mac):
In order to check the version of Python in Linux, run this code:
By using these two commands, you will get to know the version of the Python in your system. But if in any case, you are unable to get version it probably means you need to install it.
Step:2 Installing Pip
Pip is a package management system that helps us in installing and managing the package which is written in Python. It is licensed by MIT and was initially released in the year 2011. It is the easiest way of installing NumPy into our systems. Pip does not come installed into the system; we need to install them as per the version of Python we have.
In order to check if the pip is already installed, run this code in your command line:
If you have it already have, it will show you that pip is installed and if not then it would give an error message stating “program not found”.
In case pip is not present on your system, you can follow the following steps:
- Before installing download the get-pip.py file from here.
- Open the command prompt window on your system.
- In order to install pip, run the following code:
- The installation would start but be very considerate about the path where you have this file.
- Then to check the current version of the pip, run this code:
This command will return the current version of pip on your system.
In case you are using Ubuntu, then you need to follow these steps:
- Here the command line would use apt utility, install pip by running this code in your terminal:
sudo apt install python-pip
- Then verify the pip version by running this code in the terminal window:
By following these steps, you will be able to install the pip package manager on your system.
Step:3 Installing the NumPy
As pip is installed on your system., now you need to open your command prompt windows or the terminal.
For Installing pip for Python with version 2 by running this code:
pip install numpy
This will install the NumPy and also show you the version of the NumPy.
Step:4 Verifying the NumPy Installation
we can verify the installation of the NumPy by using the show command and find it it is part of our python package.
In order to check for Python with version 2, run this code:
pip show numpy
This should confirm if you have installed the NumPy properly and also which version you have in your system along with the location of the number where the package is present in your system.
Step:5 Importing the NumPy package into your system
Now that you have NumPy into our system, you will need to import it every time you need to perform some functionality in it. You can do so by using the import keyword.
In order to do so, we need to open up your Python prompt and type the following commands.
Now that you are in your prompt, you can import the package and also make an alias with the help of
as keyword. Here we are making alias as
Run this code:
import numpy as np
Now the package can be referred to as
np rather than
Upgradation of NumPy:
As now that you have installed NumPy, you can also upgrade its version whenever it is required as per the version which is available at the latest.
In order to upgrade NumPy if you are using pip, run the following code:
pip install --upgrade numpy
Install via Package Manager:
System manage packager can install various packages all over the system, and they usually use older versions of the packages.
For Ubuntu users, you can use the apt-get method and install it as a whole package on the system.
run this code:
sudo apt-get install python-numpy python-scipy python-matplotlib python-pandas ipython python-nose
For Mac users, there is no package manager already present in the system so they need to install a package manager first and then they could install it on the whole system. Some of the popular package managers are like Macports for python 3.5, and there is also another Homebrew which has incomplete coverage of certain package, but it can install NumPy, scipy, ipython and jupyter.
Install with Binaries:
These Binary files can be directly used to install packages which can come directly from the sources like Github or PyPi or also third-party repositories. For Linux, we can directly search for these binary files which are available and download them and use them. For Windows, there are various prebuilt installers available online which could be downloaded and used for the same purpose.
Other ways to install NumPy:
There are many other ways to install NumPy into your system. So let us discuss some ways to install NumPy on the window without using pip. So some of the good solutions for the windows are Enthought Canopy, Anaconda and Python(x,y). Anaconda is the only one that provides a binary installer for Windows, OS X and also for Linux and it is helping more than 15 million people and also it best for the beginners who will be interacting with it for the first time, and it provides a large number of packages to use. For advanced users, they can use Miniconda, which is a better way of installing the Conda package manager. All these packages include Python, NumPy and also many additional packages that could be used for manipulation of data.
Other options also include :
- WinPython: It is a free, open-source portable distribution for Python which window user can only use, and it supports Python 3 version. It is portable, flexible and customizable.
- Pyzo: It is an open-source computing environment based on Python. It works with Windows, Linux as well as Mac systems. The latest version is 4.10.2, and it works with a Python interpreter, including a condo environment.
I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!