NumPy Assignment #Ans1 import numpy as np arr = np.arange(6) print(arr) print(arr.dtype) [0 1 2 3 4 5] int64 #Ans2 arr = np.array([1.5, 2.6, 3.7]) if arr.dtype == np.float64: print("Data type is float64") else: print("Data type is not float64") Data type is float64 #Ans3 arr = np.array([1+2j, 3+4j, 5+6j], dtype=np.complex128) print(arr) print(arr.dtype) [1.+2.j 3.+4.j 5.+6.j] complex128 #Ans4 arr = np.array([1, 2, 3, 4, 5]) arr_float32 = arr.astype(np.float32) print(arr_float32) print(arr_float32.dtype) [1. 2. 3. 4. 5.] float32 #Ans5 arr = np.array([1.123456789, 2.123456789, 3.123456789], dtype=np.float64) arr_float32 = arr.astype(np.float32) print(arr_float32) print(arr_float32.dtype) [1.1234568 2.1234567 3.1234567] float32 #Ans6 def array_attributes(arr): return arr.shape, arr.size, arr.dtype
arr = np.array([1, 2, 3]) print(array_attributes(arr)) ((3,), 3, dtype('int64')) #Ans7 def array_dimension(arr): return arr.ndim arr = np.array([[1, 2], [3, 4]]) print(array_dimension(arr)) 2 #Ans8 def item_size_info(arr): return arr.itemsize, arr.nbytes arr = np.array([1, 2, 3, 4, 5]) print(item_size_info(arr)) (8, 40) #Ans9 def array_strides(arr): return arr.strides arr = np.array([[1, 2], [3, 4]]) print(array_strides(arr)) (16, 8) #Ans10 def shape_stride_relationship(arr): return arr.shape, arr.strides arr = np.array([[1, 2], [3, 4]]) print(shape_stride_relationship(arr)) ((2, 2), (16, 8)) #Ans11 def create_zeros_array(n): return np.zeros(n) print(create_zeros_array(5)) [0. 0. 0. 0. 0.] #Ans12 def create_ones_matrix(rows, cols):
return np.ones((rows, cols)) print(create_ones_matrix(3, 4)) [[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]] #Ans13 def generate_range_array(start, stop, step): return np.arange(start, stop, step) print(generate_range_array(1, 10, 2)) [1 3 5 7 9] #Ans14 def generate_linear_space(start, stop, num): return np.linspace(start, stop, num) print(generate_linear_space(1.0, 5.0, 10)) [1. 1.44444444 1.88888889 2.33333333 2.77777778 3.22222222 3.66666667 4.11111111 4.55555556 5. ] #Ans15 def create_identity_matrix(n): return np.eye(n) print(create_identity_matrix(4)) [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]] #Ans16 def list_to_array(py_list): return np.array(py_list) print(list_to_array([1, 2, 3, 4, 5])) [1 2 3 4 5] #Ans17 arr = np.array([1, 2, 3, 4, 5]) arr_view = arr.view() print(arr_view) [1 2 3 4 5]
#Ans18 def concatenate_arrays(arr1, arr2, axis=0): return np.concatenate((arr1, arr2), axis=axis) arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) print(concatenate_arrays(arr1, arr2, axis=0)) [[1 2] [3 4] [5 6] [7 8]] #Ans19 arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5], [6]]) print(np.concatenate((arr1, arr2), axis=1)) [[1 2 5] [3 4 6]] #Ans20 def vertical_stack(arr_list): return np.vstack(arr_list) arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) arr3 = np.array([7, 8, 9]) print(vertical_stack([arr1, arr2, arr3])) [[1 2 3] [4 5 6] [7 8 9]] #Ans21 def create_array_in_range(start, stop, step): return np.arange(start, stop+1, step) print(create_array_in_range(1, 10, 2)) [1 3 5 7 9] #Ans22 def equally_spaced_values(): return np.linspace(0, 1, 10) print(equally_spaced_values()) [0. 0.11111111 0.22222222 0.33333333 0.44444444 0.55555556 0.66666667 0.77777778 0.88888889 1. ]
#Ans23 def log_spaced_values(): return np.logspace(0, 3, 5) print(log_spaced_values()) [ 1. 5.62341325 31.6227766 177.827941 1000. ] #Ans24 import pandas as pd def create_dataframe(): data = np.random.randint(1, 101, size=(5, 3)) return pd.DataFrame(data, columns=['A', 'B', 'C']) df = create_dataframe() print(df) A B C 0 49 18 46 1 55 61 78 2 8 27 63 3 60 12 1 4 71 66 33 #Ans25 def replace_negatives_with_zero(df, column_name): df[column_name] = np.where(df[column_name] < 0, 0, df[column_name]) return df df = pd.DataFrame({'A': [1, -2, 3], 'B': [-1, 5, -3]}) print(replace_negatives_with_zero(df, 'A')) A B 0 1 -1 1 0 5 2 3 -3 #Ans26 arr = np.array([10, 20, 30, 40, 50]) print(arr[2]) 30 #Ans27 arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(arr_2d[1, 2]) 6
#Ans28 arr = np.array([3, 8, 2, 10, 5, 7]) print(arr[arr > 5]) [ 8 10 7] #Ans29 arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) print(arr[2:6]) [3 4 5 6] #Ans30 arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(arr_2d[0:2, 1:3]) [[2 3] [5 6]] #Ans31 def extract_elements(arr, indices): return arr[indices] arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) indices = np.array([0, 2]) print(extract_elements(arr, indices)) [[1 2 3] [7 8 9]] #Ans32 def filter_greater_than(arr, threshold): return arr[arr > threshold] arr = np.array([1, 5, 10, 15, 20]) print(filter_greater_than(arr, 10)) [15 20] #Ans33 def extract_from_3d_array(arr, idx1, idx2, idx3): return arr[idx1, idx2, idx3] arr = np.random.randint(0, 10, (4, 4, 4)) idx1 = np.array([0, 1, 2]) idx2 = np.array([1, 2, 3]) idx3 = np.array([2, 3, 1]) print(extract_from_3d_array(arr, idx1, idx2, idx3)) [7 8 7]
#Ans34 def filter_with_conditions(arr): return arr[(arr > 10) & (arr < 20)] arr = np.array([5, 12, 18, 25]) print(filter_with_conditions(arr)) [12 18] #Ans35 def extract_using_indices(arr, row_indices, col_indices): return arr[row_indices, col_indices] arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) row_indices = np.array([0, 1, 2]) col_indices = np.array([2, 1, 0]) print(extract_using_indices(arr, row_indices, col_indices)) [3 5 7] #Ans36 arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(arr + 5) [[ 6 7 8] [ 9 10 11] [12 13 14]] #Ans37 arr1 = np.array([[1, 2, 3]]) arr2 = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) print(arr2 * arr1.T) [[ 1 2 3 4] [10 12 14 16] [27 30 33 36]] #Ans38 arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) print(arr2 + arr1[:, np.newaxis]) [[ 2 3 4] [ 6 7 8] [10 11 12] [14 15 16]] #Ans39 arr1 = np.array([[1], [2], [3]]) arr2 = np.array([[1, 2, 3]]) print(arr1 + arr2)
[[2 3 4] [3 4 5] [4 5 6]] #Ans40 arr1 = np.array([[1, 2, 3], [4, 5, 6]]) arr2 = np.array([[1, 2], [3, 4]]) # Broadcasting won't work due to shape incompatibility. One way to solve it is reshaping: arr2_reshaped = np.repeat(arr2, 2, axis=1)[:, :-1] # Repeat elements along axis 1 and remove the last column print(arr1 * arr2_reshaped) [[ 1 2 6] [12 15 24]] #Ans41 arr = np.array([[1, 2, 3], [4, 5, 6]]) print(np.mean(arr, axis=0)) [2.5 3.5 4.5] #Ans42 arr = np.array([[1, 2, 3], [4, 5, 6]]) print(np.max(arr, axis=1)) [3 6] #Ans43 arr = np.array([[1, 2, 3], [4, 5, 6]]) print(np.argmax(arr, axis=0)) [1 1 1] #Ans44 def moving_sum(arr): return np.cumsum(arr, axis=1) arr = np.array([[1, 2, 3], [4, 5, 6]]) print(moving_sum(arr)) [[ 1 3 6] [ 4 9 15]] #Ans45 arr = np.array([[2, 4, 6], [3, 5, 7]]) print(np.all(arr % 2 == 0, axis=0)) [False False False]
#Ans46 original_array = np.array([1, 2, 3, 4, 5, 6]) reshaped_matrix = original_array.reshape(2, 3) print(reshaped_matrix) [[1 2 3] [4 5 6]] #Ans47 def flatten_matrix(matrix): return matrix.flatten() input_matrix = np.array([[1, 2, 3], [4, 5, 6]]) flattened_array = flatten_matrix(input_matrix) print(flattened_array) [1 2 3 4 5 6] #Ans48 array1 = np.array([[1, 2], [3, 4]]) array2 = np.array([[5, 6], [7, 8]]) # Concatenating along axis 0 (rows) concatenated_array = np.concatenate((array1, array2), axis=0) print(concatenated_array) print(" ") #for space between result # Concatenating along axis 1 (columns) concatenated_array_axis1 = np.concatenate((array1, array2), axis=1) print(concatenated_array_axis1) [[1 2] [3 4] [5 6] [7 8]] [[1 2 5 6] [3 4 7 8]] #Ans49 def split_array(arr, num_splits): return np.array_split(arr, num_splits, axis=0) original_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) sub_arrays = split_array(original_array, 3) print(sub_arrays) [array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]])] #Ans50 def insert_and_delete_elements(arr, indices_to_insert,
values_to_insert, indices_to_delete): modified_array = np.insert(arr, indices_to_insert, values_to_insert) modified_array = np.delete(modified_array, indices_to_delete) return modified_array original_array = np.array([1, 2, 3, 4, 5]) indices_to_insert = [2, 4] values_to_insert = [10, 11] indices_to_delete = [1, 3] result = insert_and_delete_elements(original_array, indices_to_insert, values_to_insert, indices_to_delete) print(result) [ 1 10 4 11 5] #Ans51 arr1 = np.random.randint(1, 11, 10) arr2 = np.arange(1, 11) addition_result = np.add(arr1, arr2) print(addition_result) [ 2 9 9 10 9 11 9 12 16 19] #Ans52 arr1 = np.arange(10, 0, -1) arr2 = np.arange(1, 11) subtraction_result = np.subtract(arr1, arr2) print(subtraction_result) [ 9 7 5 3 1 -1 -3 -5 -7 -9] #Ans53 arr1 = np.random.randint(1, 11, 5) arr2 = np.arange(1, 6) multiplication_result = np.multiply(arr1, arr2) print(multiplication_result) [ 9 8 3 32 20] #Ans54 arr1 = np.arange(2, 11, 2) arr2 = np.arange(1, 6) division_result = np.divide(arr1, arr2) print(division_result) [2. 2. 2. 2. 2.]
#Ans55 arr1 = np.arange(1, 6) arr2 = np.flip(arr1) exponentiation_result = np.power(arr1, arr2) print(exponentiation_result) [ 1 16 27 16 5] #Ans56 def count_substring(arr, substring): return np.char.count(arr, substring) arr = np.array(['hello', 'world', 'hello', 'numpy', 'hello']) substring_count = count_substring(arr, 'hello') print(substring_count) [1 0 1 0 1] #Ans57 def extract_uppercase(arr): return ["".join([char for char in string if char.isupper()]) for string in arr] arr = np.array(['Hello', 'World', 'OpenAI', 'GPT']) uppercase_chars = extract_uppercase(arr) print(uppercase_chars) ['H', 'W', 'OAI', 'GPT'] #Ans58 def replace_substring(arr, old_substring, new_substring): return np.char.replace(arr, old_substring, new_substring) arr = np.array(['apple', 'banana', 'grape', 'pineapple']) replaced_array = replace_substring(arr, 'apple', 'fruit') print(replaced_array) ['fruit' 'banana' 'grape' 'pinefruit'] #Ans59 def concatenate_strings(arr1, arr2): return np.char.add(arr1, arr2) arr1 = np.array(['Hello', 'World']) arr2 = np.array(['Open', 'AI']) concatenated_strings = concatenate_strings(arr1, arr2) print(concatenated_strings) ['HelloOpen' 'WorldAI']
#Ans60 def longest_string_length(arr): string_lengths = np.char.str_len(arr) return np.max(string_lengths) arr = np.array(['apple', 'banana', 'grape', 'pineapple']) longest_length = longest_string_length(arr) print(longest_length) 9 #Ans61 dataset = np.random.randint(1, 1001, 100) mean_value = np.mean(dataset) median_value = np.median(dataset) variance_value = np.var(dataset) std_deviation = np.std(dataset) print(mean_value, median_value, variance_value, std_deviation) 472.57 442.0 69238.18509999999 263.1314977344977 #Ans62 random_array = np.random.randint(1, 101, 50) percentile_25 = np.percentile(random_array, 25) percentile_75 = np.percentile(random_array, 75) print(percentile_25, percentile_75) 27.5 76.0 #Ans63 arr1 = np.random.rand(10) arr2 = np.random.rand(10) correlation_coefficient = np.corrcoef(arr1, arr2) print(correlation_coefficient) [[1. 0.17905183] [0.17905183 1. ]] #Ans64 matrix1 = np.random.rand(3, 3) matrix2 = np.random.rand(3, 3) matrix_product = np.dot(matrix1, matrix2) print(matrix_product)
[[0.25515391 1.08744736 0.91987587] [0.09307666 0.64331015 0.8951426 ] [0.16259198 1.01694721 1.16095727]] #Ans65 int_array = np.random.randint(10, 1001, 50) percentile_10 = np.percentile(int_array, 10) percentile_50 = np.percentile(int_array, 50) percentile_90 = np.percentile(int_array, 90) quartile_1 = np.percentile(int_array, 25) quartile_3 = np.percentile(int_array, 75) print(percentile_10, percentile_50, percentile_90, quartile_1, quartile_3) 86.8 490.5 881.4 182.5 749.25 #Ans66 arr = np.array([10, 20, 30, 40, 50]) index = np.where(arr == 30) print(index) (array([2]),) #Ans67 random_array = np.random.randint(1, 100, 10) sorted_array = np.sort(random_array) print(sorted_array) [ 3 6 17 19 44 49 56 63 70 71] #Ans68 arr = np.array([12, 25, 6, 42, 8, 30]) filtered_array = arr[arr > 20] print(filtered_array) [25 42 30] #Ans69 arr = np.array([1, 5, 8, 12, 15]) filtered_array = arr[arr % 3 == 0] print(filtered_array) [12 15] #Ans70 arr = np.array([10, 20, 30, 40, 50])
filtered_array = arr[(arr >= 20) & (arr <= 40)] print(filtered_array) [20 30 40] #Ans71 arr = np.array([1, 2, 3]) byte_order = arr.dtype.byteorder print(byte_order) = #Ans72 arr = np.array([1, 2, 3], dtype=np.int32) arr.byteswap(inplace=True) print(arr) [16777216 33554432 50331648] #Ans73 arr = np.array([1, 2, 3], dtype=np.int32) swapped_arr = arr.newbyteorder() print(swapped_arr) [16777216 33554432 50331648] #Ans74 arr = np.array([1, 2, 3], dtype=np.int32) if arr.dtype.byteorder == '=': swapped_arr = arr.newbyteorder() else: swapped_arr = arr print(swapped_arr) [16777216 33554432 50331648] #Ans75 arr = np.array([1, 2, 3], dtype=np.int32) byte_order = arr.dtype.byteorder if byte_order == '=': print("Byte swapping is necessary.") else: print("Byte swapping is not necessary.") Byte swapping is necessary.
#Ans76 arr1 = np.arange(1, 11) copy_arr = arr1.copy() copy_arr[0] = 100 print("Original array:", arr1) print("Modified copy:", copy_arr) Original array: [ 1 2 3 4 5 6 7 8 9 10] Modified copy: [100 2 3 4 5 6 7 8 9 10] #Ans77 matrix = np.random.randint(1, 10, (3, 3)) view_slice = matrix[:, 1:3] view_slice[0, 0] = 100 print("Original matrix:n", matrix) print("Modified slice:n", view_slice) Original matrix: [[ 2 100 3] [ 3 8 5] [ 9 6 4]] Modified slice: [[100 3] [ 8 5] [ 6 4]] #Ans78 array_a = np.arange(1, 13).reshape(4, 3) view_b = array_a[:, 1:3] view_b += 5 print("Original array:n", array_a) print("Modified view:n", view_b) Original array: [[ 1 7 8] [ 4 10 11] [ 7 13 14] [10 16 17]] Modified view: [[ 7 8] [10 11]
[13 14] [16 17]] #Ans79 orig_array = np.arange(1, 9).reshape(2, 4) reshaped_view = orig_array.reshape(4, 2) reshaped_view[0, 0] = 100 print("Original array:n", orig_array) print("Reshaped view:n", reshaped_view) Original array: [[100 2 3 4] [ 5 6 7 8]] Reshaped view: [[100 2] [ 3 4] [ 5 6] [ 7 8]] #Ans80 data = np.random.randint(1, 10, (3, 4)) data_copy = data[data > 5].copy() data_copy[0] = 100 print("Original data:n", data) print("Modified copy:n", data_copy) Original data: [[3 1 9 7] [3 1 8 9] [1 2 6 3]] Modified copy: [100 7 8 9 6] #Ans81 A = np.random.randint(1, 10, (3, 3)) B = np.random.randint(1, 10, (3, 3)) matrix_addition = A + B matrix_subtraction = A - B print("Matrix A:n", A) print("Matrix B:n", B) print("Matrix Addition:n", matrix_addition) print("Matrix Subtraction:n", matrix_subtraction)
Matrix A: [[6 1 7] [9 2 2] [7 6 1]] Matrix B: [[6 9 9] [2 8 1] [7 9 7]] Matrix Addition: [[12 10 16] [11 10 3] [14 15 8]] Matrix Subtraction: [[ 0 -8 -2] [ 7 -6 1] [ 0 -3 -6]] #Ans82 C = np.random.randint(1, 10, (3, 2)) D = np.random.randint(1, 10, (2, 4)) matrix_multiplication = np.dot(C, D) print(matrix_multiplication) [[ 62 69 101 48] [ 28 58 58 32] [ 16 35 34 19]] #Ans83 E = np.random.randint(1, 10, (3, 3)) transpose_E = E.T print("Original matrix:n", E) print("Transpose:n", transpose_E) Original matrix: [[6 3 7] [1 8 4] [8 1 2]] Transpose: [[6 1 8] [3 8 1] [7 4 2]] #Ans84 F = np.random.randint(1, 10, (3, 3)) determinant_F = np.linalg.det(F) print("Matrix F:n", F) print("Determinant:", determinant_F)
Matrix F: [[9 6 8] [9 6 3] [4 9 7]] Determinant: 285.00000000000006 #Ans85 G = np.random.randint(1, 10, (3, 3)) inverse_G = np.linalg.inv(G) print("Matrix G:n", G) print("Inverse:n", inverse_G) Matrix G: [[4 4 8] [4 4 7] [1 7 4]] Inverse: [[-1.375 1.66666667 -0.16666667] [-0.375 0.33333333 0.16666667] [ 1. -1. -0. ]]

