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.
NumPy is an array-processing package. It provides a multidimensional array object and tools for working with these arrays with high-performance.
1. A powerful N-dimensional array object
2. Sophisticated (broadcasting) functions
3. Tools for integrating C/C++ and Fortran code
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.
Step1)Open the terminal
Step2)pip install numpy
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
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)
Ex) import numpy as n
a=n.array([2,3,4])
print(a)
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
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)
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)
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)
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 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
It returns evenly spaced numbers over a specific interval.
Ex) import numpy as np
a = np.linespace(1,3,10)
print(a)
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))
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))
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))
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))
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))
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)
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)
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)
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)))
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())