1.
Introduction
2.
Installing and Importing NumPy
3.
Creating Arrays
3.1.
Using Python Lists
3.2.
Using Functions available in the NumPy Package
3.3.
General Functions to create arrays
4.
Physical attributes of an Array
4.1.
Dimension
4.2.
Shape
4.3.
Size
5.
Elementary functions on Arrays
5.1.
Sort
5.2.
Unique
5.3.
Reverse
6.

7.

8.

9.

10.

11.

12.

13.

14.

15.
16.
Key Takeaways
Last Updated: Mar 27, 2024

# NumPy Basics

Basics of Machine Learning
Free guided path
9 chapters
29+ problems

## Introduction

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.

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

Next, we import it in our jupyter notebook or python script with an alias â€˜np.â€™

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Creating Arrays

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

Output

### Using Functions available in the NumPy Package

• 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.

Output

• 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.

Output

• Numpy.diag - To create a matrix with the elements along the diagonal, we use the diag function from the Numpy Library.

Output

Output

## Physical attributes of an Array

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.

### Dimension

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

Output

### 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.

Output

### 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.

Output

## Elementary functions on Arrays

### Sort

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

Output

### Unique

We can get the unique values from the array using the unique method of the ndarray class in NumPy.

Output

### Reverse

To reverse an array, we can use the flip function of the ndarray class. The flip function reverses the elements of the specified axis.

Output

1. 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.

2. 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.

3. 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.

4. 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.

5. 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.

Check out this problem - Search In Rotated Sorted Array

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.

Guided path
Free
Basics of Machine Learning
9 chapters
29+ Problems