Numpy
Numpy
Function
Module
Package
Library
SciPy :This useful library includes modules for linear algebra, integration, optimization, and statistics.
Pandas: Pandas is a library created to help developers work with "labeled" and "relational" data intuitively.
Matplotlib: Matplotlib helps with data analyzing, and is a numerical plotting library.
Pillow: Pillow is a friendly fork of PIL (Python Imaging Library), but is more user-friendly.
Keras: Keras is a great library for building neural networks and modeling.
PyTorch: PyTorch is a framework that is perfect for data scientists who want to perform deep learning
tasks easily.
TensorFlow :TensorFlow is a popular Python framework for machine learning and deep learning,
which was developed at Google Brain
Installation of NumPy
If you have Python and PIP already installed on a system, then installation of
NumPy is very easy.
Import NumPy
●
●
● Example:
○ import numpy
○ arr= numpy.array( [1,4,7,8])
○ print(arr)
●Example: ● Output:
import numpy [1,4,7,8]
arr= numpy.array([1,4,7,8])
print(arr)
print(a)
print(arr)
Example: : ndarray.shape
● Output:
import numpy as np
(2,3)
a =np.array([[1,2,3],[4,5,6]])
ie. 2rows and 3columns
print( a.shape)
Example: : ndarray.shape
● Output:
import numpy as np
(2,3)
a =np.array( [[1,2,3], [4,5,6]])
ie. 2rows and 3columns
print( a.shape)
(3,2)
b =a.reshape(3,2)
print( b) ie. 3rows and 2columns
Example: : ndarray.ndim
● Output:
import numpy as np
2
a =np.array( [[1,2,3], [4,5,6]])
ie. Its 2dimensional array
print( a.ndim)
Example: : ndarray.size
6
print( a.size)
ie. Total no of elements in array a
Example: : numpy.arange()
● Output:
import numpy as np
1
a =np.arange(12)
print( a.ndim) ie. Its 1dimensional array
● Output:
print(a )
[0 1234 567891011]
Values are generated within the half-open interval [start, stop) (in other
words, the interval including start but excluding stop).
import numpy as np
g=np.arange(2) [0 1]
print(g)
Example:
import numpy as np [2 4 6]
g=np.arange(2,8,2)
print(g)
print(a.sum() ) ● Output:
28
all zeros
all ones
[0 0 -5 0]
[0 0 0 66]]
Example
Generate a random integer from 0 to 100: Output:
94
from numpy import random
x = random.randint(100)
print(x)
Integers
The randint() method takes a size parameter where you can specify the shape of an array.
print(x)
Example
Generate a random float from 0 to 1:
import numpy.random as np
x = np.rand() Output:
0.2341
print(x)
print(o)
Exercise 2:
[[1,2,3], [[ 1 2 3 7 8 9] [ 4
[4,5,6]] 5 6 10 11 12]]
o=np.hstack((a1, a2))
[[7,8,9], print(o)
[10,11,12]] Desired Array
Input Array
Exercise :3
Generation of given count of equally spaced numbers within
a specified range
Output a sequence of equally gapped 5 numbers in the range 0
to 100 (both inclusive)
print(o)
Desired Output
Exercise :4
Matrix Generation with one particular value
Output a matrix (numpy array) of dimension 2-by-3 with each
and every value equal to 5
o= np.full((2, 3), 5)
print(o)
[[5 5 5]
[5 5 5]]
o= np.ones((2,3))
Desired Output 0=a*5
print(o)
5. How to get the common items between two
python numpy arrays?
Input:
A= np.array([1,2,3,4,5,6,7])
B=np.array([2,5,7,8,9])
Desired Output:
print(np.intersect1d(A,B))
[2,5,7]
6. How to remove from one array those
items that exist in another?
Input:
a =
np.array([1,2,3,4,
5])
b =
Dnpes.iraerdrOauyt(pu[t5:,6,7,8#, From 'a' remove
all of 'b'
a9r]r)ay([1,2,3,4])
np.setdiff1d(a,b)
#> array([1, 2, 3,
4])
Department of Computer Engineering and Information Technology
College of Engineering Pune (COEP)
Forerunners in Technical Education