CSE1006 Foundations for Data Analytics ELA
Fall 2022-2023 Semester
Lab -12 (L45+L46) and Lab 11 -(L39+L40)
1. Write a NumPy program to create an array of 10 zeros, 10 ones, 10 fives.
Expected Output: An array of 10 zeros:
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
An array of 10 ones:
[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
An array of 10 fives:
[ 5. 5. 5. 5. 5. 5. 5. 5. 5. 5.]
2. Write a NumPy program to convert a list of numeric value into a one-
dimensional NumPy array.
Expected Output:
Original List: [12.23, 13.32, 100, 36.32]
One-dimensional NumPy array: [ 12.23 13.32 100. 36.32]
3. Write a NumPy program to create a 3x3 matrix with values ranging from
3 to 11.
Expected Output:
[[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
4. Write a NumPy program to create a null vector of size 10 and update
seventh value to 11.
Expected Output:
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
Update seventh value to 11
[ 0. 0. 0. 0. 0. 0. 11. 0. 0. 0.]
5. Write a NumPy program to create an array with values ranging from 12
to 38.
Expected Output:
[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]
6. Write a NumPy program to reverse an array (first element becomes
last).
Expected Output:
Original array:
[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]
Reverse array:
[37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15
14 13 12]
7. Write a NumPy program to convert an array to a float type.
Sample output:
Original array
[1, 2, 3, 4]
Array converted to a float type:
[ 1. 2. 3. 4.]
8. Write a NumPy program to create a two-dimensional array with 1 on the
border and 0 inside.
Original array:
[[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]]
1 on the border and 0 inside in the array
[[ 1. 1. 1. 1. 1.]
[ 1. 0. 0. 0. 1.]
[ 1. 0. 0. 0. 1.]
[ 1. 0. 0. 0. 1.]
[ 1. 1. 1. 1. 1.]]
9. Write a NumPy program to create a 8x8 matrix and fill it with a
checkerboard pattern.
Expected output:
Checkerboard pattern:
[[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]]
10. Write a NumPy program to convert a list and tuple into arrays.
Sample Output:
List to array:
[1 2 3 4 5 6 7 8]
Tuple to array:
[[8 4 6]
[1 2 3]]
11. Write a NumPy program to append values to the end of an array.
Expected Output:
Original array:
[10, 20, 30]
After append values to the end of the array:
[10 20 30 40 50 60 70 80 90]
12. Write a NumPy program to find the real and imaginary parts of an array
of complex numbers.
Expected Output:
Original array [ 1.00000000+0.j 0.70710678+0.70710678j]
Real part of the array:
[ 1. 0.70710678]
Imaginary part of the array:
[ 0. 0.70710678]
13. Write a NumPy program to get the unique elements of an array.
Expected Output:
Original array:
[10 10 20 20 30 30]
Unique elements of the above array:
[10 20 30]
Original array:
[[1 1]
[2 3]]
Unique elements of the above array:
[1 2 3]
14. Write a NumPy program to get the values and indices of the elements
that are bigger than 10 in a given array.
Original array:
[[ 0 10 20]
[20 30 40]]
Values bigger than 10 = [20 20 30 40]
Their indices are (array([0, 1, 1, 1]), array([2, 0, 1, 2]))
15. Write a NumPy program to create a new array of 3*5, filled with 2.
Expected Output:
[[2 2 2 2 2]
[2 2 2 2 2]
[2 2 2 2 2]]
[[2 2 2 2 2]
[2 2 2 2 2]
[2 2 2 2 2]]