Numpy questions with answers and practice

  • 1.
    NumPy Assignment #Ans1 import numpyas np arr = np.arange(6) print(arr) print(arr.dtype) [0 1 2 3 4 5] int64 #Ans2 arr = np.array([1.5, 2.6, 3.7]) if arr.dtype == np.float64: print("Data type is float64") else: print("Data type is not float64") Data type is float64 #Ans3 arr = np.array([1+2j, 3+4j, 5+6j], dtype=np.complex128) print(arr) print(arr.dtype) [1.+2.j 3.+4.j 5.+6.j] complex128 #Ans4 arr = np.array([1, 2, 3, 4, 5]) arr_float32 = arr.astype(np.float32) print(arr_float32) print(arr_float32.dtype) [1. 2. 3. 4. 5.] float32 #Ans5 arr = np.array([1.123456789, 2.123456789, 3.123456789], dtype=np.float64) arr_float32 = arr.astype(np.float32) print(arr_float32) print(arr_float32.dtype) [1.1234568 2.1234567 3.1234567] float32 #Ans6 def array_attributes(arr): return arr.shape, arr.size, arr.dtype
  • 2.
    arr = np.array([1,2, 3]) print(array_attributes(arr)) ((3,), 3, dtype('int64')) #Ans7 def array_dimension(arr): return arr.ndim arr = np.array([[1, 2], [3, 4]]) print(array_dimension(arr)) 2 #Ans8 def item_size_info(arr): return arr.itemsize, arr.nbytes arr = np.array([1, 2, 3, 4, 5]) print(item_size_info(arr)) (8, 40) #Ans9 def array_strides(arr): return arr.strides arr = np.array([[1, 2], [3, 4]]) print(array_strides(arr)) (16, 8) #Ans10 def shape_stride_relationship(arr): return arr.shape, arr.strides arr = np.array([[1, 2], [3, 4]]) print(shape_stride_relationship(arr)) ((2, 2), (16, 8)) #Ans11 def create_zeros_array(n): return np.zeros(n) print(create_zeros_array(5)) [0. 0. 0. 0. 0.] #Ans12 def create_ones_matrix(rows, cols):
  • 3.
    return np.ones((rows, cols)) print(create_ones_matrix(3,4)) [[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]] #Ans13 def generate_range_array(start, stop, step): return np.arange(start, stop, step) print(generate_range_array(1, 10, 2)) [1 3 5 7 9] #Ans14 def generate_linear_space(start, stop, num): return np.linspace(start, stop, num) print(generate_linear_space(1.0, 5.0, 10)) [1. 1.44444444 1.88888889 2.33333333 2.77777778 3.22222222 3.66666667 4.11111111 4.55555556 5. ] #Ans15 def create_identity_matrix(n): return np.eye(n) print(create_identity_matrix(4)) [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]] #Ans16 def list_to_array(py_list): return np.array(py_list) print(list_to_array([1, 2, 3, 4, 5])) [1 2 3 4 5] #Ans17 arr = np.array([1, 2, 3, 4, 5]) arr_view = arr.view() print(arr_view) [1 2 3 4 5]
  • 4.
    #Ans18 def concatenate_arrays(arr1, arr2,axis=0): return np.concatenate((arr1, arr2), axis=axis) arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) print(concatenate_arrays(arr1, arr2, axis=0)) [[1 2] [3 4] [5 6] [7 8]] #Ans19 arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5], [6]]) print(np.concatenate((arr1, arr2), axis=1)) [[1 2 5] [3 4 6]] #Ans20 def vertical_stack(arr_list): return np.vstack(arr_list) arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) arr3 = np.array([7, 8, 9]) print(vertical_stack([arr1, arr2, arr3])) [[1 2 3] [4 5 6] [7 8 9]] #Ans21 def create_array_in_range(start, stop, step): return np.arange(start, stop+1, step) print(create_array_in_range(1, 10, 2)) [1 3 5 7 9] #Ans22 def equally_spaced_values(): return np.linspace(0, 1, 10) print(equally_spaced_values()) [0. 0.11111111 0.22222222 0.33333333 0.44444444 0.55555556 0.66666667 0.77777778 0.88888889 1. ]
  • 5.
    #Ans23 def log_spaced_values(): return np.logspace(0,3, 5) print(log_spaced_values()) [ 1. 5.62341325 31.6227766 177.827941 1000. ] #Ans24 import pandas as pd def create_dataframe(): data = np.random.randint(1, 101, size=(5, 3)) return pd.DataFrame(data, columns=['A', 'B', 'C']) df = create_dataframe() print(df) A B C 0 49 18 46 1 55 61 78 2 8 27 63 3 60 12 1 4 71 66 33 #Ans25 def replace_negatives_with_zero(df, column_name): df[column_name] = np.where(df[column_name] < 0, 0, df[column_name]) return df df = pd.DataFrame({'A': [1, -2, 3], 'B': [-1, 5, -3]}) print(replace_negatives_with_zero(df, 'A')) A B 0 1 -1 1 0 5 2 3 -3 #Ans26 arr = np.array([10, 20, 30, 40, 50]) print(arr[2]) 30 #Ans27 arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(arr_2d[1, 2]) 6
  • 6.
    #Ans28 arr = np.array([3,8, 2, 10, 5, 7]) print(arr[arr > 5]) [ 8 10 7] #Ans29 arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) print(arr[2:6]) [3 4 5 6] #Ans30 arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(arr_2d[0:2, 1:3]) [[2 3] [5 6]] #Ans31 def extract_elements(arr, indices): return arr[indices] arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) indices = np.array([0, 2]) print(extract_elements(arr, indices)) [[1 2 3] [7 8 9]] #Ans32 def filter_greater_than(arr, threshold): return arr[arr > threshold] arr = np.array([1, 5, 10, 15, 20]) print(filter_greater_than(arr, 10)) [15 20] #Ans33 def extract_from_3d_array(arr, idx1, idx2, idx3): return arr[idx1, idx2, idx3] arr = np.random.randint(0, 10, (4, 4, 4)) idx1 = np.array([0, 1, 2]) idx2 = np.array([1, 2, 3]) idx3 = np.array([2, 3, 1]) print(extract_from_3d_array(arr, idx1, idx2, idx3)) [7 8 7]
  • 7.
    #Ans34 def filter_with_conditions(arr): return arr[(arr> 10) & (arr < 20)] arr = np.array([5, 12, 18, 25]) print(filter_with_conditions(arr)) [12 18] #Ans35 def extract_using_indices(arr, row_indices, col_indices): return arr[row_indices, col_indices] arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) row_indices = np.array([0, 1, 2]) col_indices = np.array([2, 1, 0]) print(extract_using_indices(arr, row_indices, col_indices)) [3 5 7] #Ans36 arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(arr + 5) [[ 6 7 8] [ 9 10 11] [12 13 14]] #Ans37 arr1 = np.array([[1, 2, 3]]) arr2 = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) print(arr2 * arr1.T) [[ 1 2 3 4] [10 12 14 16] [27 30 33 36]] #Ans38 arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) print(arr2 + arr1[:, np.newaxis]) [[ 2 3 4] [ 6 7 8] [10 11 12] [14 15 16]] #Ans39 arr1 = np.array([[1], [2], [3]]) arr2 = np.array([[1, 2, 3]]) print(arr1 + arr2)
  • 8.
    [[2 3 4] [34 5] [4 5 6]] #Ans40 arr1 = np.array([[1, 2, 3], [4, 5, 6]]) arr2 = np.array([[1, 2], [3, 4]]) # Broadcasting won't work due to shape incompatibility. One way to solve it is reshaping: arr2_reshaped = np.repeat(arr2, 2, axis=1)[:, :-1] # Repeat elements along axis 1 and remove the last column print(arr1 * arr2_reshaped) [[ 1 2 6] [12 15 24]] #Ans41 arr = np.array([[1, 2, 3], [4, 5, 6]]) print(np.mean(arr, axis=0)) [2.5 3.5 4.5] #Ans42 arr = np.array([[1, 2, 3], [4, 5, 6]]) print(np.max(arr, axis=1)) [3 6] #Ans43 arr = np.array([[1, 2, 3], [4, 5, 6]]) print(np.argmax(arr, axis=0)) [1 1 1] #Ans44 def moving_sum(arr): return np.cumsum(arr, axis=1) arr = np.array([[1, 2, 3], [4, 5, 6]]) print(moving_sum(arr)) [[ 1 3 6] [ 4 9 15]] #Ans45 arr = np.array([[2, 4, 6], [3, 5, 7]]) print(np.all(arr % 2 == 0, axis=0)) [False False False]
  • 9.
    #Ans46 original_array = np.array([1,2, 3, 4, 5, 6]) reshaped_matrix = original_array.reshape(2, 3) print(reshaped_matrix) [[1 2 3] [4 5 6]] #Ans47 def flatten_matrix(matrix): return matrix.flatten() input_matrix = np.array([[1, 2, 3], [4, 5, 6]]) flattened_array = flatten_matrix(input_matrix) print(flattened_array) [1 2 3 4 5 6] #Ans48 array1 = np.array([[1, 2], [3, 4]]) array2 = np.array([[5, 6], [7, 8]]) # Concatenating along axis 0 (rows) concatenated_array = np.concatenate((array1, array2), axis=0) print(concatenated_array) print(" ") #for space between result # Concatenating along axis 1 (columns) concatenated_array_axis1 = np.concatenate((array1, array2), axis=1) print(concatenated_array_axis1) [[1 2] [3 4] [5 6] [7 8]] [[1 2 5 6] [3 4 7 8]] #Ans49 def split_array(arr, num_splits): return np.array_split(arr, num_splits, axis=0) original_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) sub_arrays = split_array(original_array, 3) print(sub_arrays) [array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]])] #Ans50 def insert_and_delete_elements(arr, indices_to_insert,
  • 10.
    values_to_insert, indices_to_delete): modified_array =np.insert(arr, indices_to_insert, values_to_insert) modified_array = np.delete(modified_array, indices_to_delete) return modified_array original_array = np.array([1, 2, 3, 4, 5]) indices_to_insert = [2, 4] values_to_insert = [10, 11] indices_to_delete = [1, 3] result = insert_and_delete_elements(original_array, indices_to_insert, values_to_insert, indices_to_delete) print(result) [ 1 10 4 11 5] #Ans51 arr1 = np.random.randint(1, 11, 10) arr2 = np.arange(1, 11) addition_result = np.add(arr1, arr2) print(addition_result) [ 2 9 9 10 9 11 9 12 16 19] #Ans52 arr1 = np.arange(10, 0, -1) arr2 = np.arange(1, 11) subtraction_result = np.subtract(arr1, arr2) print(subtraction_result) [ 9 7 5 3 1 -1 -3 -5 -7 -9] #Ans53 arr1 = np.random.randint(1, 11, 5) arr2 = np.arange(1, 6) multiplication_result = np.multiply(arr1, arr2) print(multiplication_result) [ 9 8 3 32 20] #Ans54 arr1 = np.arange(2, 11, 2) arr2 = np.arange(1, 6) division_result = np.divide(arr1, arr2) print(division_result) [2. 2. 2. 2. 2.]
  • 11.
    #Ans55 arr1 = np.arange(1,6) arr2 = np.flip(arr1) exponentiation_result = np.power(arr1, arr2) print(exponentiation_result) [ 1 16 27 16 5] #Ans56 def count_substring(arr, substring): return np.char.count(arr, substring) arr = np.array(['hello', 'world', 'hello', 'numpy', 'hello']) substring_count = count_substring(arr, 'hello') print(substring_count) [1 0 1 0 1] #Ans57 def extract_uppercase(arr): return ["".join([char for char in string if char.isupper()]) for string in arr] arr = np.array(['Hello', 'World', 'OpenAI', 'GPT']) uppercase_chars = extract_uppercase(arr) print(uppercase_chars) ['H', 'W', 'OAI', 'GPT'] #Ans58 def replace_substring(arr, old_substring, new_substring): return np.char.replace(arr, old_substring, new_substring) arr = np.array(['apple', 'banana', 'grape', 'pineapple']) replaced_array = replace_substring(arr, 'apple', 'fruit') print(replaced_array) ['fruit' 'banana' 'grape' 'pinefruit'] #Ans59 def concatenate_strings(arr1, arr2): return np.char.add(arr1, arr2) arr1 = np.array(['Hello', 'World']) arr2 = np.array(['Open', 'AI']) concatenated_strings = concatenate_strings(arr1, arr2) print(concatenated_strings) ['HelloOpen' 'WorldAI']
  • 12.
    #Ans60 def longest_string_length(arr): string_lengths =np.char.str_len(arr) return np.max(string_lengths) arr = np.array(['apple', 'banana', 'grape', 'pineapple']) longest_length = longest_string_length(arr) print(longest_length) 9 #Ans61 dataset = np.random.randint(1, 1001, 100) mean_value = np.mean(dataset) median_value = np.median(dataset) variance_value = np.var(dataset) std_deviation = np.std(dataset) print(mean_value, median_value, variance_value, std_deviation) 472.57 442.0 69238.18509999999 263.1314977344977 #Ans62 random_array = np.random.randint(1, 101, 50) percentile_25 = np.percentile(random_array, 25) percentile_75 = np.percentile(random_array, 75) print(percentile_25, percentile_75) 27.5 76.0 #Ans63 arr1 = np.random.rand(10) arr2 = np.random.rand(10) correlation_coefficient = np.corrcoef(arr1, arr2) print(correlation_coefficient) [[1. 0.17905183] [0.17905183 1. ]] #Ans64 matrix1 = np.random.rand(3, 3) matrix2 = np.random.rand(3, 3) matrix_product = np.dot(matrix1, matrix2) print(matrix_product)
  • 13.
    [[0.25515391 1.08744736 0.91987587] [0.093076660.64331015 0.8951426 ] [0.16259198 1.01694721 1.16095727]] #Ans65 int_array = np.random.randint(10, 1001, 50) percentile_10 = np.percentile(int_array, 10) percentile_50 = np.percentile(int_array, 50) percentile_90 = np.percentile(int_array, 90) quartile_1 = np.percentile(int_array, 25) quartile_3 = np.percentile(int_array, 75) print(percentile_10, percentile_50, percentile_90, quartile_1, quartile_3) 86.8 490.5 881.4 182.5 749.25 #Ans66 arr = np.array([10, 20, 30, 40, 50]) index = np.where(arr == 30) print(index) (array([2]),) #Ans67 random_array = np.random.randint(1, 100, 10) sorted_array = np.sort(random_array) print(sorted_array) [ 3 6 17 19 44 49 56 63 70 71] #Ans68 arr = np.array([12, 25, 6, 42, 8, 30]) filtered_array = arr[arr > 20] print(filtered_array) [25 42 30] #Ans69 arr = np.array([1, 5, 8, 12, 15]) filtered_array = arr[arr % 3 == 0] print(filtered_array) [12 15] #Ans70 arr = np.array([10, 20, 30, 40, 50])
  • 14.
    filtered_array = arr[(arr>= 20) & (arr <= 40)] print(filtered_array) [20 30 40] #Ans71 arr = np.array([1, 2, 3]) byte_order = arr.dtype.byteorder print(byte_order) = #Ans72 arr = np.array([1, 2, 3], dtype=np.int32) arr.byteswap(inplace=True) print(arr) [16777216 33554432 50331648] #Ans73 arr = np.array([1, 2, 3], dtype=np.int32) swapped_arr = arr.newbyteorder() print(swapped_arr) [16777216 33554432 50331648] #Ans74 arr = np.array([1, 2, 3], dtype=np.int32) if arr.dtype.byteorder == '=': swapped_arr = arr.newbyteorder() else: swapped_arr = arr print(swapped_arr) [16777216 33554432 50331648] #Ans75 arr = np.array([1, 2, 3], dtype=np.int32) byte_order = arr.dtype.byteorder if byte_order == '=': print("Byte swapping is necessary.") else: print("Byte swapping is not necessary.") Byte swapping is necessary.
  • 15.
    #Ans76 arr1 = np.arange(1,11) copy_arr = arr1.copy() copy_arr[0] = 100 print("Original array:", arr1) print("Modified copy:", copy_arr) Original array: [ 1 2 3 4 5 6 7 8 9 10] Modified copy: [100 2 3 4 5 6 7 8 9 10] #Ans77 matrix = np.random.randint(1, 10, (3, 3)) view_slice = matrix[:, 1:3] view_slice[0, 0] = 100 print("Original matrix:n", matrix) print("Modified slice:n", view_slice) Original matrix: [[ 2 100 3] [ 3 8 5] [ 9 6 4]] Modified slice: [[100 3] [ 8 5] [ 6 4]] #Ans78 array_a = np.arange(1, 13).reshape(4, 3) view_b = array_a[:, 1:3] view_b += 5 print("Original array:n", array_a) print("Modified view:n", view_b) Original array: [[ 1 7 8] [ 4 10 11] [ 7 13 14] [10 16 17]] Modified view: [[ 7 8] [10 11]
  • 16.
    [13 14] [16 17]] #Ans79 orig_array= np.arange(1, 9).reshape(2, 4) reshaped_view = orig_array.reshape(4, 2) reshaped_view[0, 0] = 100 print("Original array:n", orig_array) print("Reshaped view:n", reshaped_view) Original array: [[100 2 3 4] [ 5 6 7 8]] Reshaped view: [[100 2] [ 3 4] [ 5 6] [ 7 8]] #Ans80 data = np.random.randint(1, 10, (3, 4)) data_copy = data[data > 5].copy() data_copy[0] = 100 print("Original data:n", data) print("Modified copy:n", data_copy) Original data: [[3 1 9 7] [3 1 8 9] [1 2 6 3]] Modified copy: [100 7 8 9 6] #Ans81 A = np.random.randint(1, 10, (3, 3)) B = np.random.randint(1, 10, (3, 3)) matrix_addition = A + B matrix_subtraction = A - B print("Matrix A:n", A) print("Matrix B:n", B) print("Matrix Addition:n", matrix_addition) print("Matrix Subtraction:n", matrix_subtraction)
  • 17.
    Matrix A: [[6 17] [9 2 2] [7 6 1]] Matrix B: [[6 9 9] [2 8 1] [7 9 7]] Matrix Addition: [[12 10 16] [11 10 3] [14 15 8]] Matrix Subtraction: [[ 0 -8 -2] [ 7 -6 1] [ 0 -3 -6]] #Ans82 C = np.random.randint(1, 10, (3, 2)) D = np.random.randint(1, 10, (2, 4)) matrix_multiplication = np.dot(C, D) print(matrix_multiplication) [[ 62 69 101 48] [ 28 58 58 32] [ 16 35 34 19]] #Ans83 E = np.random.randint(1, 10, (3, 3)) transpose_E = E.T print("Original matrix:n", E) print("Transpose:n", transpose_E) Original matrix: [[6 3 7] [1 8 4] [8 1 2]] Transpose: [[6 1 8] [3 8 1] [7 4 2]] #Ans84 F = np.random.randint(1, 10, (3, 3)) determinant_F = np.linalg.det(F) print("Matrix F:n", F) print("Determinant:", determinant_F)
  • 18.
    Matrix F: [[9 68] [9 6 3] [4 9 7]] Determinant: 285.00000000000006 #Ans85 G = np.random.randint(1, 10, (3, 3)) inverse_G = np.linalg.inv(G) print("Matrix G:n", G) print("Inverse:n", inverse_G) Matrix G: [[4 4 8] [4 4 7] [1 7 4]] Inverse: [[-1.375 1.66666667 -0.16666667] [-0.375 0.33333333 0.16666667] [ 1. -1. -0. ]]