numpy
numpy
Laboratory
S.Yeshwanth,
23N266
23N266, pg. 1
Importing numpy
import numpy as np
Exercise 1:
import numpy as np
nums = np.arange(10)
nums
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Exercise 2:
arr = np.array([1,2,3,4,5,6]).reshape(2,-1)
arr
array([[1, 2, 3],
[4, 5, 6]])
Exercise 3:
arr1 = np.arange(15).reshape((5,3))
arr2 = np.arange(6).reshape((3,2))
print("Arr1: ",arr1)
print("Arr2: ",arr2)
print("Prod: ",prod)
Arr1: [[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]
[12 13 14]]
Arr2: [[0 1]
[2 3]
[4 5]]
Prod: [[ 10 13]
[ 28 40]
[ 46 67]
23N266, pg. 2
[ 64 94]
[ 82 121]]
Exercise 4:
arr = np.arange(10)
odd_arr = arr[arr%2==1]
odd_arr
array([1, 3, 5, 7, 9])
Exercise 5:
arr = np.arange(1,10)
arr[arr%2 == 1] = -1
arr
Exercise 6:
Convert a 1D array to a boolean array where all positive values become True.
arr = np.array([1,-2,3,-4,5,-6])
bool_arr
array([ True, False, True, False, True, False])
Exercise 7:
Replace all even numbers in a 1D array with their negative
arr = np.arange(1,20)
arr[arr%2==0] *= -1
arr
23N266, pg. 3
Exercise 8:
matrix = np.random.random((3,3))
print(matrix)
print(normalized_matrix)
Exercise 9:
Calculate the sum of the diagonal elements of a 3x3 matrix.
arr = np.arange(1,10).reshape(3,3)
print(arr)
diagonal_sum = np.trace(arr)
print(diagonal_sum)
[[1 2 3]
[4 5 6]
[7 8 9]]
15
Exercise 10:
arr = np.array([1,2,0,0,4,0])
non_zero_indices = np.nonzero(arr)
non_zero_indices
Exercise 11:
23N266, pg. 4
arr = np.arange(1,10)
arr = np.flip(arr)
arr
array([9, 8, 7, 6, 5, 4, 3, 2, 1])
Exercise 12:
identity = np.eye(3)
identity
Exercise 13:
Reshape a 1D array to a 2D array with 5 rows and 2 columns.
arr = np.arange(10).reshape(5,2)
arr
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
Exercise 14:
arr1 = np.array([1,2,3,4,5,6])
arr2 = np.array([3,4,5,7,4,6])
stacked_array = np.vstack((arr1,arr2))
stacked_array
array([[1, 2, 3, 4, 5, 6],
[3, 4, 5, 7, 4, 6]])
Exercise 15:
23N266, pg. 5
Get the common items between two arrays.
print(common_items)
[3 4 5]
Exercise 16:
Create a 5x5 matrix with row values ranging from 0 to 4.
matrix = np.zeros((5,5))
matrix += np.array([0,1,2,3,4])
matrix
Exercise 17:
Find the index of the maximum value in a 1D array.
arr = np.array([1,2,3,4,5,6,67,8,9])
max_index = np.argmax(arr)
max_index
Exercise 18:
print(normalized_arr)
23N266, pg. 6
Exercise 19:
print(dot_product)
32
Exercise 20:
Count the number of elements in an array within a specific range.
arr = np.array([1,2,3,4,5,6,45,7,7,5,4,4,5,3,4,4,4,7,7,6,8,9,0])
count_within_range
11
Exercise 21:
arr = np.arange(15).reshape(3,5)
mean
Exercise 22:
arr = np.arange(16).reshape(4,4)
print(arr)
diagonal_elements = np.diag(arr)
print(diagonal_elements)
23N266, pg. 7
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
[ 0 5 10 15]
Exercise 23:
arr = np.array([1,2,13,4,5,34,6,7,8,5,44,6,45,4,4,3,22,2,2,2])
print(countSpecificNum(arr, 2))
Exercise 24:
Replace all values in a 1D array with the mean of the array.
arr = np.array([1,2,13,4,5,34,6,7,8,5,44,6,45,4,4,3,22,2,2,2])
arr[:] = np.mean(arr)
print(arr)
[10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10]
Exercise 25:
arr =
np.array([1,2,4,3,546,45,634,324,2,234,5,3,35,65,6,7,5,34,34,6,36,36,3
6,3,74,86,85])
min = np.argmin(arr)
max = np.argmax(arr)
print(min, max)
0 6
Exercise 26:
23N266, pg. 8
arr = np.ones((5,5))
arr[1:-1, 1:-1] = 0
arr
Exercise 27:
arr = np.array([1,2,1,33,1,3,3,4,3,4,5,6,4,5,6,5,3,3,53,3,2,4,3,2,2])
print(unique)
print(counts)
[ 1 2 3 4 5 6 33 53]
[3 4 7 4 3 2 1 1]
Exercise 28:
arr = np.arange(9).reshape(3,3)
arr
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
Exercise 29:
Calculate the exponential of all elements in a 1D array.
exponential_arr = np.exp(arr)
print(exponential_arr)
23N266, pg. 9
Exercise 30:
arr = np.array(range(15)).reshape(3,5)
arr[[1,2]] = arr[[2,1]]
arr
array([[ 0, 1, 2, 3, 4],
[10, 11, 12, 13, 14],
[ 5, 6, 7, 8, 9]])
Exercise 31:
Create a random 3x3 matrix and replace all values greater than 0.5 with 1 and all others with 0.
arr = np.random.random((3,3))
arr[arr <=0.5] = 0
arr
Exercise 32:
arr = np.array([1,2,3,345,4,6,5,4,3,3,5,3,3,42,41,2,5,2])
top_indices = np.argsort(arr)[-5:]
top_indices
Exercise 33:
matrix = np.arange(15).reshape(5,3)
print(matrix)
23N266, pg. 10
print(column_means)
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]
[12 13 14]]
[6. 7. 8.]
Exercise 34:
matrix = np.arange(15).reshape(5,3)
print(matrix)
print(normalized_matrix)
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]
[12 13 14]]
[[-1.41421356 -1.41421356 -1.41421356]
[-0.70710678 -0.70710678 -0.70710678]
[ 0. 0. 0. ]
[ 0.70710678 0.70710678 0.70710678]
[ 1.41421356 1.41421356 1.41421356]]
Exercise 35:
print(concatenated_arr)
[1 2 3 4 5 6]
Exercise 36:
23N266, pg. 11
arr = np.array([1,2,4,5,43,3,2,4,4,2,2,1,1,1,4,6,7,4]).reshape(2,-1)
print(arr)
print(sorted_arr)
[[ 1 2 4 5 43 3 2 4 4]
[ 2 2 1 1 1 4 6 7 4]]
[[ 1 2 2 3 4 4 4 5 43]
[ 1 1 1 2 2 4 4 6 7]]
Exercise 37:
Compute the mean squared error between two arrays.
mse = np.mean((arr1-arr2)**2)
print(mse)
1.0
Exercise 38:
Replace all negative values in an array with 0.
arr = np.array([1,2,1,3,2,2,3,-1,-3,-53,43,3,4,3,3,-45,2])
arr[arr<0] = 0
arr
array([ 1, 2, 1, 3, 2, 2, 3, 0, 0, 0, 43, 3, 4, 3, 3, 0,
2])
Exercise 39:
Find the 5th and 95th percentiles of an array.
arr = np.array([1,2,2,4,4,3,4,3,2,4,5,6,6,5,7,8,9,9,7,6,5,5,5])
percentile_5th = np.percentile(arr, 5)
23N266, pg. 12
print("95th Percentile:", percentile_95th)
Exercise 40:
matrix = np.random.random((3,3))
det = np.linalg.det(matrix)
det
-0.04825975536941865
Exercise 41:
Count the number of elements in an array that are greater than the mean.
arr = np.arange(10)
mean = np.mean(arr)
count_above_mean
Exercise 42:
arr = np.arange(10)
sqrt_arr = np.sqrt(arr)
sqrt_arr
Exercise 43:
matrix = np.arange(9).reshape(3,3)
23N266, pg. 13
matrix_sqrt = np.linalg.matrix_power(matrix, 2)
matrix_sqrt
Exercise 44:
float_arr = arr.astype(float)
print(float_arr)
[1. 2. 3. 4.]
Exercise 45:
Calculate the element-wise absolute values of an array.
arr = np.random.random(10)
arr = np.abs(arr)
arr
Exercise 46:
arr1 = np.array([1,2,6,4,5])
arr2 = np.array([1,2,3,4,5])
intersection
Exercise 47:
Calculate the cumulative sum of elements in a 1D array.
23N266, pg. 14
arr = np.array([1, 2, 3, 4, 5])
cumulative_sum = np.cumsum(arr)
print(cumulative_sum)
[ 1 3 6 10 15]
Exercise 48:
inverse_matrix = np.linalg.inv(matrix)
print(inverse_matrix)
[[ 1.54082111 -0.37606003]
[-1.64424784 1.56921956]]
Exercise 49:
Count the number of non-zero elements in a 2D array.
non_zero_count = np.count_nonzero(matrix)
print(non_zero_count)
Exercise 50:
matrix[np.isnan(matrix)] = 0
print(matrix)
[[1. 0. 3.]
[4. 5. 0.]
[7. 8. 9.]]
Exercise 51:
23N266, pg. 15
arr1 = np.array([1, 2, 3, 4, 5])
print(correlation_coefficient)
0.9999999999999999
Exercise 52:
unique_arr = np.unique(arr)
print(unique_arr)
[1 2 3 4 5]
Exercise 53:
print(elementwise_product)
[ 4 10 18]
Exercise 54:
print(column_stddev)
Exercise 55:
Create a 2D array and set all values above a certain threshold to that threshold.
23N266, pg. 16
matrix = np.random.random((5,5))
threshold = 0.7
matrix
Exercise 56:
Create a random 5x5 matrix and replace the maximum value by -1.
matrix = np.random.random((5,5))
matrix[max_val_index] = -1
matrix
Exercise 57:
Convert a 1D array of Fahrenheit temperatures to Celsius.
print(celsius_temps)
Exercise 58:
23N266, pg. 17
Compute the outer product of two arrays.
print(outer_product)
[[ 4 5 6]
[ 8 10 12]
[12 15 18]]
Exercise 59:
print(equidistant_arr)
Exercise 60:
Compute the cross product of two 3D arrays.
print(cross_product)
[-3 6 -3]
Exercise 61:
print(percentiles_axis1)
23N266, pg. 18
Exercise 62:
print(arr_with_border)
[0 1 2 3 4 0]
Exercise 63:
print("Histogram:", hist)
Histogram: [2 3 4]
Bin edges: [1 2 3 4]
Exercise 64:
print(normalized_rows)
Exercise 65:
23N266, pg. 19
print(sorted_matrix_by_column2)
Exercise 66:
Calculate the determinant of a 3x3 matrix.
determinant = np.linalg.det(matrix)
print(determinant)
0.029061429535904963
Exercise 67:
Calculate the element-wise exponentiation of a 1D array.
exponentiated_arr = np.exp(arr)
print(exponentiated_arr)
Exercise 68:
frobenius_norm = np.linalg.norm(matrix)
print(frobenius_norm)
2.219522924106627
Exercise 69:
Create a 2D array with random values and replace the maximum value with the minimum.
23N266, pg. 20
min_value = np.min(matrix)
matrix[max_value_index] = min_value
print(matrix)
Exercise 70:
print(matrix_multiplication)
Exercise 71:
Create a 1D array and set the values between 10 and 20 to 0.
print(arr)
[ 5 0 0 0 25]
Exercise 72:
inverse_sineh_arr = np.arcsinh(arr)
print(inverse_sineh_arr)
Exercise 73:
23N266, pg. 21
Compute the Kronecker product of two arrays.
print(kronecker_product)
[3 4 6 8]
Exercise 74:
Calculate the mean absolute deviation of a 1D array.
print(mean_absolute_deviation)
1.2
Exercise 75:
Create a 3x3 matrix and set all values above the main diagonal to zero.
matrix[np.triu_indices(3, 1)] = 0
print(matrix)
[[0.03592082 0. 0. ]
[0.98343727 0.55001654 0. ]
[0.83016671 0.31007078 0.81221473]]
Exercise 76:
print("Counts:", counts)
23N266, pg. 22
Unique values: [1 2 3 4]
Counts: [1 2 3 1]
Exercise 77:
print(cumulative_product_axis0)
Exercise 78:
Round elements of a 1D array to the nearest integer.
rounded_arr = np.round(arr)
print(rounded_arr)
[1. 3. 4. 5.]
Exercise 79:
new_element = 4
print(arr)
[1 2 3 4]
Exercise 80:
23N266, pg. 23
absolute_difference = np.abs(arr1 - arr2)
print(absolute_difference)
[1 2 7 9 3]
Exercise 81:
Create a 2D array with random values and replace the maximum value in each row with -1.
matrix[np.arange(matrix.shape[0]), max_values_indices] = -1
print(matrix)
[[ 0.02028369 -1. 0.42466127 0.94363836]
[ 0.07116187 -1. 0.08768886 0.52489983]
[-1. 0.4391846 0.35890509 0.12949558]]
Exercise 82:
print(normalized_columns)
Exercise 83:
Find the indices of the top N minimum values in a 1D array.
top_indices = np.argsort(arr)[:2]
print(top_indices)
[3 1]
Exercise 84:
23N266, pg. 24
Convert the elements of a 1D array to strings.
string_arr = arr.astype(str)
print(string_arr)
Exercise 85:
print(percentile_rank)
Exercise 86:
Create a 1D array and shuffle its elements randomly.
np.random.shuffle(arr)
print(arr)
[4 1 3 2 5]
Exercise 87:
all_nonzero = np.all(arr != 0)
print(all_nonzero)
True
23N266, pg. 25
Exercise 88:
print(max_indices_per_row)
[0 1 1]
Exercise 89:
Create a 2D array and replace all nan values with the mean of the array.
nan_mean = np.nanmean(matrix)
matrix[np.isnan(matrix)] = nan_mean
print(matrix)
[[1. 5.28571429 3. ]
[4. 5. 5.28571429]
[7. 8. 9. ]]
Exercise 90:
print(row_means_ignore_nan)
[1.5 5. 8. ]
Exercise 91:
Compute the sum of diagonal elements in a 2D array.
diagonal_sum = np.trace(matrix)
print(diagonal_sum)
2.1246107289884653
23N266, pg. 26
Exercise 92:
arr_in_degrees = np.degrees(arr_in_radians)
print(arr_in_degrees)
Exercise 93:
Calculate the pairwise Euclidean distance between two arrays.
print(euclidean_distance)
5.196152422706632
Exercise 94:
Create a 1D array and set the values between the 25th and 75th percentile to 0.
print(arr)
[10 0 0 0 50]
Exercise 95:
23N266, pg. 27
print(squared_difference)
[9 9 9]
Exercise 96:
Replace all even numbers in a 1D array with the next odd number.
arr[arr % 2 == 0] += 1
print(arr)
[ 3 5 9 13 15]
Exercise 97:
print(normalized_columns_range)
[[1. 0. 0.72054908 0. ]
[0. 1. 0. 1. ]
[0.57570135 0.07701889 1. 0.43791495]]
Exercise 98:
print(cumulative_sum_axis1)
Exercise 99:
23N266, pg. 28
arr = np.array([0, 0, 0, 1, 0])
any_nonzero = np.any(arr != 0)
print(any_nonzero)
True
Exercise 100:
Create a 2D array with random integers and replace all values greater than a certain threshold
with that threshold.
threshold = 75
print(matrix)
[[75 13 12 66]
[62 62 0 63]
[52 75 40 75]]
23N266, pg. 29