Numerical Analysis using Numpy
By: Saurav Anand
Tags: Data Science

“There is geometry in the humming of the strings , there is music in the spacing of spheres”-Pythagoras

Are you a Data science Aspirant ? Have you ever installed python and wondered what additional package needs to be installed?

You are not alone…..!!

There are many fundamental packages which are used with python , Numpy is one of them. Anyone who wants to get into the field of Machine learning and AI , needs to have thorough understanding of Numpy package.

In this blog , we will cover the following topics

Introduction of NumPy How to install NumPy Importing the NumPy package in python NumPy Arrays NumPy indexing and selection NumPy Operations NumPy Array Input and Output

NumPy is the fundamental package for scientific computing with Python. It contains among other things:

a powerful N-dimensional array object

sophisticated (broadcasting) functions

tools for integrating C/C++ and FORTRAN code

useful linear algebra, Fourier transform, and random number capabilities

NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.

NumPy is licensed under the BSD license, enabling reuse with few restrictions.

PART 2: How to install Numpy

It is strongly recommended that we install install Python using the Anaconda distribution to make sure all underlying dependencies work with conda install. If you have already installed Anaconda, install NumPy by opening your terminal(for MAC Users) or ANACONDA Command Prompt(for Windows Users) and type the following command -

conda install numpy

PART 3:-Importing the NumPy package in python

After successfully installing the Numpy package , we can import it using the following code :-

PART 4:- NumPy Arrays

Numpy built in functions are very useful for the Data scientists ad python programmers. We shall cover some of the most important topics in Numpy:

Arrays ( using Vectors & Matrices )

Number Generation Concepts

Numpy arrays are most widely used by the data scientists:-

Vectors are 1-dimensional arrays and matrices are 2-dimensional arrays(A Matrix can still possess a single row or a column).

Let’s first see how to create NumPy arrays

a) Creation of simple Array

We could create a simple Array by using a list of values or a list of “List of values”.

Using the array() method , we can convert the list into the array

b) Creation of Array from list of lists

There are multiple built-in methods to generate Arrays

c) arange

It returns evenly spaced values within a given interval as input.

d) 0’s and 1's

Generate arrays of 0’s or 1's

e) linspace

Return evenly spaced numbers over a specified interval.

Note :- The difference between arange and linspace is that in case of later the stop value is considered.

f) eye

Return a 2-D array with ones on the diagonal and zeros elsewhere. Also called an identity matrix

Random

Numpy array has lot of options to create random number arrays :-

a) rand

Create an array of the given shape and populate it with random variables derived from a uniform distribution between [0, 1) .

b) randn

Return a variable (or a set of variables) from the “Standard Normal” distribution. Unlike rand which is from a uniform distribution:

A standard Normal Distribution has mean 0 and SD of 1 as we know.

c) randint

Return random integers from low (inclusive) to high (exclusive).

Array Attributes and Methods for an array

Let us look at some important attributes and methods for an array.

a) Reshape

Returns an array containing the same data with a new shape.

b) max,min,argmax,argmin

These are useful methods for finding max or min values. Or to find their index locations using argmin or argmax

c) Shape

Shape is an attribute that arrays have. It is not a method.

d) dtype

We could retrieve the data type of the object in an array using dtype.

PART 5:-NumPy indexing and selection

We will now learn how to select objects or groups of objects from an array

Using brackets for Indexing and selection

Broadcasting

NumPy arrays have the capability of Broadcasting values as seen below

Setting a fixed value using index range

Reset the array

Sub-setting an array

Changing the value in subset

Please Note that the changes in the subset has affected the original sample_array as well

Data is not copied when we subset, it is a sub-view of the original array

This is a feature by default in order to avoid memory problems

Indexing a Matrix — 2 dimensional arrays

The general formats used are

sample_matrix[row][col]

or

sample_matrix[row,col]

Indexing Rows

Finding Individual element from the matrix

Slicing Matrix

Extracting value from a particular row

Custom Indexing of Matrix

Custom or Fancy indexing allows you to select entire rows or columns specifically in interest.

Selection

Using brackets for selection based on operators for comparison

PART 6:-NumPy Operations

Arithmetic Operations

Universal Array Functions

Numpy has numerous universal array functions. They are mathematical operations that can be performed to the entire array.

Calculate the Square root

Calculate the exponential values

Calculate Max and Index of the maximum value

Trigonometric functions

Logarithm functions

Creating a matrix with decimal numbers

Rounding the values up to two decimals

Rounding the values up to zero decimal places

Calculate std. deviation, variance and mean

Unique function

Why do we see cricket twice in above output??

Part 7:- Numpy Array Input and Output

Create an array and save it on our hard disk

Change the array created above

The original saved copy will remain the save

Saving multiple files into a zip file

Loading multiple arrays using load

Saving the arrays into text files

Conclusion

Numpy allow a wide range of operations which can be performed and used for the numerical analysis of the data to derive relevant insights .

Happy Reading …!!

Saurav Anand

Github:- https://github.com/Saurav6789

LinkedIn:- https://nl.linkedin.com/in/saurav-anand-92229584