Tag Archives: python for beginners

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 …

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

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

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

(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:”) print( “q=n”, …