Numpy | Python Library

Every Data Scientists/ Data enthusiasts Favorite library is NumPy and the most used library. Every enthusiast while performing a Data science problem. this is the library they will be using to perform plenty of tasks and majorly for mathematical calculations/operations. 

In general – NumPy library is the most popular library and fundamental library in python. When we deal with a large number of datasets NumPy handles very well in this type of scenario. Before stepping into the data science world, we should have a solid grip over NumPy library.

Before exploring the NumPy library,let’s have a quick look over the concepts called Lists, Array, Vectors and Matrices.

Lists

As the name suggests, list is an ordered sequence of data. Declaring a list, using square braces [ ] and separated items by columns.

For Ex

[ ‘Ajay ’ , ‘1234’ ]

Arrays

Similar to the list, array is one of the data structures which can hold data more than one value at a time. Array is an ordered collection of data elements with similar data type.

For ex

[ 1, 2, 3 , 4, 5] —- integer array

[ ‘a’, ‘B’, ‘c’ ] —- is an array of strings

Vector

In programming, a vector is a type of array that is one dimensional. In other ways, one dimensional array is called a vector.

For ex

[1, 2, 3,4]—- is an array of integers and also a vector representation. 

Matrix

A matrix is the one which is represented in the format of rows and columns (i, e numbers)

For ex

     A = [ 1  2  3

             4  5  6]

From the above example, the matrix has 2 rows and 3 columns

What is NumPy?

NumPy is an open source library and Numpy stands for Numerical imputations/calculations in python. NumPy arrays are the one which is mainly used in NumPy libraries. When we compared with the list, NumPy library is quite fast because it has binding with the C programming language.

NumPy arrays are N-dimensional arrays and can store elements of the same type and size. We have other libraries like pandas, matplotlib and sklearn which are built using the library.

Why NumPy?

 1. It is mainly used for numerical type of data in python.

 2. For numerical computations, it is easier and faster.

 3. It consumes less memory.

How to Import NumPy Library?

In anaconda navigator, no need of installation since it comes with pre-installation. For other IDE’s you have to just run the below code in the command prompt.

Pip install numpy

When it comes to importing the library, we can load/import the NumPy library in two ways

1. Import numpy

2. Import numpy as np

Note: np is the shorthand for numpy and also simply serves as an alias of the library

Array in NumPy

Like we have seen the definition of list and array above, array is the data structure An array in NumPy can hold the collection of ordered elements of similar data type.

We have heard arrays as ndarray or we can say that N – dimensional array. An N – dimensional array is an array with n number of dimensions.

For example,

1-D or 1- Dimensional Array –  [1, 2, 3 ,4]

2-D or 2- Dimensional Array –  [1  2

                                                     3  4]

3-D or 3- Dimensional Array  – [1  2  3

                                                    5  6  7

                                                    8  9  10]

          Pic credits: Google

Creating Numpy Array from Python List

 1. Create a python list

2. Import numpy library

3. Pass the python list into array ( Using np.array() )

Let’s compare python list and NumPy array

1. In python list, we can contain data elements of different data types whereas in arrays, we contain data elements of the same data-type.

2. When we deal with time consumption and memory usage, python list takes a huge amount of time and memory whereas in arrays, time taken in numerical imputations is very less and memory usage is also low.

Basic methods on arrays and reshaping

Adding, removing and slicing

1. Adding an element

Using append function to add new element

2. Removing an element

You can remove an element from the array using delete function

3. Sorting an array

Here, we are sorting an array in the ascending order. In this we are using .sort() to sort the NumPy array to sort in ascending order.

Reshaping an array

Can we reshape an array?Yes, we can reshape an array or an list

1. Using np.reshape() we can reshape/ change the array.

2. Majorly, there won’t be any changes in the original data other than shape of the data.

Indexing and slicing

Indexing

1. Positive Indexing

  •  Starts from the leftmost element
  • 0 is the first index till n-1

For example,  x= [1,    2,     3]

                  Index  0      1     2 

From the above code, we have array values 1, 2, 3. If we want to get the third element then we have to use square braces with index values and pass it in our code

arr[2], gives the value 3. We can use this concept in nested lists also.

2. Negative Indexing

  • Starts from the right.
  • -1 is the first index.

For example,  x= [1,    2,     3]

                  Index -3    -2     -1

From the above code, we have array values 1, 2, 3. If we want to get the third negative  element then we have to use square braces with index values and pass it in our code

arr[-3], gives the value 3. We can use this concept in 2-d and N-d also.

Combine and split an array

1. Combine an array

2. Split an array

Endnotes:

Yes, We have almost covered important topics and there are a lot more concepts to learn. This blog just gives an overview of NumPy arrays and try to explore more concepts with hands-on experience.

Read the official documentation here

Thanks for reading!

article by: Krishna Heroor

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *