NumPy in Python

  • date 2nd August, 2019 |
  • by Prwatech |
  • 0 Comments

 

Numpy Tutorial in Python

 

Numpy Tutorial in Python, Welcome to the world of Python NumPy Tutorial. Are you the one who is looking forward to knowing the Python NumPy? Or the one who is very keen to explore the NumPy tutorial in Python with examples that are available? Then you’ve landed on the Right path which provides the standard information of Python NumPy Tutorial.

 

What is NumPy in Python?

NumPy is an array-processing package. It provides a multidimensional array object and tools for working with these arrays with high-performance.

 

Features of Numpy in Python

 

1. A powerful N-dimensional array object
2. Sophisticated (broadcasting) functions
3. 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 using Numpy which allows NumPy to seamlessly and speedily integrate with a large variety of databases.

 

Installation of Python Numpy

 

Installation of Numpy on Mac

 

Step1)Open the terminal
Step2)pip install numpy

 

Installation of Numpy on windows

 

Step1) Go to the File menu
Step2) Go to settings
Step3) Go to Project
Step4) Go to project Interpreter
Step5) Click on ‘+’ icon
Step6) Type numPy.
Step7) Select it and install it.
Step8) import numpy as n
Step9) Use it

 

 

Properties of Numpy

 

Arrays in NumPy: NumPy’s mainly used for homogeneous multidimensional array.

1. It is a table kind structure consisting of elements, having a similar data type, indexed by a tuple of positive integers.
2. In NumPy dimensions are known as axes. The number of axes is rank.

Ex) [[11,22,33],
[44,55,66]]
Here,
rank= 2 (as it is two dimensional or you can say it has 2 axis)

 

How to implement Numpy

 

Ex) import numpy as n
a=n.array([2,3,4])
print(a)

Numpy Arrays in Python

 

Arrays are of 2 types

 

Single Dimension Arrays: Arrays having only one dimension i.e. only a row or only a column.

 

Ex)  import numpy as n
a=n.array([1,8,6])

 

Multi Dimension Arrays: Array having more than one dimension is known as multi-dimension arrays.

 

Ex) import numpy as n
a= n.array([1,2,4],[2,5,7],[7,8,9],[1,2,4])

1. It occupies Less Memory.
2. It is a pity Fast as compared to List
3. It is also convenient to use Convenient

 

Numpy Operators in Python

 

ndim

 

It is used to find the dimension of the array, i.e. whether it is a two-dimensional array, five Dimension array or a single dimensional array.

Ex) import numpy as np
a = np.array([(1,2,3),(4,5,6)])
print(a.ndim)

 

item size

 

It is used to calculate the byte size of each element.

Ex) import numpy as np
a = np.array([(1,2,3),(4,5,6)])
print(a.itemsize)

 

dtype

 

It is used to find the data type of the elements stored in an array.

Ex) import numpy as np
a = np.array([(1,2,3),(4,5,6)])
print(a.dtype)

 

Reshape

 

It is used to change the number of rows and columns to give a new view to an object.

Ex) import numpy as np
a = np.array([(1,2,3),(4,5,6)])
print(a)
a=a.reshape(3,2)
print(a)

 

Slicing

 

Slicing is actually extracting a particular set of elements from an array.

Ex) import numpy as np
a = np.array([(1,2,3),(4,5,6),(11,12,13)]),
print(a[0,2])          #output:      3
print(a[0:,2])         #output       3,5
print(a[0:2,1])       #output       3,6,13

 

linespace

 

It returns evenly spaced numbers over a specific interval.

Ex) import numpy as np
a = np.linespace(1,3,10)
print(a)

 

max()

 

It returns the maximum number from a given array.

Ex) import numpy as np
a = np.array([(1,2,3),(4,5,6)])
print(max(a))

 

min()

 

It returns the minimum number from a given array.

Ex) import numpy as np
a = np.array([(1,2,3),(4,5,6)])
print(min(a))

 

sum()

 

It returns the sum of numbers from a given array.

Ex) import numpy as np
a = np.array([(1,2,3),(4,5,6)])
print(sum(a))

 

sqrt()

 

It returns the square root of the numbers from a given array.

Ex) import numpy as np
a = np.array([(1,2,3),(4,5,6)])
print(sqrt(a))

 

std()

 

It returns the standard deviation of the numbers from a given array.

Ex) import numpy as np
a = np.array([(1,2,3),(4,5,6)])
print(std(a))

 

Additional operator

 

Used to add elements of 2 arrays

Ex) import numpy as np
a = np.array([(1,2,3),(4,5,6)])
b = np.array([(5,2,6),(8,4,6)])
print(a+b)

 

Subtraction operator

 

Used to substract elements of 2 arrays

Ex) import numpy as np
a = np.array([(1,2,3),(4,5,6)])
b = np.array([(5,2,6),(8,4,6)])
print(a-b)

 

Division operator

 

Used to divide elements of 2 arrays

Ex) import numpy as np
a = np.array([(1,2,3),(4,5,6)])
b = np.array([(5,2,6),(8,4,6)])
print(a/b)

 

Vertical & Horizontal Stacking

 

If you want to concatenate two arrays but not add them, you can perform it using two ways – vertical stacking and horizontal stacking.

Ex) import numpy as np
a = np.array([(1,2,3),(4,5,6)])
b = np.array([(5,2,6),(8,4,6)])
print(np.vstack((x,y)))
print(np.hstack((x,y)))

 

Ravel

It converts an array into a single column.

Ex) import numpy as np
a = np.array([(1,2,3),(4,5,6)])
print(a.ravel())

Leave a Reply

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

Quick Support

image image