I will walk you through the basics of NumPy. If you want to do machine learning then knowledge of NumPy is necessary. It one of the most widely used Python libraries. It is the most useful library if you are dealing with numbers in Python. NumPy guarantees great execution speed compared to standard Python libraries. It comes with a great number of built-in functions.
Advantages of using NumPy with Python:
First, let’s talk about its installation. NumPy is not part of the basic Python installation. We need to install it after the installation of Python in our system. We can do it by the pip using command, pip install NumPy
, or by installing Conda.
We are done with the installation and now we can jump right into NumPy. First, let’s start with the most important object in NumPy, the ndarray or multi-dimensional array. A multi-dimensional array is an array of arrays. In multi-dimensional arrays, this array, [1,2,3]
, is a one-dimensional array because it contains only one row. The below is array is a two-dimensional array, as it contains multiple rows as well as multiple columns.
[[1 2 3][4 5 6]
[7 8 9]]
Let’s do some coding now. Here I am using Jupyter Notebook to run my code; you can use any IDE available and best suited to you.
We start with import NumPy
.
In the following code, I am renaming the package to np
for convenience sake.
import numpy as np
Now, in order to create an array in NumPy, we use its array function as shown below:
array = np.array([1,2,3])print(array)
Output: [1 2 3]
This an example of a one-dimensional array.
Another way to create an array in NumPy is by using the zeros
function.
zeros = np.zeros(3)print(zeros)
Output: [0. 0. 0.]
If you look closely at the output, the generated array contains three zeros, but the type of the value is a float and, by default, NumPy creates the array of float values.
type(zeros[0])Output: numpy.float64
Going back to the first example inside NumPy’s array
function, we pass a list so we can also pass the list
variable inside the array
function and the output will be the same.
my_list = [1,2,3]array = np.array(my_list)
print(array)
Output: [1 2 3]
Now, let’s look into how to create a two-dimensional array using NumPy. Instead of passing the list now we have to pass a list of tuples or list of lists as mentioned below.
two_dim_array = np.array([(1,2,3), (4,5,6), (7,8,9)])print(two_dim_array)
Output:
[[1 2 3]
[4 5 6]
[7 8 9]]
Note that the number of columns should be equal, otherwise NumPy will create an array of a list.
arr = np.array([[1,2,3], [4,6], [7,8,9]])print(arr)
Output: [list([1, 2, 3]) list([4, 6]) list([7, 8, 9])]
Now, to create an array of a range, which is very good for making plots, we use the linspace
function.
range_array = np.linspace(0, 10, 4)print(range_array)
Output: [ 0. 3.33333333 6.66666667 10. ]
Here, the first argument is the starting point and next is the endpoint and the last argument defines how many elements you want in your array.
Now, to create random arrays we can use the random
function. Here, I’ve created an array of random integers, and, therefore, used randint
where first I specified the maximum value and then the size of my array.
random_array = np.random.randint(15, size=10)print(random_array)
Output: [ 7 11 8 2 6 4 9 6 10 9]
Now we know the basics of how to create arrays in NumPy. Now let’s look into some of its basic operations. First, we will start by finding the size and shape of an array. Size will give the number of elements in an array whereas shape will give us the shape of an array.
For a one dimensional array, the shape would be (n, )
, where n
is the number of elements in your array.
For a two dimensional array, the shape would be (n,m)
, where n
is the number of rows and m
is the number of columns in your array
print(array.size)Output: 3
print(array.shape)
Output: (3,)
print(multi_dim_array.size)
Output: 9
print(multi_dim_array.shape)
Output: (3, 3)
If we want to change the shape of an array we can easily do it with the reshape
function. It will look like something like this:
two_dim_array = np.array([(1,2,3,4), (5,6,7,8)])two_dim_array = two_dim_array.reshape(4,2)
print(two_dim_array)
Output:
[[1 2]
[3 4]
[5 6]
[7 8]]
We need to make sure that the rows and columns can be interchangeable. For example, here, we can change rows and columns from (2,4) to (4,2) but can not change them to (4,3) because, for that, we’d need 12 elements and we have only 8. Doing so will give an error as shown below.
ValueError: cannot reshape array of size 8 into shape (4,3)
To check the dimensions of our array. we can use the ndim
function.
print(two_dim_array.ndim)Output: 2
Now, to get values from an array, a process known as slicing can be done in various ways. For example, array[1]
will fetch the second element of my array, but if we want a range we can use array[0:1]
, which will give us the first two elements. For the last value of the array, we can use array[-1]
, which is similar to the standard method of getting elements from a list in Python.
Now to find the sum all we have to use is the sum()
, function but if we want to find the sum of the axis we can pass an argument for the axis.
print(two_dim_array.sum(axis=0))Output: [ 6 8 10 12]
print(two_dim_array.sum(axis=1))
Output: [10 26]
Now to add two arrays all we have to use if + operator. For example:
print(two_dim_array + two_dim_array)Output:
[[ 2 4 6 8]
[10 12 14 16]]
Similarly, we can use other operands as well, like multiple, subtract, and divide.
We have many other operations present in NumPy like sqrt
, which will give us the square root of every element, and std
, which is used to find the standard deviation. To explore more about these operations visit the NumPy’s documentation.
And that’s it for the introduction of NumPy.
☞ Learn Programming with Python Step by Step
☞ MySQL Databases With Python Tutorial
☞ Creating Web Sites using Python and Flask
☞ Complete Python: Go from zero to hero in Python
☞ An A-Z of useful Python tricks
☞ A Complete Machine Learning Project Walk-Through in Python
☞ Learning Python: From Zero to Hero
☞ MongoDB with Python Crash Course - Tutorial for Beginners
☞ Introduction to PyTorch and Machine Learning
Originally published by Prabhat Kashyap at https://dzone.com
#numpy #python