Working with Big Data inPython | 239
Sr # Command Purpose Sample Code with Output
>>> np.divide(arr2, arr1)
array([[inf, 6., 4.],
[3.33333333, 3.,2.8]])
>>> np.power(arr1,2)
array([[ 0, 1, 4],
[ 9, 16, 25]])
>>> np.mod(arr2, arr1)
array([[0, 0, 0],
[1, 0, 4]])
>>> np.remainder(arr2, arr1)
array([[0, 0, 0],
[1, 0, 4]])
4. sort, where,
nonzero
For sorting and
searching
>>> a = np.array([[21,7,
14],[19,40,8]])
>>> a
array([[21, 7, 14],
[19, 40, 8]])
>>> np.sort(a)
array([[ 7, 14, 21],
[ 8, 19, 40]])
>>> np.sort(a, axis = 0)
array([[19, 7, 8],
[21, 40, 14]])
>>> np.sort(a, axis = 1)
array([[ 7, 14, 21],
[ 8, 19, 40]])
>>> np.where(a > 13)
(array([0, 0, 1, 1]), array([0,
2, 0, 1]))
>>> print(a[np.where(a > 13)])
[21 14 19 40]
(Continued)
M09 Big Data Simplified XXXX 01.indd 239 5/10/2019 10:22:57 AM
240 | Big Data Simplied
Sr # Command Purpose Sample Code with Output
5. mean, median Statistical functions
>>> np.mean(a)
18.166666666666668
>>> np.mean(a, axis = 1)
array([14. , 22.33333333])
>>> np.mean(a, axis = 0)
array([20. , 23.5, 11. ])
>>> np.median(a)
16.5
>>> np.median(a, axis = 1)
array([14., 19.])
>>> np.median(a, axis = 0)
array([20. , 23.5, 11. ])
>>> np.std(a)
11.036555420762202
>>> np.var(a)
121.80555555555554
Practice Problem: Write a simple program to take the value of loop counter as input and build a
string array of that size. After that, print array values in a loop. Also, print the entire array at
the end.
import numpy as np
n = int(input(Enter the array size needed: ))
var2 = np.empty(n, dtype = object)
strval = “”
for i in range (1, n):
var2[i] = This is string # + str(i)
print(var2[i])
print(var2)
Enter the array size needed: 3
This is string # 1
This is string # 2
[None ‘This is string # 1’’This is string # 2’]
M09 Big Data Simplified XXXX 01.indd 240 5/10/2019 10:22:57 AM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.165.62