NumPy

numpy

Introduction

NumPy (or Numpy) is a Linear Algebra Library for Python, the reason it is so important for Data Science with Python is that almost all of the libraries in the PyData Ecosystem rely on NumPy as one of their main building blocks. Numpy is also incredibly fast, as it has bindings to C libraries.

It is highly recommended you install Python using the Anaconda distribution to make sure all underlying dependencies (such as Linear Algebra libraries) all sync up with the use of a conda install.

Installation Instructions

It is highly recommended you install Python using the Anaconda distribution to make sure all underlying dependencies (such as Linear Algebra libraries) all sync up with the use of a conda install. If you have Anaconda, install NumPy by going to your terminal or command prompt and typing:

conda install numpy

If you do not have Anaconda and can not install it, please refer to Numpyโ€™s official documentation on various installation instructions.

How to code using NumPy?

To use NumPy in our program, first, we have to import it. To do this, we have to write the following line:

import numpy as np

Convert a 1D list to a 1D array using NumPy:

Image description

Convert a 2D list to a 2D array:

Image description

numpy.arrange()

np.arrange() is like range() that we use in loops. We can write this in three ways:

arange(stop): Values are generated within the half-open intervalย [0,ย stop)ย (in other words, the interval includesย the startย but excludesย the stop).

Image description

arange(start,ย stop): Values are generated within the half-open intervalย [start,ย stop).

Image description

arange(start,ย stop,ย step)ย Values are generated within the half-open intervalย [start,ย stop), with spacing between values given byย step.

Image description

Zeros and Ones

np.zeros() return a new array of given shape and type, filled with zeros.

Image description

np.ones() return a new array of given shapes and types, filled with zeros.

Image description

numpy.linspace()

numpy.linspace() returns evenly spaced numbers over a specific interval.

Image description

numpy.eye()

It returns a 2-D array with ones on the diagonal and zeros elsewhere.

Image description

Create an array with random numbers

We can use the random module to create an array with random numbers.

Image description

Change the shape of an array

We can reshape our array by using reshape(). But we have to make sure that the dimension is the same.

Image description

Find the maximum and minimum of an array and their index

Image description

Find the shape and data type of an array

It will return a tuple with the dimensions of the array.

Image description

Array Indexing (1D Array)

NumPy array indexing is similar to list indexing.

Image description

There is a problem in slicing the array. To understand it better, letโ€™s see an example:

Image description

From the photo above, we can see that, when you edit c, it also affects b. It is happening because in the 3rd line we are not copying the array. Instead of copying, it only shows the live view of the array b. So, to fix this issue what we can do is we can use a method called .copy().

Image description

Now, changing c is not affecting b.

Array Indexing (2D Array)

This code will help you to understand 2D array indexing and slicing. Slicing is almost similar to the 1D array. Just you have to think row and column-wise.

Image description

We can also filter our data using conditions like these:

Image description

NumPy Operations

We can add two arrays like a normal variable and what will happen is all the elements will be added with the corresponding other array elements. Other operators will act like the same way.

Image description

Total
1
Shares
Leave a Reply

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

Previous Post
flip-stack-weekly-#53

FLiP Stack Weekly #53

Next Post
what-is-success?

What is Success?

Related Posts