python numpy assignment
python numpy assignment
Unit – 1 NumPy
Assignment
Program – 1
You have salary data for different job roles in the tech industry over three years (2015, 2016,
and 2017). The data is given in thousands of dollars:
• Data Scientist: [133, 132, 137]
• Product Manager: [127, 140, 145]
• Designer: [118, 118, 127]
• Software Engineer: [129, 131, 137]
Using NumPy, create an array to store this data and then check the number of dimensions of
the array. Write the code to create the NumPy array and find the number of dimensions.
Program – 2
You have the number of units sold by three different stores over five days. The data is:
• Store X: [20, 22, 18, 25, 30]
• Store Y: [15, 17, 14, 20, 22]
• Store Z: [25, 27, 30, 28, 26]
Using NumPy, create an array to store this data and find the maximum value in the array.
Program – 3
You have been provided with data representing various metrics for Department A over five
days. The data includes daily sales (in units), stock prices (in dollars), rainfall (in mm), working
hours (in hours), and player ages (in years). The data is given as follows:
• Department A:
o Sales: [150, 200, 250, 160, 210]
o Stock Prices: [110, 115, 120, 125, 130]
o Rainfall: [80, 90, 100, 110, 120]
o Working Hours: [40, 42, 45, 48, 50]
o Player Ages: [24, 28, 26, 30, 25]
Using NumPy, perform the following operations:
1. Create NumPy arrays to store the data for each metric.
2. Calculate the maximum sales.
3. Find the minimum stock price.
4. Calculate the total rainfall.
5. Calculate the average working hours per day.
6. Calculate the variance of player ages.
Write the code and provide the corresponding outputs for each operation.
Program – 4
You have been provided with new salary data (in $1000) for various job roles over five years
(2015, 2016, 2017, 2018, and 2019). The updated data is as follows:
• Data Scientist: [140, 145, 150, 155, 160]
• Product Manager: [135, 138, 142, 147, 150]
• Designer: [120, 125, 130, 135, 140]
• Software Engineer: [132, 134, 138, 142, 145]
• Marketing Specialist: [128, 133, 137, 140, 143]
• Sales Manager: [125, 130, 135, 140, 145]
Using NumPy, create an array to store this new data and then determine the number of job roles
and the number of years.
Program – 5
You have been provided with Apple's stock prices (in dollars) for the month of May 2018. The
data is given as follows:
prices = [ 189, 186, 186, 188, 187, 188, 188, 186, 188, 188, 187, 186 ]
Program – 6
You have been provided with rainfall data (in mm) for five cities over a month. The data is
given as follows:
rainfall = [80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320]
Program – 8
Given the matrix C: [[3, 2], [1, 4]]
• Calculate the determinant of matrix C.
• Find the inverse of matrix C (if it exists).
• Check if matrix C is symmetric.
• Find the eigenvalues and eigenvectors of matrix C.
• Compute the rank of matrix C.
Program – 9
Given the matrix E: [[2, 3, 1], [3, 6, 1], [1, 1, 2]]
• Calculate the inverse of matrix E.
• Perform a matrix multiplication of E with its inverse and verify that the result is the
identity matrix.
• Find the determinant of matrix E.
• Perform Cholesky decomposition on matrix E.
• Verify that E is equal to the product of the Cholesky factor and its transpose.
Program – 10
Given the array: ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
• Create a copy of arr using copy(), modify an element, and check if the original array
is affected.
• Create a view of arr using view(), modify an element, and check if the original array
is affected.
• Check if the copy shares the same memory as the original array.
• Check if the view shares the same memory as the original array.
Program – 11
Given the string:text = " Python Programming is Fun! "
1. Remove any leading and trailing spaces from the string using a string method.
2. Convert the string to all lowercase letters.
3. Convert the string to all uppercase letters.
4. Replace the word "Fun" with "Awesome".
5. Count how many times the letter "o" appears in the string.
Program – 12
Given the string:
sentence = "Data Science is an exciting field."
Extract the word "Science" from the string using slicing.
Check if the string contains the word "exciting".
Find the index of the word "an" in the string.
Replace "exciting" with "dynamic" and print the new string.
Split the string into a list of words.
Program – 13
Given the following 3D array arr:
([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]],
[[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]],
[[21, 22, 23, 24, 25], [26, 27, 28, 29, 30]],
[[31, 32, 33, 34, 35], [36, 37, 38, 39, 40]]])
Extract the element at position [2, 1, 3].
Slice the entire second row from each 2D slice of the array.
Slice the first two 2D matrices from the array.
Extract the last column of every row for the third 2D slice.
Slice the array to get the first 3 rows and the first 3 columns.
Program – 14
Given the following 3D array arr:
([[[10, 20, 30, 40], [50, 60, 70, 80], [90, 100, 110, 120]],
[[130, 140, 150, 160], [170, 180, 190, 200], [210, 220, 230, 240]],
[[250, 260, 270, 280], [290, 300, 310, 320], [330, 340, 350, 360]],
[[370, 380, 390, 400], [410, 420, 430, 440], [450, 460, 470, 480]]])
1. Extract the first 2D slice from the 3D array.
2. Extract the second row from the second 2D slice.
3. Extract the last column of all the rows in the last 2D slice.
4. Slice the first two 2D matrices and get the third row of each.
5. Slice the array to get the first 2D slice and the first two columns of each row.
Program – 15
Given the following 3D array arr:
([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]],
[[16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26, 27, 28, 29, 30]],
[[31, 32, 33, 34, 35], [36, 37, 38, 39, 40], [41, 42, 43, 44, 45]]])
1. Slice the entire first 2D matrix and extract every second element from each row.
2. Slice the second 2D matrix and extract the last three columns.
3. Extract the first row from both 2D matrices using slicing.
4. Slice all rows and columns with a step size of 2.
5. Extract the last row from the first 2D matrix.
Program – 16
Given the following 3D array arr:
([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]],
[[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]],
[[21, 22, 23, 24, 25], [26, 27, 28, 29, 30]],
[[31, 32, 33, 34, 35], [36, 37, 38, 39, 40]],
[[41, 42, 43, 44, 45], [46, 47, 48, 49, 50]]])
1. Slice to get the first 3 rows and the first 4 columns.
2. Extract the middle row from each 2D slice.
3. Slice the first 2D slice to get the second column and the last column.
4. Slice all 5 matrices, but only include columns 1 through 3.
5. Slice to get all rows and columns in the last 2D matrix.
Program – 17
Given the following 3D array arr:
([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[10, 11, 12], [13, 14, 15], [16, 17, 18]],
[[19, 20, 21], [22, 23, 24], [25, 26, 27]],
[[28, 29, 30], [31, 32, 33], [34, 35, 36]],
[[37, 38, 39], [40, 41, 42], [43, 44, 45]]])
1. Extract a subarray that contains the first 3D slice and the first column of each row.
2. Slice the array to get every second element from each row in all 5 slices.
3. Extract the first and second columns from all 2D matrices.
4. Slice the entire array to get all rows and only the last column of each row.
5. Slice the array to get the last 2D slice and the first two rows from it.