Data Science Assignment 2 Solutions
Data Science Assignment 2 Solutions
Create a 2D NumPy array with shape (3, 4) containing consecutive integers starting from 1.
Code:
import numpy as np
reshaped_array = array.reshape(2, 6)
---
Generate a random 1D NumPy array of size 20 with values ranging from 0 to 50. Calculate the
Code:
import numpy as np
median = np.median(array)
std_dev = np.std(array)
print("Array:", array)
print("Mean:", mean)
print("Median:", median)
---
Create a 3x3 matrix with values 1, 2, and 3 along its diagonal. Subtract the mean of each row from
Code:
import numpy as np
---
Code:
import numpy as np
mean = np.mean(array)
print("Array:", array)
print("Mean:", mean)
---
Create two 2D arrays with the same number of columns. Stack them vertically and then split the
Code:
import numpy as np
---
Create a 1D NumPy array with random integers between 0 and 10. Replace all values greater than
5 with 0.
Code:
import numpy as np
array[array > 5] = 0
---
Create a 2D NumPy array with shape (2, 3) and then transpose it to have shape (3, 2).
Code:
import numpy as np
transposed_array = array.T
print("Original Array:\n", array)
---
Given a 2D NumPy array, extract a new array containing elements from the second and third rows
Code:
import numpy as np
print("Array:\n", array)
---
Create a 2D NumPy array with random integers between 1 and 10. Add 5 to all even numbers and
Code:
import numpy as np
array[array % 2 == 0] += 5
array[array % 2 != 0] -= 3
---
Perform matrix multiplication between two given 2D NumPy arrays, and then find the sum of the
Code:
import numpy as np
diagonal_sum = np.trace(result_matrix)