Day: April 1, 2021

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

Hits: 15 (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 …

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

Hits: 5 (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) …

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

Hits: 1 (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 …

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

Hits: 8 (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) …

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

Hits: 6 (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 …

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

Hits: 0 (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(): …

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

Hits: 8 (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: …

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

Hits: 2 (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 …

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

Hits: 2 (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 …

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

Hits: 0 (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 …

Python Example – Write a NumPy program to calculate the Frobenius norm and the condition number of a given array

Hits: 6 (Python Example for Beginners)   Write a NumPy program to calculate the Frobenius norm and the condition number of a given array.   Sample Solution: Python Code : import numpy as np a = np.arange(1, 10).reshape((3, 3)) print(“Original array:”) print(a) print(“Frobenius norm and the condition number:”) print(np.linalg.norm(a, ‘fro’)) print(np.linalg.cond(a, ‘fro’)) Sample Output: Original …

Python Example – Write a NumPy program to get the qr factorization of a given array

Hits: 2 (Python Example for Beginners)   Write a NumPy program to get the qr factorization of a given array.   Sample Solution: Python Code : import numpy as np a = np.array([[4, 12, -14], [12, 37, -53], [-14, -53, 98]], dtype=np.int32) print(“Original array:”) print(a) q, r = np.linalg.qr(a) print(“qr factorization of the said array:”) …

Python Example – Write a NumPy program to compute the sum of the diagonal element of a given array

Hits: 4 (Python Example for Beginners)   Write a NumPy program to compute the sum of the diagonal element of a given array.   Sample Solution: Python Code : import numpy as np m = np.arange(6).reshape(2,3) print(“Original matrix:”) print(m) result = np.trace(m) print(“Condition number of the said matrix:”) print(result) Sample Output: Original matrix: [[0 1 …

Python Example – Write a NumPy program to compute the condition number of a given matrix

Hits: 4 (Python Example for Beginners)   Write a NumPy program to compute the condition number of a given matrix. From Wikipedia: In the field of numerical analysis, the condition number of a function with respect to an argument measures how much the output value of the function can change for a small change in …

Python Example – Write a NumPy program to calculate the QR decomposition of a given matrix

Hits: 3 (Python Example for Beginners)   Write a NumPy program to calculate the QR decomposition of a given matrix. From Wikipedia: In linear algebra, a QR decomposition (also called a QR factorization) of a matrix is a decomposition of a matrix A into a product A = QR of an orthogonal matrix Q and …

Python Example – Write a NumPy program to compute the inverse of a given matrix

Hits: 2 (Python Example for Beginners)   Write a NumPy program to compute the inverse of a given matrix.   Sample Solution : Python Code : import numpy as np m = np.array([[1,2],[3,4]]) print(“Original matrix:”) print(m) result = np.linalg.inv(m) print(“Inverse of the said matrix:”) print(result) Sample Output: Original matrix: [[1 2] [3 4]] Inverse of the …