Python NumPy Tutorial – Mastery With NumPy Array Library
In the Python NumPy tutorial will discuss each and every topic of NumPy array python library from scratch. At the end of this tutorial, you will achieve mastery in the NumPy library.
What is NumPy?
NumPy is a scientific computing package (library) for python programming language.
Numpy is a powerful Python programming language library to solve numerical problems.
What is the meaning of NumPy word?
Num stands for numerical and Py stands for Python programming language.
Python NumPy library is especially used for numeric and mathematical calculation like linear algebra, Fourier transform, and random number capabilities using Numpy array.
NumPy supports large data in the form of a multidimensional array (vector and matrix).
Prerequisites to learn Python NumPy Library
NumPy Python library is too simple to learn. The basic python programming and its other libraries like Pandas and Matplotlib knowledge will help to solve real-world problems.
Some basic mathematics like vector and metrics are plus point.
Representation of NumPy multidimensional arrays
Fig 1.1 Multidimensional NumPy arrays
The above figure 1.1 shows one dimensional (1D), two dimensional (2D) and three dimensional (3D) NumPy array
One Dimensional NumPy array (1D): It means the collection of homogenous data in a single row (vector).
Two Dimensional NumPy arrays (2D): It means the collection of homogenous data in lists of a list (matrix).
Three Dimensional NumPy arrays (3D): It means the collection of homogenous data in lists of lists of a list (tensor).
Why NumPy array instead of Python List ?
If you observe in Fig 1.1. To create a NumPy array used list. NumPy array and Python list are both the most similar. NumPy has written in C and Python. That’s a reason some special advantage over Python list is given below.
- Faster
- Uses less memory to store data.
- Convenient.
Why use NumPy for machine learning, Deep Learning, and Data Science?
Fig 1.2 NumPy for Machine Learning
To solve computer vision and MRI, etc. So for that machine learning model want to use images, but the ML model can’t read image directly. So need to convert image into numeric form and then fit into NumPy array. which is the best way to give data to the ML model.
Fig 1.3 NumPy for Machine Learning
Machine Learning model also used to solve business problems. But we can’t provide ‘.tsv’, ‘.csv’ files data to the ML model, So for that also need to use NumPy array.
In python NumPy tutorial at this movement, we have learned about Python NumPy Library theoretically but its time to do practicals.
Practical Session of Python NumPy Tutorial
How to install Python NumPy Library (package)?
To use the NumPy package first of all need to install it.
If you installed Anaconda Navigator and launched Jupyter Notebook or Spyder then no need to install NumPy. Anaconda Navigator installed NumPy itself. If you are using another IDE instead of Anaconda Navigator then follow below command in command prompt or terminal to install Python NumPy Library (Package).
1 |
|
While entering the above command, your system has an internet connection because ‘pip’ package download ‘numpy’ package and then install it. After successful installation, You are ready to take the advantages of the NumPy package.
How to import NumPy Library in IDE or How to use it?
To use NumPy first import it. For import NumPy, follows below syntax in the python program file.
1 |
|
-
import: import keyword imports the NumPy package in the current file.
-
as: as is a keyword used to create sort name of NumPy.
-
np: np is a short name given to NumPy, you can give any name (identifier) instead of it. If we use NumPy name in the program repeatedly so it will consume typing time and energy as well so for that we gave a short name for our convenience.
Flow the below syntax to create NumPy ndarray (multidimensional array)
How to create one dimensional NumPy array?
To create python NumPy array use array() function and give items of a list.
Syntax: numpy.array(object, dtype=None, copy=True, order=’K’, subok=False, ndmin=0)
1 2 3 |
|
1 |
|
How to create two dimensional NumPy array?
To create 2D array, give items of lists in list to NumPy array() function.
1 2 3 |
|
1 2 3 |
|
In this way, you can create NumPy ndarray
Let’s going forward to learn more in python NumPy tutorial.
How to check the type of ndarray?
The type() function give the type of data.
Syntax: type(self, /, *args, **kwargs)
1 |
|
1 |
|
How to check dimension of NumPy ndarray?
The ndim attribute help to find the dimension of any NumPy array.
Syntax: array_name.ndim
1 |
|
1 |
|
1 value represent, one_d_array array has one dimension.
How to check the size of the NumPy array?
The size attribute help to know, how many items present in a ndarray.
Syntax: array_name.size
1 |
|
1 |
|
4 value represent, total 4 item present in the one_d_array.
How to check the shape of ndarray?
The shape attribute help to know the shape of NumPy ndarray. It gives output in the form of a tuple data type. Tuple represents the number of rows and columns. Ex: (rows, columns)
Syntax: array_name.shape
1 |
|
1 |
|
The two_d_array has 3 rows and 3 columns.
How to the data type of NumPy ndarray?
The dtype attribute help to know the data type of ndarray.
Syntax: array_name.dtype
1 |
|
1 |
|
As per the above output one-d_array contain integer type data. This data store in 32 bit format (4 byte).
Up to here you can create and know about NumPy ndarray in python NumPy tutorial. Let’s know more something interesting.
Create metrics using python NumPy functions
Ones metrics use NumPy ones() function.
Syntax: np.ones(shape, dtype=None, order=‘C’)
1 |
|
1 2 3 4 |
|
Zeros metrics use NumPy zeros() function.
Syntax: np.zeros(shape, dtype=None, order=‘C’)
1 |
|
1 2 3 4 |
|
Empty metrics use NumPy empty() function.
Syntax: np.empty(shape, dtype=None, order=‘C’)
1 |
|
1 2 3 |
|
By default NumPy empty() function give float64 bit random value. According to your requirement change dtype.
Create NumPy 1D array using arange() function
Syntax: np.arange([start,] stop[, step,], dtype=None)
1 2 |
|
1 |
|
Create NumPy 1D array using linspace() function
Return evenly spaced numbers over a specified interval.
Syntax: np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0,)
1 |
|
1 |
|
Convert 1D array to multidimensional array using reshape() function
Syntax: np.reshape(a, newshape, order=‘C’)
1 2 |
|
1 2 3 4 |
|
Convert multidimensional array in one dimensional
To convert multidimensional array into 1D use ravel() or flatten() function.
Syntax: np.ravel(array_name, order=‘C’) or array_name.ravel(order=‘C’)
array_name.flatten(order=‘C’)
1 |
|
1 |
|
Transpose metrics
Syntax: np.transpose(array_name, axes=None) or
array_name.T
1 2 3 4 5 |
|
Conclusion
I hope, you learn each and every topic of python NumPy tutorial. This all topics important to do the project on machine learning and data science. Apart from the above-explained NumPy methods and operators, you can learn from numpy.org. This is an official website of python NumPy library. If you have fined any mistake in this tutorial of suggestions mention in the comment box.