1. Write a NumPy program to compute the weighted of a given array.
import numpy as np
x = np.arange(5)print("\nOriginal array:")print(x)
weights = np.arange(1,6)
r1 = np.average(x, weights=weights)
r2 =(x*(weights/weights.sum())).sum()assert np.allclose(r1, r2)print("\nWeighted average of the said array:")print(r1)
2.Write a NumPy program to compute pearson product-moment correlation coefficients of two given arrays.
np.corrcoef(x, y)
3. Write a NumPy program to compute the histogram of nums against the bins.
2. Write a NumPy program to remove the leading whitespaces of all the elements of a given array.
import numpy as np
x = np.array([' python exercises ',' PHP ',' java ',' C++'], dtype=np.str)print("Original Array:")print(x)
lstripped_char = np.char.lstrip(x)print("\nRemove the leading whitespaces : ", lstripped_char)
Original Array:[' python exercises '' PHP '' java '' C++']
Remove the leading whitespaces :['python exercises ''PHP ''java ''C++']
3. Write a NumPy program to make all the elements of a given string to a numeric string of 5 digits with zeros on its left.
import numpy as np
x = np.array(['2','11','234','1234','12345'], dtype=np.str)print("\nOriginal Array:")print(x)
r = np.char.zfill(x,5)print("\nNumeric string of 5 digits with zeros:")print(r)
Original Array:['2''11''234''1234''12345']
Numeric string of 5 digits with zeros:['00002''00011''00234''01234''12345']
4. Write a NumPy program to count the number of “P” in a given array, element-wise.
import numpy as np
x1 = np.array(['Python','PHP','JS','examples','html'], dtype=np.str)print("\nOriginal Array:")print(x1)print("Number of ‘P’:")
r = np.char.count(x1,"P")print(r)
Original Array:['Python''PHP''JS''examples''html']
Number of ‘P’:[12000]
5. Write a NumPy program to count the lowest index of “P” in a given array, element-wise.
import numpy as np
x1 = np.array(['Python','PHP','JS','EXAMPLES','HTML'], dtype=np.str)print("\nOriginal Array:")print(x1)print("count the lowest index of ‘P’:")
r = np.char.find(x1,"P")print(r)
Original Array:['Python''PHP''JS''EXAMPLES''HTML']
count the lowest index of ‘P’:[00-14-1]
6. Write a NumPy program to check whether each element of a given array is composed of digits only, lower case letters only and upper case letters only.
import numpy as np
x = np.array(['Python','PHP','JS','Examples','html5','5'], dtype=np.str)print("\nOriginal Array:")print(x)
r1 = np.char.isdigit(x)
r2 = np.char.islower(x)
r3 = np.char.isupper(x)print("Digits only =", r1)print("Lower cases only =", r2)print("Upper cases only =", r3)
Original Array:['Python''PHP''JS''Examples''html5''5']
Digits only =[FalseFalseFalseFalseFalseTrue]
Lower cases only =[FalseFalseFalseFalseTrueFalse]
Upper cases only =[FalseTrueTrueFalseFalseFalse]
Statistics1. Write a NumPy program to compute the weighted of a given array.import numpy as npx = np.arange(5)print("\nOriginal array:")print(x)weights = np.arange(1, 6)r1 = np.average(x, weigh...