Numpy Array Exercises#

Question-1#

Create a 5x5 2D NumPy array with random values between 0 and 1:

  • Set a fixed seed for np.random to ensure reproducibility.

  • Replace all values in the second and fourth columns with 1 using indexing or slicing.

  • Replace all values in the second and fourth rows with 0 using indexing or slicing. Plot the final array.

Question-2#

Select 4 images from the digits dataset and combine their NumPy arrays to create a single image arranged in a 2x2 grid.

  • Place 2 images on the top row and the other 2 images directly below them.

Question-3#

Generate an array of 50 random names with lengths of 3, 4, or 5 characters.

  • The length of each name should be chosen randomly, and the characters should be selected randomly.

Question-4#

Create a 2D array with 50 rows and 5 columns containing values between 0 and 100. These values represent the grades for Test-1, Test-2, Test-3, Final, and LAB for each student, with their names generated in the previous question. Calculate the weighted percentage grade using the following weights:

Weighted average = 0.15 * Test-1 + 0.20 * Test-2 + 0.25 * Test-3 + 0.30 * Final + 0.10 * LAB

A student passes if their weighted average is greater than 55; otherwise, they fail. For each test, draw random values from a normal distribution with means chosen randomly between 20 and 80, and standard deviations between 5 and 10.

You may round the values if necessary.

Store the names, grades, weighted averages, and the corresponding pass/fail result in a single array. Add a header row with the column labels: Name, Test-1, Test-2, Test-3, Final, LAB, Result.

The first two rows of the output:

[[‘Name’ ‘Test-1’ ‘Test-2’ ‘Test-3’ ‘Final’ ‘LAB’ ‘Weighted Average’ ‘Result’]
[‘pva’ ‘0.0’ ‘0.0’ ‘0.0’ ‘0.0’ ‘0.0’ ‘0.0’ ‘Fail’]]

Question-5#

Create a 10x10 array with ones along both diagonals and zeros in all other positions. Plot the array so that it forms an “X” shape.

Question-6#

Generate 10,000 randomly chosen passwords, each consisting of five lowercase letters and three numbers in any order.