What is NumPy?
If you are to learn data science or machine learning, you need to know NumPy (numerical python). It is an open-source core library for scientific computing. You can also say it is a library available in python for scientific computing.
Numpy is one of the most useful libraries, especially when you are crunching numbers. Why is it so good? Because you can crunch numbers much faster rather than using python loops, and it has got many built-in functions, and it also works perfectly with matrice multiplication.
NumPy is written in C, which is the low-level language, in turn, makes its processing speed faster. Operations in NumPy are faster because they take advantage of parallelism i.e., single instruction multiple data (SIMD).
It also provides :
- an n-dimensional array of objects.
- provides tools for integrating C, C++.
- contains useful mathematical functions, linear algebra, Fourier transforms, and random number capabilities, sorting, and shape manipulation.
- can be used as a multidimensional container for generic data.
- use matplotlib with NumPy, which is used for data analysis and for making graphs.
Why use NumPy?
It provides an alternative to the regular python list. You can analyze the data much more efficiently than using python lists. It uses less memory space compared to lists so it can work with a vast amount of data. It can also work with SciPy and other Libraries available.
If you want to work with large datasets, there will be some operation you will need to perform, for example, operations on vectors, matrix manipulation, n-dimensional arrays, and regressions as well. In traditional python, these can be non-trivial to execute. With NumPy, they come out-of-the-box, which are already optimized and tested.
Moreover, you can use NumPy lists as NumPy array, and also you use files as NumPy array. With NumPy, it is convenient to work with statistical data, matrix multiplication, reshaping, and other machine learning packages. TensorFlow and scikit-learn use NumPy arrays to compute the matrix multiplication in the back end.
Using NumPy, you can work with image processing and computer graphics as images are represented as multidimensional arrays of numbers and can perform various operations on images using NumPy, such as mirroring the image, rotating, and sharing images.
Getting started
Before starting with NumPy, you must know python lists.
Python list is somewhat like an array in other languages; it is a collection of heterogeneous types of values that we can add, delete or manipulate. The elements are enclosed within square brackets[].
You can create a Python list by writing the list name and the elements assigned within square brackets.
Example: t = [2,4,8,45]
Python list has some drawbacks python NumPy overcomes these drawbacks.
For example:
If you try to multiply two lists, it will give an error.
This is because it has no idea how to do this calculation with the list.
To overcome this, you have to multiply the elements of both the lists by accessing them from their index number, which is a lengthy and time-consuming process.
To overcome such type of problems, you have to use NumPy.
For example, using NumPy, you can multiply the elements without writing the index numbers again and again.
How to use NumPy?
There are several IDE’s where you can run python code, for example, pycharm, Jupiter notebook, google colab, etc.
I’ll be using google colab.
Installation
You can install NumPy using pip.
After installing, you need to import NumPy.
Operations:
There are several operations you can perform using NumPy. We will learn each operation one by one in this section.
Creating an array
- You can create and enter elements directly.
Here you just need to write an array name and assign the elements.
- You can also use the list as an array.
In this example, I made a list “a” and assigned that list as an input to the NumPy array “z.”
- Create 2D arrays
- Create zero’s array.
You can create an array of any length with the elements as 0’s.
In this example, I have created an array “a” with a length of 3 elements.
- Create one’s array.
You can also create an array of any length with the elements as 1’s.
We can also see the type (data type) of the array:
We can also see the type (data type) of the element:
You can check the shape of the array:
- We can make an array by giving starting point, ending point and number of elements
In this example, 2 is the starting point, 10 is the ending point, 5 is the number of elements. It’s useful while plotting a graph, for example, plotting the x-axis.
- You can create a random array using random.randint(limit, size) method
Get elements of an array:
- Get specific elements (using index number) from the array.
- To get elements from within a range.
- To get the last element.
Mathematical operations with NumPy:
- Addition: you can simply add elements of two arrays.
You can just add to each element:
Similarly, you can multiply 10 with each element:
- Multiplication: you can multiply two arrays.
- Dot product: you can find the dot product of two arrays using the “@” symbol.
- Sort: you can sort an array:
- To get the values of array less than a number say 3,
Here, you will get the values true or false. If the value is less than 3 you will get true, else false.
- To get the values of array greater than a number say 3
- If you want an array which is greater than 3
- Photos with NumPy:
- you can read a photo as a NumPy array:
- You can check the type of picture:
- You can check the shape of the image by passing the array name.
Here, 165 is the number of rows in an image, 220 is the number of columns in a picture, and 3 is the color of the image with the 3 channels RGB(red, green, blue).
- To view an image:
- To make the image upside down:
Here first colon (:) is the starting point second colon (:) is the stop point, and -1 is the step. -1 is for backward and +1 is for forwarding.
- To mirror an image
- To crop an image, you have to provide a range
- You can also take every other row or column.
- You can also perform mathematical functions on the image array.
Such as for :
Sin: print(np.sin(img))
Sum: print(np.sum(img))
Product: print(np.prod(img))
Minimum: print(np.min(img))
Maximum: print(np.max(img))
Argmax: print(np.argmax(img))
Argmin: print(np.argmin(img))
Click here to deploy your AI workloads on E2E GPU Cloud.