0% found this document useful (0 votes)
13 views5 pages

Unit IV Part5

The document provides an overview of various NumPy functionalities, including array slicing, broadcasting methods like eye(), zeros(), and ones(), and array joining techniques such as concatenate(), hstack(), and vstack(). It also discusses how to create subsets of arrays using split(), hsplit(), and vsplit(), along with statistical functions like mean(), std(), var(), sum(), and prod(). Examples are included to illustrate the usage of these functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Unit IV Part5

The document provides an overview of various NumPy functionalities, including array slicing, broadcasting methods like eye(), zeros(), and ones(), and array joining techniques such as concatenate(), hstack(), and vstack(). It also discusses how to create subsets of arrays using split(), hsplit(), and vsplit(), along with statistical functions like mean(), std(), var(), sum(), and prod(). Examples are included to illustrate the usage of these functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

A[: :4, : :3]

11 2 3 56 14
40 52 16 12 20
70 8 9 32 22
18 30 17 44 49
25 55 66 78 82

Negative Index
(-2th row ), (-5th column and -3rd 1column, -1st column)

A[-2:-3, -5::2]
-5 -4 -3 -2 -1

-5 11 2 3 56 14
-4 40 52 16 12 20
-3 70 8 9 32 22
-2 18 30 17 44 49
-1 25 55 66 78 82
A. Broadcasting Methods of NumPy:

i. eye() or identity() Function


The eye() function creates a 2D array and fills the elements in the diagonal with
1s.
Syntax: eye(n, dtype=datatype)

This function will create an array with n rows and n columns with diagonal elements as
1s.The default data type is float. For Ex:

import numpy
a=numpy.eye(3)
print(a)
output-
[ [ 1. 0. 0.]
[ 0. 1. 0.]
[0. 0. 1.] ]

ii. zeros() function in 2D array:


This function is used to create two dimensional array with the 0 as default
value and default data type is float. For Example:
import numpy
Q = numpy.zeros([3,2], dtype = int)
Z = numpy.zeros([4,4], dtype = float)
print(Q)
print(Z)

Output- [[0 0]
[0 0]
[0 0]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

iii. ones() function in 2D array:


This function will be used to create the array with 1 as default value for each of
individual defined element.
Example:
import numpy
Q = numpy.ones([3,2], dtype = int)
Z = numpy.ones([4,4], dtype = float)
print(Q)
print(Z)

Output-
[[1 1]
[1 1]
[1 1]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]

B. Joins in Array:
We can join array in numpy by following method-
1. Concatenate()
2. hstack()
3. vstack()
1. concatenate()
Is used to join more than one array but the array must be of the same shape
Example:
import numpy as np
a=np.array([2,3,4,50])
b=np.array([8, 9,10,11,15])
c=np.concatenate([a,b])
print (c) #Output: [2 3 4 50 8 9 10 11 15]
import numpy as np
a=np.array([[2,3,4],[4,5,6],[7,8,9]])
b=np.concatenate([a,a],axis=1) #concatenate array a with array a column wise
print (b)

Output: [ [2 3 4 2 3 4]
[4 5 6 4 5 6]
[7 8 9 7 8 9] ]

Example-2
import numpy as np
a=np.array([[2,3,4],[4,5,6],[7,8,9]])
b=np.concatenate([a,a],axis=0) #concatenate array a with array a row wise
print (b)

Output: [ [2 3 4]
[4 5 6]
[7 8 9]
[2 3 4]
[4 5 6]
[7 8 9] ]

2. hstack()
It is used to join more than one array horizontally or row wise.
Example:
import numpy as np
a=np.array([1,2,3])
b=np.array([10,11,12])
c=np.hstack((a,b))
print (c) Output: [1 2 3 10 11 12]

3. vstack()
It is used to join more than one array vertically or column wise.
Example:
import numpy as np
a=np.array([1,2,3])
b=np.array([10,11,12])
c=np.vstack((a,b))
print (c) Output : [[1 2 3 ]
[10 11 12]]
Array subsets
We can get subsets of a numpy array by using any of the following-

1. split()
2. hsplit()
3. vsplit()
split():
It is used to split a numpy array into equal or unequal parts.

import numpy as np
x = [1, 2, 3, 99, 99, 3, 2, 1]
x1, x2, x3 = np.split(x, [3, 5]) #split array into 3 subsets like- [0 : 3], [3 : 5] and [5 : ]
print(x1, x2, x3)

Output-[1 2 3] [99 99] [3 2 1]


0 1 2 3 4 5 6 7

hsplit()
It is used to provide the subsets of an array after splitting it horizontally.
import numpy as np
a= np.arange(16).reshape((4, 4))
print( a)

Output-
array([[ 0, 1, 2, 3],
left
right
0
1
2
3
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
0 1 2 3
Example:
left, right = np.hsplit(a, 2) 4 5 6 7
print(left)
print(right) 8 9 10 11

Output-
12 13 14 15
[[ 0 1]
left right
[ 4 5]
[ 8 9]
[12 13]]
[[ 2 3]
[ 6 7]
[10 11]
[14 15]]

vsplit()
It is used to provide the subsets of an array after splitting it vertically.
Example:-
import nump as np
a= np.arange(16).reshape((4, 4))
print (a)

Output- array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])

Example:
top, bottom = np.vsplit(a, 2)
print(top)
0 1 2 3 Top
print(bottom)
4 5 6 7
Output- 8 9 10 11 Bottom
[[0 1 2 3] 12 13 14 15
[4 5 6 7]]
[[ 8 9 10 11]
[12 13 14 15]]

Statistical Function in Numpy

np.mean():Compute the arithmetic mean along the specified axis.


np.std():Compute the standard deviation along the specified axis.
np.var():Compute the variance along the specified axis.
np.sum() : Sum of array elements over a given axis.
np.prod() : Return the product of array elements over a given axis.
np.min()/np.max():Return the minimum / maximum of an array or minimum along axis.

Example:
import numpy as np
array1 = np.array([[10,20,30],[40,50,60]])

print(“Mean:” , np.mean(array1))
print(“Std: “, np.std(array1))
print(“Var: “, np.var(array1))
print(“Sum: “, np.sum(array1))
print(“Prod: “, np.prod(array1))

Output:
Mean: 35.0
Std: 17.07825127659933
Var: 291.6666666666667
Sum : 210
Prod: 720000000

You might also like