Day: April 1, 2021

Python Example – Write a NumPy program to compute the covariance matrix of two given arrays

(Python Example for Beginners)   Write a NumPy program to compute the covariance matrix of two given arrays.   Sample Solution: Python Code: import numpy as np x = np.array([0, 1, 2]) y = np.array([2, 1, 0]) print(“nOriginal array1:”) print(x) print(“nOriginal array1:”) print(y) print(“nCovariance matrix of the said arrays:n”,np.cov(x, y)) Sample Output: Original array1: [0 …

Python Example – Write a NumPy program to compute the weighted of a given array

(Python Example for Beginners)   Write a NumPy program to compute the weighted of a given array.   Sample Solution: Python Code: 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) Sample Output: …

Python Example – Write a NumPy program to compute the 80th percentile for all elements in a given array along the second axis

(Python Example for Beginners)   Write a NumPy program to compute the 80th percentile for all elements in a given array along the second axis.   Sample Solution: Python Code: import numpy as np x = np.arange(12).reshape((2, 6)) print(“nOriginal array:”) print(x) r1 = np.percentile(x, 80, 1) print(“n80th percentile for all elements of the said array along …

Python Example – Write a NumPy program to check two random arrays are equal or not

(Python Example for Beginners)   Write a NumPy program to check two random arrays are equal or not.   Sample Solution: Python Code : import numpy as np x = np.random.randint(0,2,6) print(“First array:”) print(x) y = np.random.randint(0,2,6) print(“Second array:”) print(y) print(“Test above two arrays are equal or not!”) array_equal = np.allclose(x, y) print(array_equal) Sample Output: …

Python Example – Write a NumPy program to create a random vector of size 10 and sort it

(Python Example for Beginners)   Write a NumPy program to create a random vector of size 10 and sort it.   Sample Solution: Python Code : import numpy as np x = np.random.random(10) print(“Original array:”) print(x) x.sort() print(“Sorted array:”) print(x) Sample Output: Original array: [ 0.73123073 0.67714015 0.95615347 0.4759837 0.88789818 0.69104042 0.59996415 0.26144489 0.51618644 0.89943882] …

Python Example – Write a NumPy program to shuffle numbers between 0 and 10 (inclusive)

(Python Example for Beginners)   Write a NumPy program to shuffle numbers between 0 and 10 (inclusive).   Sample Solution: Python Code : import numpy as np x = np.arange(10) np.random.shuffle(x) print(x) print(“Same result using permutation():”) print(np.random.permutation(10)) Sample Output: [2 7 1 5 3 9 0 4 6 8] Same result using permutation(): [8 9 …

Python Example – Write a NumPy program to create a 5×5 array with random values and find the minimum and maximum values

(Python Example for Beginners)   Write a NumPy program to create a 5×5 array with random values and find the minimum and maximum values.   Sample Solution : Python Code : import numpy as np x = np.random.random((5,5)) print(“Original Array:”) print(x) xmin, xmax = x.min(), x.max() print(“Minimum and Maximum Values:”) print(xmin, xmax) Sample Output: Original Array: …

Python Example – Write a NumPy program to generate six random integers between 10 and 30

(Python Example for Beginners)   Write a NumPy program to generate six random integers between 10 and 30.   Sample Solution: Python Code: import numpy as np x = np.random.randint(low=10, high=30, size=6) print(x) Sample Output: [14 25 20 12 27 22] Pictorial Presentation:   Python Example – Write a NumPy program to generate six random …

Python Example – Write a NumPy program to generate six random integers between 10 and 30

(Python Example for Beginners)   Write a NumPy program to generate six random integers between 10 and 30.   Sample Solution: Python Code: import numpy as np x = np.random.randint(low=10, high=30, size=6) print(x) Sample Output: [14 25 20 12 27 22]     Python Example – Write a NumPy program to generate six random integers …

Python Example – Write a NumPy program to generate six random integers between 10 and 30

(Python Example for Beginners)   Write a NumPy program to generate six random integers between 10 and 30.   Sample Solution: Python Code: import numpy as np x = np.random.randint(low=10, high=30, size=6) print(x) Sample Output: [14 25 20 12 27 22]     Python Example – Write a NumPy program to generate six random integers …