NumPy stands for Numerical Python. It is a tool for mathematical computing and data preparation in Python. It can be utilized to perform several mathematical operations on arrays such as trigonometric, statistical, and algebraic routines. This library provides many useful features including handling n-dimensional arrays, broadcasting, performing operations, data generation, etc., thus, it’s the fundamental package for scientific computing with Python. It also provides a large collection of high-level mathematical functions to operate on arrays.
Why use NumPy..?
It is incredibly fast, as it has bindings to C libraries. In Python, we have lists that serve the purpose of arrays, but they are slow to process. Python list has fewer properties than the NumPy array. So using Numpy is more suggestible.
Why is NumPy Faster Than Lists..?
NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently. This behavior is called locality of reference in computer science. This is the main reason why NumPy is faster than lists. Also, it is optimized to work with the latest CPU architectures.
Numpy is surprisingly compact, fast, and easy to use, so let’s dive into the installation.
Installation of NumPy
If you have Python and pip already installed on a system, then the installation of NumPy is very easy.
Install it using this command:
C:\Users\Your Name>pip install numpyif |
If you have anaconda already installed in your machine then it is easy to install this package with conda run:
Type this command in Jupyter notebook:
conda install -c anaconda numpy |
Once NumPy is installed, import it in your applications
How to Import NumPy
We can import Numpy into our application by using import keyword
NumPy is usually imported under the np alias.
What is an alias: In Python alias are an alternate name for referring to the same thing.
Create an alias with the ‘as’ keyword while importing:
import numpy as np
Now the NumPy package can be referred to as np instead of numpy
Numpy has many different built-in functions and capabilities. We will focus on some of the most important aspects in Numpy Now let’s discuss the most important concept in numpy.
NumPy Arrays
So, what are arrays?
An array is a data structure consisting of a collection of elements, each identified by at least one array index or key.
NumPy is used to work with arrays. The array object in NumPy is called ndarray.
We can create a NumPy ndarray object by using the array() function.
The pre-built function np.array is the correct way to create an array.
Example:
import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr) print(type(arr)).
Output:
[1 2 3 4 5] <class ‘numpy.ndarray’> |
2-D Arrays
An array of arrays is known as 2D array. The two dimensional (2D) array is also known as matrix. A matrix can be represented as a table of rows and columns. Before we discuss more about two Dimensional arrays let’s have a look at the following example.
Example:
Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr) |
Output:
[[1 2 3]
[4 5 6]]
3-D Arrays
A 3D array is a multi-dimensional array(array of arrays). A 3D array is a collection of 2D arrays . It is specified by using three subscripts
Block size, Row size and Column size
Example
Create a 3-D array with two 2-D arrays, both containing two arrays with the values 1,2,3 and 4,5,6:
import numpy as np arr = np.array([[[1, 2],[4, 5]],[[1, 2],[4, 5]]]) print(arr)
Output:
[[[1 2][4 5]] [[1 2][4 5]]] |
Check Number of Dimensions?
NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array have.
Example:
Check how many dimensions the arrays have:
import numpy as np a = np.array(42) b = np.array([1, 2, 3]) c = np.array([[1, 2], [4, 5]]) d = np.array([[[1, 2], [4, 5]], [[1, 2], [4, 5]]]) print(a.ndim) print(b.ndim) print(c.ndim) print(d.ndim)
Output:
0 1 2 3 |
NumPy Array Indexing
Access Array Elements
Array indexing is the same as accessing an array element. You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1, etc.
Example:
import numpy as np a = np.array([1,2,3,4]) print(arr[2])
Output:
3 |
Access 2-D Arrays
To access elements from 2-D arrays we can use comma-separated integers representing the dimension and the index of the element.
The first element indicates the row and the second element indicates the column
Example:
Access the 2nd element on 1st dim:
import numpy as np arr = np.array([[1,2,3], [6,7,8]]) print(arr[0,2]) print(arr[1,2])
Output:
3 8 |
Negative Indexing
import numpy as np arr = np.array([[1,2,3,4,5], [6,7,8,9,10]]) print('Last element from 2nd dim: ', arr[1, -1])
Output:
Last element from 2nd dim: 9 |
Built-In Methods
Numpy allows us to use many built-in methods for generating arrays. Let’s examine the most used from those, as well as their purpose:
- np.arange( ) – array of arranged values from low to high value
- np.zeros( ) – array of zeros with specified shape
- np.ones( ) – similarly to zeros, array of ones with specified shape
- np.linspace( ) – array of linearly spaced numbers, with specified size
Arange
Numpy’s arange function will return evenly spaced values within a given interval. Works similarly to Python’s range() function. The only required parameter is ‘stop’, while all the other parameters are optional:
Example:
import numpy as np arr=np.arange(start=1, stop=10) print(arr)
Output:
array([1, 2, 3, 4, 5, 6, 7, 8, 9]) |
Zeros and ones
Numpy provides functions that are able to create arrays of 1’s and 0’s. The required parameter for these functions is ‘shape’.
Example for Zeros:
arr=np.zeros(shape=5) arr
Output:
array([0., 0., 0., 0., 0.]) |
Example for Ones:
arr=np.ones(3) arr
Output:
array([1., 1., 1.]) |
linspace
Numpy’s linspace function will return evenly spaced numbers over a specified interval. Required parameters for this functions are ‘start’ and ‘stop’.
Example:
arr=np.linspace(start=1, stop=100, num=5) arr
Output:
array([ 1. , 25.75, 50.5 , 75.25, 100. ]) |
Random Numbers in NumPy
Numpy allows you to use various functions to produce arrays with random values. To access these functions, first we have to access the ‘random’ function itself. This is done using ‘np.random’, after which we specify which function we need. Here is a list of the most used random functions and their purpose:
- np.random.rand() – produce random values in the given shape from 0 to 1
- np.random.randn() – produce random values with a ‘standard normal’ distribution, from -1 to 1
- np.random.randint() – produce random numbers from low to high, specified as a parameter
Rand
The rand function uses only one parameter which is the ‘shape’ of the output. You need to specify the output format you need, whether it is one or two-dimensional array. If there is no argument passed to the function, it returns a single value. Otherwise, it produces several numbers as specified
Example:
arr=np.random.rand(2) arr
Output:
array([0.66988993, 0.34697049]) |
Randn
The randn function is similar to the rand function, except it produces a number with standard normal distribution. What this means, is that it generates number with distribution of 1 and mean of 0, i.e. value from -1 to +1 by default:
Example:
arr=np.random.randn(2) arr
Output:
array([1.35689157, -0.36031165]) |
Randint
Randint is used to generate whole random numbers, ranging between low(inclusive) and high(exclusive) value. Specifying a parameter like ‘(1, 100)’ will create random values from 1 to 99.
Example:
arr=np.random.randint(1,10,5) // (start,stop,number of elements ) arr
Output:
array([7, 3, 9, 8, 1]) |
Reshape
This method allows you to transform a one-dimensional array to more dimensional, or the other way around. Reshape will not affect your data values. Let’s check out this code:
arr=np.arange(16)
array([7, 3, 9, 8, 1])
Now we can reshape this
ar=arr.reshape(4,4) array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]])
The array named ‘arr’ is now reshaped into a 4 by 4 matrix and by this, we can specify the number of rows and the number of columns. The key thing to notice is that the array still has all 16 elements.
These are all the basic functionalities off NumPy. We will discuss the rest of functionalities in another article
5 Responses