0% found this document useful (0 votes)
14 views

Data Science Assignment 2 Solutions

Uploaded by

k626856k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Data Science Assignment 2 Solutions

Uploaded by

k626856k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Name: Kunal Yadav

Enrollment Number: 0901CD211031

Assignment: Data Science Assignment-2

Questions and Solutions

Question 1: Array Manipulation

Create a 2D NumPy array with shape (3, 4) containing consecutive integers starting from 1.

Reshape the array to have shape (2, 6).

Code:

import numpy as np

array = np.arange(1, 13).reshape(3, 4)

reshaped_array = array.reshape(2, 6)

print("Original Array:\n", array)

print("Reshaped Array:\n", reshaped_array)

---

Question 2: Statistical Operations

Generate a random 1D NumPy array of size 20 with values ranging from 0 to 50. Calculate the

mean, median, and standard deviation of the array.

Code:

import numpy as np

array = np.random.randint(0, 51, 20)


mean = np.mean(array)

median = np.median(array)

std_dev = np.std(array)

print("Array:", array)

print("Mean:", mean)

print("Median:", median)

print("Standard Deviation:", std_dev)

---

Question 3: Broadcasting and Element-wise Operations

Create a 3x3 matrix with values 1, 2, and 3 along its diagonal. Subtract the mean of each row from

the entire row.

Code:

import numpy as np

matrix = np.diag([1, 2, 3])

row_means = np.mean(matrix, axis=1).reshape(-1, 1)

adjusted_matrix = matrix - row_means

print("Original Matrix:\n", matrix)

print("Adjusted Matrix:\n", adjusted_matrix)

---

Question 4: Masking and Filtering


Given a 1D NumPy array, find and print all values greater than the mean of the array.

Code:

import numpy as np

array = np.random.randint(0, 100, 15)

mean = np.mean(array)

greater_than_mean = array[array > mean]

print("Array:", array)

print("Mean:", mean)

print("Values greater than mean:", greater_than_mean)

---

Question 5: Stacking and Splitting

Create two 2D arrays with the same number of columns. Stack them vertically and then split the

resulting array back into the original two arrays.

Code:

import numpy as np

array1 = np.random.randint(1, 10, (2, 3))

array2 = np.random.randint(1, 10, (2, 3))

stacked_array = np.vstack((array1, array2))

split_array1, split_array2 = np.vsplit(stacked_array, 2)

print("Array 1:\n", array1)

print("Array 2:\n", array2)


print("Stacked Array:\n", stacked_array)

print("Split Array 1:\n", split_array1)

print("Split Array 2:\n", split_array2)

---

Question 6: Boolean Indexing

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 = np.random.randint(0, 11, 10)

array[array > 5] = 0

print("Modified Array:", array)

---

Question 7: Reshaping and Transposing

Create a 2D NumPy array with shape (2, 3) and then transpose it to have shape (3, 2).

Code:

import numpy as np

array = np.random.randint(1, 10, (2, 3))

transposed_array = array.T
print("Original Array:\n", array)

print("Transposed Array:\n", transposed_array)

---

Question 8: Advanced Indexing

Given a 2D NumPy array, extract a new array containing elements from the second and third rows

and the first and third columns.

Code:

import numpy as np

array = np.random.randint(1, 10, (4, 4))

selected_elements = array[1:3, [0, 2]]

print("Array:\n", array)

print("Selected Elements:\n", selected_elements)

---

Question 9: Element-wise Arithmetic

Create a 2D NumPy array with random integers between 1 and 10. Add 5 to all even numbers and

subtract 3 from all odd numbers.

Code:

import numpy as np

array = np.random.randint(1, 11, (3, 3))

array[array % 2 == 0] += 5
array[array % 2 != 0] -= 3

print("Modified Array:\n", array)

---

Question 10: Matrix Multiplication

Perform matrix multiplication between two given 2D NumPy arrays, and then find the sum of the

resulting matrix's diagonal elements.

Code:

import numpy as np

array1 = np.random.randint(1, 5, (2, 2))

array2 = np.random.randint(1, 5, (2, 2))

result_matrix = np.dot(array1, array2)

diagonal_sum = np.trace(result_matrix)

print("Array 1:\n", array1)

print("Array 2:\n", array2)

print("Result Matrix:\n", result_matrix)

print("Sum of Diagonal Elements:", diagonal_sum)

You might also like