NumPy is an open-source code library in python that is used to work with arrays. The full form of NumPy is Numerical Python. NumPy library contains several array objects and methods that can perform mathematical and logical operations on the arrays.

Installing and Importing NumPy

Before working with the NumPy library, we first have to install and import it in our scripts or jupyter notebooks.

To install the NumPy library in the python environment, we’ll use the python package manager pip.

pip install numpy

Or, if we want to install it in an anaconda environment, we’ll install it using the conda package manager.

conda install numpy

Next, we import it in our jupyter notebook or python script with an alias ‘np.’

import numpy as np

Creating Arrays

With NumPy, we can easily create single and multidimensional arrays. There are various ways in Numpy to create and initialize arrays.

Numpy.arange - This method creates an array with incrementing values. This function takes some parameters like start number, end number, incrementing step value, and datatype.

import numpy as np

# creating an array with the first six natural numbers array1 = np.arange(6) print(array1, "\n")

# creating an array with 2 unit difference between integers # the given array starts from 4 and ends on 16 array2 = np.arange(4, 16, 2) print(array2, "\n")

# creating an array of float datatype numbers array3 = np.arange(4.2, 5, 0.2, dtype='float') print(array3, "\n")

Output

[012345]

[ 468101214]

[4.24.44.64.8]

Numpy.eye - We use the eye method to create 2-Dimensional arrays in python. The parameters in this function are the rows and the columns of the matrix. The elements where row and column index are identical (i.e., i=j) are 1, and the rest are 0.

import numpy as np

# creating a 2-D array of 3x3 shape # if we give only one parameter, it constructs a square matrix of the given length array1 = np.eye(3) print(array1, "\n")

# creating a 2-D array with four rows and six columns array2 = np.eye(4, 5) print(array2, "\n")

In terms of physical properties, each NumPy array has a shape, a size, and a dimensional value.

NOTE: We refer to the arrays in NumPy as ndarray (n-dimensional array). It is a homogenous object in NumPy, which provides us with the methods and functions to operate on the arrays.

To get the dimensions or the axes of the array, we use the ndim property of the ndarray class.

print(array_1d.ndim, "\n")

print(array_2d.ndim, "\n")

print(array_3d.ndim, "\n")

Output

1

2

3

Shape

The shape of the array is the number of elements stored in each dimension. A 2-D array with three rows and four columns will have a shape of (3, 4). We use the shape property of the ndarray class to get the shape of an array.

print(array_1d.shape, "\n")

print(array_2d.shape, "\n")

print(array_3d.shape, "\n")

Output

(3,)

(2, 6)

(2, 2, 4)

Size

The size of the array is the total number of elements stored in the array. The size property of the ndarray class gives us the size of the array.

print(array_1d.size, "\n")

print(array_2d.size, "\n")

print(array_3d.size, "\n")

Output

3

12

16

Elementary functions on Arrays

Sort

There is a sort function in the ndarray class to sort the elements of an array.

import numpy as np

# Using the sort function to sort the elements of the one-dimensional array. array1 = np.array([12, 1, 60, 8, 100, 16]) sorted_array1 = np.sort(array1) print("Sorted Array:\n", sorted_array1, "\n")

# Using the sort function to sort the elements of the two-dimensional array array2 = np.array([[12, 4, 6], [1, 0, 34]]) sorted_array2 = np.sort(array2, axis=0) print("Column Sorted 2-D Array:\n", sorted_array2, "\n")

What is the difference between a list and an array in Python? A python list can contain different data types in a single list, whereas all the elements in an array are homogenous. When compared to a list, an array is faster and takes up less memory space.

How to install NumPy and import it in python? To install the NumPy library in our system, we use the python package installer PIP or conda if we use the Anaconda environment.

pip install numpy or conda install numpy installs the numpy library in our environment.

To import it into our script, we have to write the import keyword along with an alias import numpy as np.

What is the rank of an array? The rank of an array is determined by the number of axes or the number of dimensions of the array. So, for a one-dimensional array, the rank will be one; for a two-dimensional array, it will be two, and so on.

How can I reshape the array? In python, we can reshape any array with a simple syntax. The reshape() function takes the new dimensions (newshape) as the parameter and then changes the array’s shape without disturbing any values.

What are the advantages of NumPy? NumPy supports the OOPS approach. For instance, the ndarray is a class that contains several properties and functions to make changes in the array. Numpy is much faster than, and the code written using NumPy more closely resembles standard mathematical notation (making it easier, typically, to correctly code mathematical constructs).

Key Takeaways

Congratulations on making it this far. In this blog, we discussed a fundamental overview of the NumPy library.

We saw how to install and import NumPy in our python program. Then we discussed some methods to create arrays using NumPy. We learned how to identify the physical attributes of the array using NumPy, and lastly, we saw some essential functions to deal with arrays using NumPy.

If you are preparing for the upcoming Campus Placements, then don’t worry. Coding Ninjas has your back. Visit this link for a carefully crafted and designed course on-campus placements and interview preparation.