NumPy: Array Object Exercises, Practice, Solution
NumPy Array Object [205 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts.]
1. Write a NumPy program to print the NumPy version in your system. Go to the editor
Click me to see the sample solution
2. Write a NumPy program to convert a list of numeric value into a one-dimensional NumPy array. Go to the editor
Expected Output:
Original List: [12.23, 13.32, 100, 36.32]
One-dimensional NumPy array: [ 12.23 13.32 100. 36.32]
Click me to see the sample solution
3.Write a NumPy program to create a 3x3 matrix with values ranging from 2 to 10. Go to the editor
Expected Output:
[[ 2 3 4]
[ 5 6 7]
[ 8 9 10]]
Click me to see the sample solution
4. Write a NumPy program to create a null vector of size 10 and update sixth value to 11.Go to the editor
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
Update sixth value to 11
[ 0. 0. 0. 0. 0. 0. 11. 0. 0. 0.]
Click me to see the sample solution
5. Write a NumPy program to create an array with values ranging from 12 to 38.Go to the editor
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]
Click me to see the sample solution
6. Write a NumPy program to reverse an array (first element becomes last). Go to the editor
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]
Click me to see the sample solution
7. Write a NumPy program to convert an array to a float type. Go to the editor
Sample output:
Original array
[1, 2, 3, 4]
Array converted to a float type:
[ 1. 2. 3. 4.]
Click me to see the sample solution
8. Write a NumPy program to create a 2d array with 1 on the border and 0 inside. Go to the editor
Expected Output:
Original array:
[[ 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. 1. 1. 1. 1.]]
Click me to see the sample solution
9. Write a NumPy program to add a border (filled with 0's) around an existing array. Go to the editor
Expected Output:
Original array:
[[ 1. 1. 1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]
1 on the border and 0 inside in the array
[[ 0. 0. 0. 0. 0.]
...........
[ 0. 0. 0. 0. 0.]]
Click me to see the sample solution
10. Write a NumPy program to create a 8x8 matrix and fill it with a checkerboard pattern. Go to the editor
Checkerboard pattern:
[[0 1 0 1 0 1 0 1]
..........
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]]
Click me to see the sample solution
11. Write a NumPy program to convert a list and tuple into arrays. Go to the editor
List to array:
[1 2 3 4 5 6 7 8]
Tuple to array:
[[8 4 6]
[1 2 3]]
Click me to see the sample solution
12. Write a NumPy program to append values to the end of an array. Go to the editor
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]
Click me to see the sample solution
13. Write a NumPy program to create an empty and a full array. Go to the editor
Expected Output:
[ 6.93270651e-310 1.59262180e-316 6.93270559e-310 6.93270665e-310]
[ 6.93270667e-310 6.93270671e-310 6.93270668e-310 6.93270483e-310]
[ 6.93270668e-310 6.93270671e-310 6.93270370e-310 6.93270488e-310]]
[[6 6 6]
[6 6 6]
[6 6 6]]
Click me to see the sample solution
14. Write a NumPy program to convert the values of Centigrade degrees into Fahrenheit degrees and vice versa. Values are stored into a NumPy array. Go to the editor
Sample Array [0, 12, 45.21, 34, 99.91]
[-17.78, -11.11, 7.34, 1.11, 37.73, 0. ]
Expected Output:
Values in Fahrenheit degrees:
[ 0. 12. 45.21 34. 99.91 32. ]
Values in Centigrade degrees:
[-17.78 -11.11 7.34 1.11 37.73 0. ]
Values in Centigrade degrees:
[-17.78 -11.11 7.34 1.11 37.73 0. ]
Values in Fahrenheit degrees:
[-0. 12. 45.21 34. 99.91 32. ]
Click me to see the sample solution
15. Write a NumPy program to find the real and imaginary parts of an array of complex numbers. Go to the editor
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]
Click me to see the sample solution
16. Write a NumPy program to find the number of elements of an array, length of one array element in bytes and total bytes consumed by the elements. Go to the editor
Expected Output:
Size of the array: 3
Length of one array element in bytes: 8
Total bytes consumed by the elements of the array: 24
Click me to see the sample solution
17. Write a NumPy program to test whether each element of a 1-D array is also present in a second array. Go to the editor
Expected Output:
Array1: [ 0 10 20 40 60]
Array2: [0, 40]
Compare each element of array1 and array2
[ True False False True False]
Click me to see the sample solution
18. Write a NumPy program to find common values between two arrays. Go to the editor
Expected Output:
Array1: [ 0 10 20 40 60]
Array2: [10, 30, 40]
Common values between two arrays:
[10 40]
Click me to see the sample solution
19. Write a NumPy program to get the unique elements of an array. Go to the editor
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]
Click me to see the sample solution
20. Write a NumPy program to find the set difference of two arrays. The set difference will return the sorted, unique values in array1 that are not in array2. Go to the editor
Expected Output:
Array1: [ 0 10 20 40 60 80]
Array2: [10, 30, 40, 50, 70, 90]
Set difference between two arrays:
[ 0 20 60 80]
Click me to see the sample solution
21. Write a NumPy program to find the set exclusive-or of two arrays. Set exclusive-or will return the sorted, unique values that are in only one (not both) of the input arrays. Go to the editor
Array1: [ 0 10 20 40 60 80]
Array2: [10, 30, 40, 50, 70]
Unique values that are in only one (not both) of the input arrays:
[ 0 20 30 50 60 70 80]
Click me to see the sample solution
22. Write a NumPy program to find the union of two arrays. Union will return the unique, sorted array of values that are in either of the two input arrays. Go to the editor
Array1: [ 0 10 20 40 60 80]
Array2: [10, 30, 40, 50, 70]
Unique sorted array of values that are in either of the two input arrays:
[ 0 10 20 30 40 50 60 70 80]
Click me to see the sample solution
23. Write a NumPy program to test whether all elements in an array evaluate to True. Go to the editor
Note: 0 evaluates to False in NumPy.
Click me to see the sample solution
24. Write a NumPy program to test whether any array element along a given axis evaluates to True. Go to the editor
Note: 0 evaluates to False in NumPy.
Click me to see the sample solution
25. Write a NumPy program to construct an array by repeating. Go to the editor
Sample array: [1, 2, 3, 4]
Expected Output:
Original array
[1, 2, 3, 4]
Repeating 2 times
[1 2 3 4 1 2 3 4]
Repeating 3 times
[1 2 3 4 1 2 3 4 1 2 3 4]
Click me to see the sample solution
26. Write a NumPy program to repeat elements of an array. Go to the editor
Expected Output:
[3 3 3 3]
[1 1 2 2 3 3 4 4]
Click me to see the sample solution
27. Write a NumPy program to find the indices of the maximum and minimum values along the given axis of an array. Go to the editor
Original array: [1 2 3 4 5 6]
Maximum Values: 5
Minimum Values: 0
Click me to see the sample solution
28. Write a NumPy program compare two given arrays. Go to the editor
Array a: [1 2]
Array b: [4 5]
a > b
[False False]
a >= b
[False False]
a < b
[ True True]
a <= b
[ True True]
Click me to see the sample solution
29. Write a NumPy program to sort an along the first, last axis of an array. Go to the editor
Sample array: [[2,5],[4,4]]
Expected Output:
Original array:
[[4 6]
[2 1]]
Sort along the first axis:
[[2 1]
[4 6]]
Sort along the last axis:
[[1 2]
[4 6]]
Click me to see the sample solution
30. Write a NumPy program to sort pairs of first name and last name return their indices. (first by last name, then by first name). Go to the editor
first_names = (Betsey, Shelley, Lanell, Genesis, Margery)
last_names = (Battle, Brien, Plotner, Stahl, Woolum)
Expected Output:
[1 3 2 4 0]
Click me to see the sample solution
31. Write a NumPy program to get the values and indices of the elements that are bigger than 10 in a given array. Go to the editor
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]))
Click me to see the sample solution
32. Write a NumPy program to save a NumPy array to a text file. Go to the editor
Click me to see the sample solution
33. Write a NumPy program to find the memory size of a NumPy array. Go to the editor
Expected Output:
128 bytes
Click me to see the sample solution
34. Write a NumPy program to create an array of ones and an array of zeros. Go to the editor
Expected Output:
Create an array of zeros
Default type is float
[[ 0. 0.]]
Type changes to int
[[0 0]]
Create an array of ones
Default type is float
[[ 1. 1.]]
Type changes to int
[[1 1]]
Click me to see the sample solution
35. Write a NumPy program to change the dimension of an array. Go to the editor
Expected Output:
6 rows and 0 columns
(6,)
(3, 3) -> 3 rows and 3 columns
[[1 2 3]
[4 5 6]
[7 8 9]]
Change array shape to (3, 3) -> 3 rows and 3 columns
[[1 2 3]
[4 5 6]
[7 8 9]]
Click me to see the sample solution
36. Write a NumPy program to create a contiguous flattened array. Go to the editor
Original array:
[[10 20 30]
[20 40 50]]
New flattened array:
[10 20 30 20 40 50]
Click me to see the sample solution
37. Write a NumPy program to create a 2-dimensional array of size 2 x 3 (composed of 4-byte integer elements), also print the shape, type and data type of the array. Go to the editor
Expected Output:
(2, 3)
int32
Click me to see the sample solution
38. Write a NumPy program to create a new shape to an array without changing its data. Go to the editor
Reshape 3x2:
[[1 2]
[3 4]
[5 6]]
Reshape 2x3:
[[1 2 3]
[4 5 6]]
Click me to see the sample solution
39. Write a NumPy program to change the data type of an array. Go to the editor
Expected Output:
[[ 2 4 6]
[ 6 8 10]]
Data type of the array x is: int32
New Type: float64
[[ 2. 4. 6.]
[ 6. 8. 10.]]
Click me to see the sample solution
40. Write a NumPy program to create a new array of 3*5, filled with 2. Go to the editor
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]]
Click me to see the sample solution
41. Write a NumPy program to create an array of 10's with the same shape and type of a given array. Go to the editor
Sample array: x = np.arange(4, dtype=np.int64)
Expected Output:
[10 10 10 10]
Click me to see the sample solution
42. Write a NumPy program to create a 3-D array with ones on a diagonal and zeros elsewhere. Go to the editor
Expected Output:
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
Click me to see the sample solution
43. Write a NumPy program to create a 2-D array whose diagonal equals [4, 5, 6, 8] and 0's elsewhere. Go to the editor
Expected Output:
[[4 0 0 0]
[0 5 0 0]
[0 0 6 0]
[0 0 0 8]]
Click me to see the sample solution
44. Write a NumPy program to create a 1-D array going from 0 to 50 and an array from 10 to 50. Go to the editor
Expected Output:
Array from 0 to 50:
[ 0 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]
Array from 10 to 50:
[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]
Click me to see the sample solution
45. Write a NumPy program to Create a 1-D array of 30 evenly spaced elements between 2.5. and 6.5, inclusive. Go to the editor
Expected Output:
[ 2.5 2.63793103 2.77586207 2.9137931 3.05172414 3.18965517
.................
5.81034483 5.94827586 6.0862069 6.22413793 6.36206897 6.5 ]
Click me to see the sample solution
46. Write a NumPy program to create a 1-D array of 20 element spaced evenly on a log scale between 2. and 5., exclusive. Go to the editor
Expected Output:
[ 100. 141.25375446 199.5262315 281.83829313
......................
25118.8643151 35481.33892336 50118.72336273 70794.57843841]
Click me to see the sample solution
47. Write a NumPy program to create an array which looks like below array. Go to the editor
Expected Output:
[[ 0. 0. 0.]
...........
[ 1. 1. 1.]]
Click me to see the sample solution
48. Write a NumPy program to create an array which looks like below array. Go to the editor
Expected Output:
[[ 2 3 4]
[ 5 6 7]
[ 0 9 10]
[ 0 0 13]]
Click me to see the sample solution
49. Write a NumPy program to collapse a 3-D array into one dimension array. Go to the editor
Expected Output:
3-D array:
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
One dimension array:
[ 1. 0. 0. 0. 1. 0. 0. 0. 1.]
Click me to see the sample solution
50. Write a NumPy program to find the 4th element of a specified array. Go to the editor
Expected Output:
[[ 2 4 6]
[ 6 8 10]]
Forth e1ement of the array:
6
Click me to see the sample solution
51. Write a NumPy program to interchange two axes of an array. Go to the editor
Sample array: [[1 2 3]]
Expected Output:
[[1]
[2]
[3]]
Click me to see the sample solution
52. Write a NumPy program to move axes of an array to new positions. Other axes remain in their original order. Go to the editor
Expected Output:
(3, 4, 2)
(4, 2, 3)
Click me to see the sample solution
53. Write a NumPy program to move the specified axis backwards, until it lies in a given position. Go to the editor
Move the following 3rd array axes to first position.
(2,3,4,5)
Sample Expected Output:
(2, 5, 3, 4)
Click me to see the sample solution
54. Write a NumPy program to convert specified inputs to arrays with at least one dimension. Go to the editor
Expected Output:
[ 12.]
[[ 0. 1. 2.]
[ 3. 4. 5.]]
[array([1]), array([3, 4])]
Click me to see the sample solution
55. Write a NumPy program to view inputs as arrays with at least two dimensions, three dimensions. Go to the editor
Expected Output:
View inputs as arrays with at least two dimensions:
[10]
[[ 0. 1.]
[ 2. 3.]]
View inputs as arrays with at least three dimensions:
[[[15]]]
[[[ 0.]
[ 1.]
[ 2.]]]
Click me to see the sample solution
56. Write a NumPy program to insert a new axis within a 2-D array. Go to the editor
2-D array of shape (3, 4).
Expected Output:
New shape will be will be (3, 1, 4).
Click me to see the sample solution
57. Write a NumPy program to remove single-dimensional entries from a specified shape. Go to the editor
Specified shape: (3, 1, 4)
Expected Output: (3, 4)
Click me to see the sample solution
58. Write a NumPy program to concatenate two 2-dimensional arrays. Go to the editor
Expected Output:
Sample arrays: ([[0, 1, 3], [5, 7, 9]], [[0, 2, 4], [6, 8, 10]]
Expected Output:
[[ 0 1 3 0 2 4]
[ 5 7 9 6 8 10]]
Click me to see the sample solution
59. Write a NumPy program to convert 1-D arrays as columns into a 2-D array. Go to the editor
Sample array: (10,20,30), (40,50,60)
Expected Output:
[[10 40]
[20 50]
[30 60]]
Click me to see the sample solution
60. Write a NumPy program to convert (in sequence depth wise (along third axis)) two 1-D arrays into a 2-D array. Go to the editor
Sample array: (10,20,30), (40,50,60)
Expected Output:
[[[10 40]]
[[20 50]]
[[30 60]]]
Click me to see the sample solution
61. Write a NumPy program to split an array of 14 elements into 3 arrays, each of which has 2, 4, and 8 elements in the original order. Go to the editor
Expected Output:
Original array: [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
After splitting:
[array([1, 2]), array([3, 4, 5, 6]), array([ 7, 8, 9, 10, 11, 12, 13, 14])]
Click me to see the sample solution
62. Write a NumPy program to split of an array of shape 4x4 it into two arrays along the second axis. Go to the editor
Sample array :
[[ 0 1 2 3]
........
[12 13 14 15]]
Expected Output:
[array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]), array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]]), array([], shape=(4, 0), dtype=int64)]
Click me to see the sample solution
63. Write a NumPy program to get the number of nonzero elements in an array. Go to the editor
Expected Output:
Original array:
[[ 0 10 20]
[20 30 40]]
Number of non zero elements in the above array:
5
Click me to see the sample solution
64. Write a NumPy program to create a 5x5 matrix with row values ranging from 0 to 4. Go to the editor
Original array:
[[ 0. 0. 0. 0. 0.]
.........
[ 0. 0. 0. 0. 0.]]
Row values ranging from 0 to 4.
[[ 0. 1. 2. 3. 4.]
..........
[ 0. 1. 2. 3. 4.]]
Click me to see the sample solution
65. Write a NumPy program to test whether specified values are present in an array. Go to the editor
Expected Output:
Original array:
[[ 1.12 2. 3.45]
[ 2.33 5.12 6. ]]
True
False
True
False
True
Click me to see the sample solution
66. Write a NumPy program to create a vector of size 10 with values ranging from 0 to 1, both excluded. Go to the editor
Expected Output:
[ 0.09090909 0.18181818 0.27272727 0.36363636 0.45454545 0.54545455
0.63636364 0.72727273 0.81818182 0.90909091]
Click me to see the sample solution
67. Write a NumPy program to make an array immutable (read-only). Go to the editor
Expected Output:
Test the array is read-only or not:
Try to change the value of the first element:
Traceback (most recent call last):
File "19236bd0-0bd9-11e7-a232-c706d0968eb6.py", line 6, in
x[0] = 1
ValueError: assignment destination is read-only
Click me to see the sample solution
68. Write a NumPy program (using NumPy) to sum of all the multiples of 3 or 5 below 100. Go to the editor
Expected Output:
[ 3 5 6 9 10 12 15 18 20 21 24 25 27 30 33 35 36 39 40 42 45 48 50 51 54
55 57 60 63 65 66 69 70 72 75 78 80 81 84 85 87 90 93 95 96 99]
2318
Click me to see the sample solution
69. Write a NumPy program to create an array with 10^3 elements. Go to the editor
Expected Output:
[ 0. 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.
- - - - - - - - - - - - - - - - -
972. 973. 974. 975. 976. 977. 978. 979. 980. 981. 982. 983.
984. 985. 986. 987. 988. 989. 990. 991. 992. 993. 994. 995.
996. 997. 998. 999.]
Click me to see the sample solution
70. Write a NumPy program to create display every element of a NumPy array. Go to the editor
Expected Output:
0 1 2 3 4 5 6 7 8 9 10 11
Click me to see the sample solution
71. Write a NumPy program to create and display every element of a NumPy array in Fortran order. Go to the editor
Expected Output:
Elements of the array in Fortan array:
0 4 8 1 5 9 2 6 10 3 7 11
Click me to see the sample solution
72. Write a NumPy program to create a 5x5x5 cube of 1's. Go to the editor
Expected Output:
[[[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 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]]]
Click me to see the sample solution
73. Write a NumPy program to create an array of (3, 4) shape, multiply every element value by 3 and display the new array. Go to the editor
Expected Output:
Original array elements:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
New array elements:
[[ 0 3 6 9]
[12 15 18 21]
[24 27 30 33]]
Click me to see the sample solution
74. Write a NumPy program to combine a one and a two dimensional array together and display their elements. Go to the editor
Expected Output:
One dimensional array:
[0 1 2 3]
Two dimensional array:
[[0 1 2 3]
[4 5 6 7]]
0:0
1:1
2:2
3:3
0:4
1:5
2:6
3:7
Click me to see the sample solution
75. Write a NumPy program to create an array of zeros and three column types (integer, float, character). Go to the editor
Expected Output:
[(1, 2., b'Albert Einstein') (2, 2., b'Edmond Halley')
(3, 3., b'Gertrude B. Elion')]
Click me to see the sample solution
76. Write a NumPy program to create a function cube which cubes all the elements of an array. Go to the editor
Expected Output:
[ 1 8 27]
Click me to see the sample solution
77. Write a NumPy program to create an array of (3, 4) shape and convert the array elements in smaller chunks. Go to the editor
Expected Output:
Original array elements:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[0 4 8]
[1 5 9]
[ 2 6 10]
[ 3 7 11]
Click me to see the sample solution
78. Write a NumPy program to create a record array from a (flat) list of arrays. Go to the editor
Sample arrays: [1,2,3,4], ['Red', 'Green', 'White', 'Orange'], [12.20,15,20,40]
Expected Output:
(1, 'Red', 12.2)
(2, 'Green', 15.0)
(3, 'White', 20.0)
Click me to see the sample solution
79. Write a NumPy program to generate a generic 2D Gaussian-like array. Go to the editor
Expected Output:
2D Gaussian-like array:
[[ 0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.60279818
0.57375342 0.51979489 0.44822088 0.36787944]
..........
[ 0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.60279818
0.57375342 0.51979489 0.44822088 0.36787944]]
Click me to see the sample solution
80. Write a NumPy program to convert a NumPy array into Python list structure. Go to the editor
Expected Output:
Original array elements:
[[0 1]
[2 3]
[4 5]]
Array to list:
[[0, 1], [2, 3], [4, 5]]
Click me to see the sample solution
81. Write a NumPy program to access an array by column. Go to the editor
Expected Output:
Original array elements:
[[0 1]
[2 3]
[4 5]]
Access an array by column:
First column:
[0 1]
Second column:
[2 3]
Third column:
[4 5]
Click me to see the sample solution
82. Write a NumPy program to convert a NumPy array of float values to a NumPy array of integer values. Go to the editor
Expected Output:
Original array elements:
[[ 12. 12.51]
[ 2.34 7.98]
[ 25.23 36.5 ]]
Convert float values to intger values:
[[12 12]
[ 2 7]
[25 36]]
Click me to see the sample solution
83. Write a NumPy program to display NumPy array elements of floating values with given precision. Go to the editor
Expected Output:
Original array elements:
[ 0.26153123 0.52760141 0.5718299 0.5927067 0.7831874 0.69746349
0.35399976 0.99469633 0.0694458 0.54711478]
Print array values with precision 3:
[ 0.262 0.528 0.572 0.593 0.783 0.697 0.354 0.995 0.069 0.547]
Click me to see the sample solution
84. Write a NumPy program to suppresses the use of scientific notation for small numbers in NumPy array. Go to the editor
Expected Output:
Original array elements:
[ 1.60000000e-10 1.60000000e+00 1.20000000e+03 2.35000000e-01]
Print array values with precision 3:
[ 0. 1.6 1200. 0.235]
Click me to see the sample solution
85. Write a NumPy program to create a NumPy array of 10 integers from a generator. Go to the editor
Expected Output:
[0 1 2 3 4 5 6 7 8 9]
Click me to see the sample solution
86. Write a NumPy program to add an extra column to a NumPy array. Go to the editor
Expected Output:
[[ 10 20 30 100]
[ 40 50 60 200]]
Click me to see the sample solution
87. Write a NumPy program to find unique rows in a NumPy array. Go to the editor
Expected Output:
Original array:
[[20 20 20 0]
.......
[10 20 20 20]]
Unique rows of the above array:
[[ 0 20 20 20]
[10 20 20 20]
[20 20 20 0]]
Click me to see the sample solution
88. Write a NumPy program to replace all elements of NumPy array that are greater than specified array. Go to the editor
Expected Output:
Original array:
[[ 0.42436315 0.48558583 0.32924763]
[ 0.7439979 0.58220701 0.38213418]
[ 0.5097581 0.34528799 0.1563123 ]]
Replace all elements of the said array with .5 which are greater than. 5
[[ 0.42436315 0.48558583 0.32924763]
[ 0.5 0.5 0.38213418]
[ 0.5 0.34528799 0.1563123 ]]
Click me to see the sample solution
89. Write a NumPy program to remove specific elements in a NumPy array. Go to the editor
Expected Output:
Original array:
[ 10 20 30 40 50 60 70 80 90 100]
Delete first, fourth and fifth elements:
[ 20 30 60 70 80 90 100]
Click me to see the sample solution
90. Write a NumPy program to replace the negative values in a NumPy array with 0. Go to the editor
Expected Output:
Original array:
[-1 -4 0 2 3 4 5 -6]
Replace the negative values of the said array with 0:
[0 0 0 2 3 4 5 0]
Click me to see the sample solution
91. Write a NumPy program to remove all rows in a NumPy array that contain non-numeric values. Go to the editor
Expected Output:
Original array:
[[ 1. 2. 3.]
[ 4. 5. nan]
[ 7. 8. 9.]
[ 1. 0. 1.]]
Remove all non-numeric elements of the said array
[[ 1. 2. 3.]
[ 7. 8. 9.]
[ 1. 0. 1.]]
Click me to see the sample solution
92. Write a NumPy program to select indices satisfying multiple conditions in a NumPy array. Go to the editor
Sample array :
a = np.array([97, 101, 105, 111, 117])
b = np.array(['a','e','i','o','u'])
Note: Select the elements from the second array corresponding to elements in the first array that are greater than 100 and less than 110
Expected Output:
Original arrays
[ 97 101 105 111 117]
['a' 'e' 'i' 'o' 'u']
Elements from the second array corresponding to elements in the first
array that are greater than 100 and less than 110:
['e' 'i']
Click me to see the sample solution
93. Write a NumPy program to get the magnitude of a vector in NumPy. Go to the editor
Expected Output:
Original array:
[1 2 3 4 5]
Magnitude of the vector:
7.4161984871
Click me to see the sample solution
94. Write a NumPy program to count the frequency of unique values in NumPy array. Go to the editor
Expected Output:
Original array:
[10 10 20 10 20 20 20 30 30 50 40 40]
Frequency of unique values of the said array:
[[10 20 30 40 50]
[ 3 4 2 2 1]]
Click me to see the sample solution
95. Write a NumPy program to check whether the NumPy array is empty or not. Go to the editor
Expected Output:
2
0
Click me to see the sample solution
96. Write a NumPy program to divide each row by a vector element. Go to the editor
Expected Output:
Original array:
[[20 20 20]
[30 30 30]
[40 40 40]]
Vector:
[20 30 40]
[[ 1. 1. 1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]
Click me to see the sample solution
97. Write a NumPy program to print all the values of an array. Go to the editor
Expected Output:
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
Click me to see the sample solution
98. Write a NumPy program to convert the raw data in an array to a binary string and then create an array. Go to the editor
Expected Output:
Original array:
[ 10. 20. 30.]
Binary string array:
b'\x00\x00\x00\x00\x00\[email protected]\x00\x00\x00\x00\x00\[email protected]\x00\x00\x00\x00\x00\x00>@'
Array using fromstring():
[ 10. 20. 30.]
Click me to see the sample solution
99. Write a NumPy program to sum and compute the product of a NumPy array elements. Go to the editor
Expected Output:
Original array:
[ 10. 20. 30.]
Sum of the array elements:
60.0
Product of the array elements:
6000.0
Click me to see the sample solution
100. Write a NumPy program to take values from a source array and put them at specified indices of another array. Go to the editor
Expected Output:
[ 10. 10. 20. 30. 30.]
Put 0 and 40 in first and fifth position of the above array
Array x after put two values:
[ 0. 10. 20. 30. 40.]
Click me to see the sample solution
101. Write a NumPy program to print the full NumPy array, without truncation. Go to the editor
Truncated output:
[ 0 1 2 ... 1997 1998 1999]
Click me to see the sample solution
102. Write a NumPy program to convert a NumPy array into a csv file. Go to the editor
Click me to see the sample solution
103. Write a NumPy program to calculate the Euclidean distance. Go to the editor
From Wikipedia:
In mathematics, the Euclidean distance or Euclidean metric is the "ordinary" straight-line distance between two points in Euclidean space. With this distance, Euclidean space becomes a metric space. The associated norm is called the Euclidean norm. Older literature refers to the metric as the Pythagorean metric.
Sample Output:
Euclidean distance: 5.196152422706632
Click me to see the sample solution
104. Write a NumPy program to access last two columns of a multidimensional columns. Go to the editor
Sample Output:
[[1 2 3]
[4 5 6]
[7 8 9]]
[[2 3]
[5 6]
[8 9]]
Click me to see the sample solution
105. Write a NumPy program to read a CSV data file and store records in an array. Go to the editor
Sample CSV file: fdata.csv
Date,Open,High,Low,Close
03-10-16,774.25,776.065002,769.5,772.559998
.................
07-10-16,779.659973,779.659973,770.75,775.080017
Sample Output:
[(b'Date', nan, nan, nan, nan)
(b'03-10-16', 774.25, 776.065, 769.5 , 772.56)
......................
(b'07-10-16', 779.66, 779.66 , 770.75, 775.08)]
Click me to see the sample solution
106. Write a NumPy program to count the occurrence of a specified item in a given NumPy array. Go to the editor
Sample Output:
Original array:
[10 20 20 20 20 0 20 30 30 30 0 0 20 20 0]
1
7
3
4
Click me to see the sample solution
107. Write a NumPy program to calculate percentiles for a sequence or single-dimensional NumPy array. Go to the editor
Sample Output:
50th percentile (median):
3.0
40th percentile:
2.6
90th percentile:
4.6
Click me to see the sample solution
108. Write a NumPy program to convert a PIL Image into a NumPy array. Go to the editor
Sample Output:
[[[255 255 255 0]
.......
[255 255 255 0]]]
Click me to see the sample solution
109. Write a NumPy program to convert a NumPy array to an image. Display the image. Go to the editor
Sample Output:
Click me to see the sample solution
110. Write a NumPy program to remove nan values from a given array. Go to the editor
Sample Output:
Original array:
[200. 300. nan nan nan 700.]
After removing nan values:
[200. 300. 700.]
Original array:
[[ 1. 2. 3.]
[nan 0. nan]
[ 6. 7. nan]]
After removing nan values:
[1. 2. 3. 0. 6. 7.]
Click me to see the sample solution
111. Write a NumPy program to create a Cartesian product of two arrays into single array of 2D points. Go to the editor
Sample Output:
[[1 4]
......
[3 5]]
Click me to see the sample solution
112. Write a NumPy program to get the memory usage by NumPy arrays. Go to the editor
Sample Output:
8256
Click me to see the sample solution
113. Write a NumPy program to build an array of all combinations of three NumPy arrays. Go to the editor
Sample Output:
Original arrays:
Array-1
[1, 2, 3]
Array-2
[4, 5]
Array-3
[6, 7]
Combine array:
[[1 4 6]
........
[3 4 7]
[3 5 7]]
Click me to see the sample solution
114. Write a NumPy program to create random set of rows from 2D array. Go to the editor
Sample Output:
Random set of rows from 2D array array:
[[4 0 2]
.......
[3 4 3]]
Click me to see the sample solution
115. Write a NumPy program to find indices of elements equal to zero in a NumPy array. Go to the editor
Sample Output:
Original array:
[1 0 2 0 3 0 4 5 6 7 8]
Indices of elements equal to zero of the said array:
[1 3 5]
Click me to see the sample solution
116. Write a NumPy program to compute the histogram of a set of data. Go to the editor
Sample Output:
Click me to see the sample solution
117. Write a NumPy program to compute the line graph of a set of data. Go to the editor
Sample Output:
Click me to see the sample solution
118. Write a NumPy program to find the position of the index of a specified value greater than existing value in NumPy array. Go to the editor
Sample Output:
Original array:
[-6 -5 -4 -3 -2 -1 0 1 2 3 4 5]
Position of the index:
9
Click me to see the sample solution
119. Write a NumPy program to add a new row to an empty NumPy array. Go to the editor
Sample Output:
Empty array:
[]
After adding two new arrays:
[[10 20 30]
[40 50 60]]
Click me to see the sample solution
120. Write a NumPy program to get the index of a maximum element in a NumPy array along one axis. Go to the editor
Sample Output:
Original array:
[[1 2 3]
[4 3 1]]
Index of a maximum element in a NumPy array along one axis:
4
Click me to see the sample solution
121. Write a NumPy program to join a sequence of arrays along a new axis. Go to the editor
Sample Output:
Original arrays:
[1 2 3]
[2 3 4]
Sequence of arrays along a new axis:
[[1 2 3]
[2 3 4]]
Original arrays:
[[1]
[2]
[3]]
[[2]
[3]
[4]]
Sequence of arrays along a new axis:
[[1]
[2]
[3]
[2]
[3]
[4]]
Click me to see the sample solution
122. Write a NumPy program to find the index of the sliced elements as follows from a given 4x4 array. Go to the editor
Sample Output:
Original arrays:
[[ 0 1 2 3]
........
[12 13 14 15]]
Sliced elements:
[ 0 5 11]
Click me to see the sample solution
123. Write a NumPy program to create two arrays of size bigger and smaller than a given array. Go to the editor
Sample Output:
Original arrays:
[[ 0 1 2 3]
........
[12 13 14 15]]
Array with size 2x2 from the said array:
[[0 1]
[2 3]]
Array with size 6x6 from the said array:
[[ 0 1 2 3 4 5]
.........
[14 15 0 1 2 3]]
Click me to see the sample solution
124. Write a NumPy program to broadcast on different shapes of arrays where p(3,3) + q(3). Go to the editor
Sample Output:
Original arrays:
Array-1
[[0 0 0]
[1 2 3]
[4 5 6]]
Array-2
[10 11 12]
New Array:
[[10 11 12]
[11 13 15]
[14 16 18]]
Click me to see the sample solution
125. Write a NumPy program to broadcast on different shapes of arrays where a(,3) + b(3). Go to the editor
Sample Output:
Original arrays:
Array-1
[[ 0]
[10]
[20]]
Array-2
[10 11 12]
New Array:
[[10 11 12]
[20 21 22]
[30 31 32]]
Click me to see the sample solution
126. Write a NumPy program to rearrange the dimensions of a given array. Go to the editor
Sample Output:
Original arrays:
[[ 0 1 2 3]
...........
[20 21 22 23]]
After reverse the dimensions:
[[ 0 4 8 12 16 20]
............
[ 3 7 11 15 19 23]]
Click me to see the sample solution
127. Write a NumPy program to stack arrays in sequence horizontally (column wise). Go to the editor
Sample Output:
Original arrays:
Array-1
[[0 1 2]
[3 4 5]
[6 7 8]]
Array-2
[[ 0 3 6]
[ 9 12 15]
[18 21 24]]
Stack arrays in sequence horizontally:
[[ 0 1 2 0 3 6]
[ 3 4 5 9 12 15]
[ 6 7 8 18 21 24]]
Click me to see the sample solution
128. Write a NumPy program to stack arrays in sequence vertically. Go to the editor
Sample Output:
Original arrays:
Array-1
[[0 1 2]
[3 4 5]
[6 7 8]]
Array-2
[[ 0 3 6]
[ 9 12 15]
[18 21 24]]
Stack arrays in sequence vertically:
[[ 0 1 2]
...........
[18 21 24]]
Click me to see the sample solution
129. Write a NumPy program to stack 1-D arrays as columns wise. Go to the editor
Sample Output:
Original arrays:
Array-1
[1 2 3]
Array-2
[2 3 4]
Stack 1-D arrays as columns wise:
[[1 2]
[2 3]
[3 4]]
Click me to see the sample solution
130. Write a NumPy program to stack 1-D arrays as row wise. Go to the editor
Sample Output:
Original arrays:
Array-1
[1 2 3]
Array-2
[2 3 4]
Stack 1-D arrays as rows wise:
[[1 2 3]
[2 3 4]]
Click me to see the sample solution
131. Write a NumPy program to split a given array into multiple sub-arrays vertically (row-wise). Go to the editor
Sample Output:
Original arrays:
[[ 0. 1. 2. 3.]
.............
[12. 13. 14. 15.]]
Split an array into multiple sub-arrays vertically:
[array([[0., 1., 2., 3.],
[4., 5., 6., 7.]]), array([[ 8., 9., 10., 11.],
[12., 13., 14., 15.]])]
Click me to see the sample solution
132. Write a NumPy program to split array into multiple sub-arrays along the 3rd axis. Go to the editor
Sample Output:
Original arrays:
[[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]]
[[ 8. 9. 10. 11.]
[12. 13. 14. 15.]]]
split array into multiple sub-arrays along the 3rd axis:
[array([[[ 0., 1.],
[ 4., 5.]],
[[ 8., 9.],
[12., 13.]]]), array([[[ 2., 3.],
[ 6., 7.]],
[[10., 11.],
[14., 15.]]])]
Click me to see the sample solution
133. Write a NumPy program to count the number of dimensions, number of elements and number of bytes for each element in a given array. Go to the editor
Sample Output:
Original arrays:
[[ 0 1 2 3 4 5 6 7 8 9 10 11]
[12 13 14 15 16 17 18 19 20 21 22 23]]
Number of dimensions:
2
Number of elements:
24
Number of bytes for each element in the said array:
8
Click me to see the sample solution
134. Write a NumPy program to extract all the elements of the first row from a given (4x4) array. Go to the editor
Sample Output:
Original array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Extracted data: First row
[0 1 2 3]
Click me to see the sample solution
135. Write a NumPy program to extract all the elements of the second row from a given (4x4) array. Go to the editor
Sample Output:
Original array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Extracted data: Second row
[4 5 6 7]
Click me to see the sample solution
136. Write a NumPy program to extract all the elements of the third column from a given (4x4) array. Go to the editor
Sample Output:
Original array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Extracted data: Third column
[ 2 6 10 14]
Click me to see the sample solution
137. Write a NumPy program to extract first and second elements of the first and second rows from a given (4x4) array. Go to the editor
Sample Output:
Original array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Extracted data: First and second elements of the first and second rows
[[0 1]
[4 5]]
Click me to see the sample solution
138. Write a NumPy program to extract third and fourth elements of the first and second rows from a given (4x4) array. Go to the editor
Sample Output:
Original array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Extracted data: Third and fourth elements of the first and second rows
[[2 3]
[6 7]]
Click me to see the sample solution
139. Write a NumPy program to extract first and third elements of the first and third rows from a given (4x4) array. Go to the editor
Sample Output:
Original array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Extracted data: First and third elements of the first and third rows
[[ 0 2]
[ 8 10]]
Click me to see the sample solution
140. Write a NumPy program to extract second and fourth elements of the second and fourth rows from a given (4x4) array. Go to the editor
Sample Output:
Original array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Extracted data: Second and fourth elements of the second and fourth rows
[[ 5 7]
[13 15]]
Click me to see the sample solution
141. Write a NumPy program to extract all the elements of the second and third columns from a given (4x4) array. Go to the editor
Sample Output:
Original array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Extracted data: All the elements of the second and third columns
[[ 1 2]
[ 5 6]
[ 9 10]
[13 14]]
Click me to see the sample solution
142. Write a NumPy program to extract all the elements of the first and fourth columns from a given (4x4) array. Go to the editor
Sample Output:
Original array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Extracted data: All the elements of the first and fourth columns
[[ 0 3]
[ 4 7]
[ 8 11]
[12 15]]
Click me to see the sample solution
143. Write a NumPy program to extract first element of the second row and fourth element of fourth row from a given (4x4) array. Go to the editor
Sample Output:
Original array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Extracted data: First element of the second row and fourth element of fourth row
[ 4 15]
Click me to see the sample solution
144. Write a NumPy program to extract second and third elements of the second and third rows from a given (4x4) array. Go to the editor
Sample Output:
Original array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Extracted data: Second and third elements of the second and third rows
[[ 5 6]
[ 9 10]]
Click me to see the sample solution
145. Write a NumPy program to extract first, third and fifth elements of the third and fifth rows from a given (6x6) array. Go to the editor
Sample Output:
Original array:
[[ 0 1 2 3 4 5]
................
[30 31 32 33 34 35]]
Extracted data: First, third and fifth elements of the third and fifth rows
[[12 14 16]
[24 26 28]]
Click me to see the sample solution
146. Write a NumPy program to add two arrays A and B of sizes (3,3) and (,3). Go to the editor
Sample Output:
Original array:
Array-1
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
Array-2
[0 1 2]
A + B:
[[1. 2. 3.]
[1. 2. 3.]
[1. 2. 3.]]
Click me to see the sample solution
147. Write a NumPy program to create an array that represents the rank of each item of a given array. Go to the editor
Sample Output:
Original array:
[24 27 30 29 18 14]
Rank of each item of the said array:
[2 3 5 4 1 0]
Click me to see the sample solution
148. Write a NumPy program to copy data from a given array to another array. Go to the editor
Sample Output:
Original array:
[24 27 30 29 18 14]
Copy of the said array:
[24 27 30 29 18 14]
Click me to see the sample solution
149. Write a NumPy program to find elements within range from a given array of numbers. Go to the editor
Sample Output:
Original array:
[ 1 3 7 9 10 13 14 17 29]
Elements within range: index position
(array([2, 3, 4, 5, 6, 7]),)
Click me to see the sample solution
150. Write a NumPy program to swap columns in a given array. Go to the editor
Sample Output:
Original array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
After swapping arrays:
[[ 1 0 2 3]
[ 5 4 6 7]
[ 9 8 10 11]]
Click me to see the sample solution
151. Write a NumPy program to get the row numbers in given array where at least one item is larger than a specified value. Go to the editor
Sample Output:
Original array:
[[ 0 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]]
Row numbers where at least one item is larger than 10:
(array([1, 2, 3]),)
Click me to see the sample solution
152. Write a NumPy program to calculate the sum of all columns of a 2D NumPy array. Go to the editor
Sample Output:
Original array:
[[ 0 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]]
Sum of all columns:
[54 58 62 66 70 74 78 82 86]
Click me to see the sample solution
153. Write a NumPy program to extract upper triangular part of a NumPy matrix. Go to the editor
Sample Output:
Original array:
[[ 0 1 2]
...........
[15 16 17]]
Extract upper triangular part of the said array:
[0 1 2 4 5 8]
Extract upper triangular part of the said array:
[0 1 4]
Click me to see the sample solution
154. Write a NumPy program to get a copy of a matrix with the elements below the k-th diagonal zeroed. Go to the editor
Sample Output:
Original array:
[[1 2 3]
[0 5 6]
[0 0 9]
[0 0 0]]
Copy of a matrix with the elements below the k-th diagonal zeroed:
[[1 2 3]
[0 5 6]
[0 0 9]
[0 0 0]]
Click me to see the sample solution
155. Write a NumPy program to check whether a Numpy array contains a specified row. Go to the editor
Sample Output:
Original array:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
True
False
True
Click me to see the sample solution
156. Write a NumPy program to calculate averages without NaNs along a given array. Go to the editor
Sample Output:
Original array:
[[10. 20. 30.]
[40. 50. nan]
[nan 6. nan]
[nan nan nan]]
Averages without NaNs along the said array:
[20. 45. 6. nan]
Click me to see the sample solution
157. Write a NumPy program to create a new array which is the average of every consecutive triplet of elements of a given array. Go to the editor
Sample Output:
Original array:
[ 1 2 3 2 4 6 1 2 12 0 -12 6]
Average of every consecutive triplet of elements of the said array:
[ 2. 4. 5. -2.]
Click me to see the sample solution
158. Write a NumPy program to calculate average values of two given NumPy arrays. Go to the editor
Sample Output:
Original arrays:
[[0, 1], [2, 3]]
[[4, 5], [0, 3]]
Average values of two said NumPy arrays:
[[2. 3.]
[1. 3.]]
Click me to see the sample solution
159. Write a NumPy program to rearrange columns of a given NumPy 2D array using given index positions. Go to the editor
Sample Output:
Original arrays:
[[ 11 22 33 44 55]
[ 66 77 88 99 100]]
New array:
[[ 22 44 11 55 33]
[ 77 99 66 100 88]]
Click me to see the sample solution
160. Write a NumPy program to find the k smallest values of a given NumPy array. Go to the editor
Sample Output:
Original arrays:
[ 1. 7. 8. 2. 0.1 3. 15. 2.5]
k smallest values:
[0.1 1. 2. 2.5]
Click me to see the sample solution
161. Write a NumPy program to create a white image of size 512x256. Go to the editor
Sample Output:
Click me to see the sample solution
162. Create an array (a) of shape 3, 4, 8 (K=3, J=4, I=8). tidx is an array of the same length as a.shape[1], i.e. contains J = 4 elements where each index denotes which element of K should be chosen.
Write a NumPy program to select from the first axis (K) by the indices tidx to get an array of shape (J=4, I=8) back. Go to the editor
Sample Output:
Original array and shape:
[[[3 2 2 7 7 7 0 3]
[5 8 4 2 9 9 3 9]
[6 8 2 8 5 7 8 7]
[5 2 4 0 4 9 2 5]]
------------------
tidex: [0 2 2 2]
Result:
[[3 2 2 7 7 7 0 3]
[3 9 2 6 3 3 1 0]
[5 4 0 6 0 2 7 8]
[6 3 1 8 8 1 5 7]]
Click me to see the sample solution
163. Create two arrays of six elements. Write a NumPy program to count the number of instances of a value occurring in one array on the condition of another array. Go to the editor
Sample Output:
Original arrays:
[ 10 -10 10 -10 -10 10]
[0.85 0.45 0.9 0.8 0.12 0.6 ]
Number of instances of a value occurring in one array on the condition of another array:
3
Click me to see the sample solution
164. Write a NumPy program to save as text a matrix which has in each row 2 float and 1 string at the end. Go to the editor
Sample Output:
string
1 0 aaa
0 1 bbb
0 1 ccc
Click me to see the sample solution
165. Write a NumPy program to merge three given NumPy arrays of same shape. Go to the editor
Click me to see the sample solution
166. Write a NumPy program to combine last element with first element of two given ndarray with different shapes. Go to the editor
Sample Output:
Original arrays:
['PHP', 'JS', 'C++']
['Python', 'C#', 'NumPy']
After Combining:
['PHP' 'JS' 'C++Python' 'C#' 'NumPy']
Click me to see the sample solution
167. Write a NumPy program to convert a Python dictionary to a NumPy ndarray. Go to the editor
Sample Output:
Original dictionary:
{'column0': {'a': 1, 'b': 0.0, 'c': 0.0, 'd': 2.0},
'column1': {'a': 3.0, 'b': 1, 'c': 0.0, 'd': -1.0},
'column2': {'a': 4, 'b': 1, 'c': 5.0, 'd': -1.0},
'column3': {'a': 3.0, 'b': -1.0, 'c': -1.0, 'd': -1.0}}
Type: <class 'dict'>
ndarray:
[[ 1. 0. 0. 2.]
[ 3. 1. 0. -1.]
[ 4. 1. 5. -1.]
[ 3. -1. -1. -1.]]
Type: <class 'numpy.ndarray'>
Click me to see the sample solution
168. Write a NumPy program to convert Pandas dataframe to NumPy array with headers. Go to the editor
Sample Output:
Original NumPy array:
[[0.18308157 0.32258608 0.39644848]
[0.31018507 0.08220454 0.40018982]
[0.63639779 0.34908174 0.39878868]
............
[0.02482073 0.02318678 0.36032194]
[0.69001834 0.04285817 0.31768865]]
Type: <class 'numpy.ndarray'>
Panda's DataFrame:
A B C
0 0.015583 0.968431 0.943192
1 0.221607 0.704098 0.241200
2 0.466129 0.269226 0.450781
3 0.747222 0.674508 0.686070
............
10 0.272827 0.817238 0.093650
11 0.196416 0.643602 0.262683
Type: <class 'pandas.core.frame.DataFrame'>
Click me to see the sample solution
169. Write a NumPy program to get all 2D diagonals of a 3D NumPy array. Go to the editor
Sample Output:
Original NumPy array:
[[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
............
[[40 41 42 43 44]
[45 46 47 48 49]
[50 51 52 53 54]
[55 56 57 58 59]]]
Type: <class 'numpy.ndarray'>
2D diagonals:
[[ 0 6 12 18]
[20 26 32 38]
[40 46 52 58]]
Type: <class 'numpy.ndarray'>
Click me to see the sample solution
170. Create a 2-dimensional array of size 2 x 3, composed of 4-byte integer elements. Write a NumPy program to find the number of occurrences of a sequence in the said array. Go to the editor
Sample Output:
Original NumPy array:
[[1 2 3]
[2 1 2]]
Type: <class 'numpy.ndarray'>
Sequence: 2,3
Number of occurrences of the said sequence: 2
Click me to see the sample solution
171. Write a NumPy program to search the index of a given array in another given array. Go to the editor
Sample Output:
Original NumPy array:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
Searched array:
[4 5 6]
Index of the searched array in the original array:
[1]
Click me to see the sample solution
172. Write a NumPy program to find and store non-zero unique rows in an array after comparing each row with other row in a given matrix. Go to the editor
Sample Output:
Original array:
[[ 1 1 0]
[ 0 0 0]
[ 0 2 3]
[ 0 0 0]
[ 0 -1 1]
[ 0 0 0]]
Non-zero unique rows:
[[ 1 1 0]
[ 0 2 3]
[ 0 -1 1]]
Click me to see the sample solution
173. Write a NumPy program to set zero to lower triangles along the last two axes of a three-dimensional of a given array. Go to the editor
Sample Output:
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.]]]
Result:
[[[0. 1. 1. 1. 1. 1. 1. 1.]
[0. 0. 1. 1. 1. 1. 1. 1.]
.................
[0. 0. 0. 0. 0. 0. 0. 0.]]]
Click me to see the sample solution
174. Write a NumPy program to get the number of items, array dimensions, number of array dimensions and the memory size of each element of a given array. Go to the editor
Sample Output:
Original array:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
Number of items of the said array:
12
Array dimensions:
(3, 4)
Number of array dimensions:
2
Memory size of each element of the said array
8
Click me to see the sample solution
175. Write a NumPy program to create an 1-D array of 20 elements. Now create a new array of shape (5, 4) from the said array, then restores the reshaped array into a 1-D array. Go to the editor
Sample Output:
Original array:
[ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38]
New array of shape(5, 3):
[[ 0 2 4 6]
[ 8 10 12 14]
[16 18 20 22]
[24 26 28 30]
[32 34 36 38]]
Restore the reshaped array into a 1-D array:
[ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38]
Click me to see the sample solution
176. Write a NumPy program to create an array of 4,5 shape and swap column1 with column4. Go to the editor
Sample Output:
Original array:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
After swapping column1 with column4:
[[ 3 1 2 0 4]
[ 8 6 7 5 9]
[13 11 12 10 14]
[18 16 17 15 19]]
Click me to see the sample solution
177. Write a NumPy program to create an array of 4,5 shape and to reverse the rows of the said array. After reversing 1st row will be 4th and 4th will be 1st, 2nd row will be 3rd row and 3rd row will be 2nd row. Go to the editor
Sample Output:
Original array:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
After reversing:
[[15 16 17 18 19]
[10 11 12 13 14]
[ 5 6 7 8 9]
[ 0 1 2 3 4]]
Click me to see the sample solution
178. Write a NumPy program to replace all the nan (missing values) of a given array with the mean of another array. Go to the editor
Sample Output:
Original arrays:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
[[ 1. 2. nan]
[ 4. 5. 6.]
[nan 7. nan]]
All the nan of array_nums2 replaced by the mean of array_nums1:
[[1. 2. 9.5]
[4. 5. 6. ]
[9.5 7. 9.5]]
Click me to see the sample solution
179. Write a NumPy program to fetch all items from a given array of 4,5 shape which are either greater than 6 and a multiple of 3. Go to the editor
Sample Output:
Original arrays:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
Items greater than 6 and a multiple of 3 of the said array:
[ 9 12 15 18]
Click me to see the sample solution
180. Write a NumPy program to check whether the dimensions of two given arrays are same or not. Go to the editor
Click me to see the sample solution
181. Write a NumPy program to place a specified element in specified time randomly in a specified 2D array. Go to the editor
Sample Output:
Original array:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
Place a specified element in specified time randomly:
[[10. 0. 0. 0.]
[10. 0. 0. 0.]
[ 0. 0. 0. 10.]
[ 0. 0. 0. 0.]]
Click me to see the sample solution
182. Write a NumPy program to subtract the mean of each row of a given matrix. Go to the editor
Sample Output:
Original matrix:
[[0.59243452 0.51883289 0.03732848 0.49544926 0.22637201 0.45750412
0.81614237 0.86681236 0.95482226 0.54789281]
..............
[0.14428353 0.20556412 0.97059136 0.53545871 0.93828877 0.81535277
0.60563373 0.47543413 0.0468766 0.97460889]]
Subtract the mean of each row of the said matrix:
[[ 0.04107541 -0.03252622 -0.51403063 -0.05590985 -0.3249871 -0.09385499
0.26478326 0.31545325 0.40346315 -0.0034663 ]
....................
[-0.42692573 -0.36564514 0.3993821 -0.03575056 0.36707951 0.24414351
0.03442447 -0.09577513 -0.52433266 0.40339963]]
Click me to see the sample solution
183. Write a NumPy program to test whether a given 2D array has null columns or not. Go to the editor
Sample Output:
Original array:
[[1 2 1 1 1 2 1 1 2 0]
[1 1 0 0 0 0 2 1 2 0]
[0 0 2 1 0 2 2 2 2 2]
[1 1 1 2 0 0 0 0 1 2]]
Test whether the said array has null columns or not:
False
Click me to see the sample solution
184. Write a NumPy program to create an array using generator function that generates 15 integers. Go to the editor
Sample Output:
New array:
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.]
Click me to see the sample solution
185. Write a NumPy program to create a new vector with 2 consecutive 0 between two values of a given vector. Go to the editor
Sample Output:
Original array:
[1 2 3 4 5 6 7 8]
New array:
[1. 0. 0. 2. 0. 0. 3. 0. 0. 4. 0. 0. 5. 0. 0. 6. 0. 0. 7. 0. 0. 8.]
Click me to see the sample solution
186. Write a NumPy program to multiply an array of dimension (2,2,3) by an array with dimensions (2,2). Go to the editor
Sample Output:
Original array:
[[[1. 1. 1.]
[1. 1. 1.]]
[[1. 1. 1.]
[1. 1. 1.]]]
New array:
[[[3. 3. 3.]
[3. 3. 3.]]
[[3. 3. 3.]
[3. 3. 3.]]]
Click me to see the sample solution
187. Write a NumPy program to convert a given vector of integers to a matrix of binary representation. Go to the editor
Sample Output:
Original vector:
[ 0 1 3 5 7 9 11 13 15]
Binary representation of the said vector:
[[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 1]
..............
[0 0 0 0 1 1 1 1]]
Click me to see the sample solution
188. Write a NumPy program to extract rows with unequal values (e.g. [1,1,2]) from 10x3 matrix. Go to the editor
Sample Output:
Original vector:
[[3 2 0]
........
[3 0 2]]
Rows with unequal values:
[[3 2 0]
[2 3 1]
.........
[3 0 2]]
Click me to see the sample solution
189. Write a NumPy program to find rows of a given array of shape (8,3) that contain elements of each row of another given array of shape (2,2). Go to the editor
Sample Output:
Original arrays:
[[5 2 5 1]
[5 4 1 3]
..........
[4 0 4 0]]
[[2 3 1]
[1 1 4]]
Rows of a given array that contain elements of each row of another given array:
[0 1 2 4]
Click me to see the sample solution
190. Write a NumPy program to create a record array from a given regular array. Go to the editor
Sample Output:
Original arrays:
[['Yasemin Rayner' '88.5' '90']
['Ayaana Mcnamara' '87' '99']
['Jody Preece' '85.5' '91']]
Record array;
[(b'Yasemin Rayner', 88.5, 90) (b'Ayaana Mcnamara', 87. , 99)
(b'Jody Preece', 85.5, 91)]
Click me to see the sample solution
191. Write a NumPy program to get the block-sum (block size is 5x5) from a given array of shape 25x25. Go to the editor
Sample Output:
Original arrays:
[[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. 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. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
Block-sum (5x5) of the said array:
[[25. 25. 25. 25. 25.]
[25. 25. 25. 25. 25.]
[25. 25. 25. 25. 25.]
[25. 25. 25. 25. 25.]
[25. 25. 25. 25. 25.]]
Click me to see the sample solution
192. Write a NumPy program to extract all the contiguous 4x4 blocks from a given random 12x12 matrix. Go to the editor
Sample Output:
Original arrays:
[[3 3 1 2 0 3 0 3 2 0 1 0]
[4 0 4 2 0 0 0 0 2 2 2 3]
..................
[1 2 3 4 1 2 3 4 3 2 3 4]
[4 0 4 2 2 4 1 4 2 0 0 0]]
Contiguous 4x4 blocks:
[[[[3 3 1 2]
[4 0 4 2]
[1 4 0 1]
[1 4 2 4]]
...
[[4 1 1 0]
[1 0 2 2]
[3 2 3 4]
[2 0 0 0]]]]
Click me to see the sample solution
193. Write a Numpy program to test whether numpy array is faster than Python list or not. Go to the editor
Time to aggregates elements from each of the iterables:
Sample Output:
List:
72.64399528503418
NumPy array:
19.61684226989746
Click me to see the sample solution
194. Write a NumPy program to create two arrays with shape (300,400, 5), fill values using unsigned integer (0 to 255). Insert a new axis that will appear at the beginning in the expanded array shape. Now combine the said two arrays into one. Go to the editor
Sample Output:
Array1:
[[[ 46 117 73]
[215 90 86]
[ 80 89 220]
...
[ 47 94 234]
[ 95 72 61]
[154 91 175]]
[[232 194 26]
[219 116 116]
[126 179 177]
...
Array2:
[[[ 37 10 228]
[241 52 82]
[196 47 189]
...
[127 70 246]
[158 46 12]
[ 56 129 162]]
[[224 215 47]
[139 72 13]
[218 64 78]
...
Combined array:
[[[[ 46 117 73]
[215 90 86]
[ 80 89 220]
...
[208 187 55]
[109 115 254]
[115 134 68]]
[[158 106 165]
[189 48 54]
[ 5 84 26]
...
[131 235 25]
[101 146 90]
[216 15 92]]]]
Click me to see the sample solution
195. Write a NumPy program to remove the first dimension from a given array of shape (1,3,4). Go to the editor
Sample Output:
Shape of the said array:
(1, 3, 4)
After removing the first dimension of the shape of the said array:
Click me to see the sample solution
196. Write a NumPy program to create a 12x12x4 array with random values and extract any array of shape(6,6,3) from the said array. Go to the editor
Sample Output:
Original array:
[[[0.67420382 0.5592584 0.27096382]
[0.2896283 0.68346522 0.13996167]
[0.74283318 0.06323309 0.0980022 ]
[0.43203954 0.38888969 0.44801756]
[0.04391897 0.4851516 0.34044817]
[0.2202623 0.81434798 0.51900666]
[0.12442371 0.91247823 0.01874549]
[0.2287782 0.88089638 0.40583551]]
........
[[0.28021231 0.47537742 0.72971633]
[0.87380873 0.83031311 0.56713737]
[0.23093306 0.22830678 0.54439754]
[0.88130002 0.37081258 0.78148687]
[0.00318428 0.62297164 0.58875116]
[0.68102061 0.31822913 0.04432477]
[0.70410386 0.56770957 0.42998752]
[0.5891714 0.25692428 0.19184309]]]
Extract array of shape (6,6,3) from the said array:
[[[0.67420382 0.5592584 0.27096382]
[0.2896283 0.68346522 0.13996167]
[0.74283318 0.06323309 0.0980022 ]
[0.43203954 0.38888969 0.44801756]
[0.04391897 0.4851516 0.34044817]
[0.2202623 0.81434798 0.51900666]]
.......
[[0.85965509 0.11357479 0.27325381]
[0.74156642 0.35108524 0.40305073]
[0.44791592 0.28270286 0.45377936]
[0.01543443 0.14978493 0.47738367]
[0.63671823 0.75239388 0.59118693]
[0.55932007 0.32759274 0.25519358]]]
Click me to see the sample solution
197. Write a NumPy program to create to concatenate two given arrays of shape (2, 2) and (2,1). Go to the editor
Sample Output:
Original arrays:
[[4.5 3.5]
[5.1 2.3]]
[[1]
[2]]
Concatenating the said two arrays:
[[4.5 3.5 1. ]
[5.1 2.3 2. ]]
Click me to see the sample solution
198. Write a NumPy program to create a 10x4 array filled with random floating point number values with and set the array values with specified precision. Go to the editor
Sample Output:
Original arrays:
[[-1.23335604 0.54368124 1.93500931 -0.45001639]
[ 1.20528013 2.04423279 -0.68858903 -0.46642789]
[ 1.29030601 0.60914425 0.03139104 -1.72528221]
[-0.32607231 -2.36951679 -0.80374203 -0.26212641]
[-0.16426628 -0.21524949 -0.35957311 -2.27709735]
[-0.28851276 -0.59101441 0.22293919 -0.50128832]
[ 1.3577567 0.05529019 0.81208832 0.70810424]
[ 0.46853801 -0.81857981 0.80443323 0.52391606]
[-0.29149088 -0.91153449 -1.00515549 0.31065165]
[ 2.68952752 1.86676839 1.49239198 -1.0409156 ]]
Set the array values with specified precision:
[[-1.2334 0.5437 1.935 -0.45 ]
[ 1.2053 2.0442 -0.6886 -0.4664]
[ 1.2903 0.6091 0.0314 -1.7253]
[-0.3261 -2.3695 -0.8037 -0.2621]
[-0.1643 -0.2152 -0.3596 -2.2771]
[-0.2885 -0.591 0.2229 -0.5013]
[ 1.3578 0.0553 0.8121 0.7081]
[ 0.4685 -0.8186 0.8044 0.5239]
[-0.2915 -0.9115 -1.0052 0.3107]
[ 2.6895 1.8668 1.4924 -1.0409]]
Click me to see the sample solution
199. Write a NumPy program to create an array using scientific notation numbers. Set the precision value to 6 and print the array. Go to the editor
Sample Output:
Original arrays:
[1.2e-07 1.5e-06 1.7e-05]
Set the precision value to 10:
[0.00000012 0.0000015 0.000017 ]
Click me to see the sample solution
200. Write a NumPy program to remove a specific column from a given array. Go to the editor
Sample Output:
Original array:
[[0.54420704 0.35710194 0.79167579 0.72249474 0.99968936]
[0.22306352 0.31085825 0.09849254 0.11708716 0.45757945]
[0.19381592 0.13587749 0.90455038 0.95146017 0.55716851]
[0.62031347 0.84275698 0.84665943 0.06562172 0.58415968]
[0.41903059 0.0660559 0.85270403 0.94184265 0.95371587]
[0.02577681 0.91577282 0.1969686 0.3472482 0.23337827]
[0.43563908 0.62308811 0.09606371 0.79053989 0.69382428]]
Delete the first column of the said array:
[[0.35710194 0.79167579 0.72249474 0.99968936]
[0.31085825 0.09849254 0.11708716 0.45757945]
[0.13587749 0.90455038 0.95146017 0.55716851]
[0.84275698 0.84665943 0.06562172 0.58415968]
[0.0660559 0.85270403 0.94184265 0.95371587]
[0.91577282 0.1969686 0.3472482 0.23337827]
[0.62308811 0.09606371 0.79053989 0.69382428]]
Delete the last column of the said array:
[[0.54420704 0.35710194 0.79167579 0.72249474]
[0.22306352 0.31085825 0.09849254 0.11708716]
[0.19381592 0.13587749 0.90455038 0.95146017]
[0.62031347 0.84275698 0.84665943 0.06562172]
[0.41903059 0.0660559 0.85270403 0.94184265]
[0.02577681 0.91577282 0.1969686 0.3472482 ]
[0.43563908 0.62308811 0.09606371 0.79053989]]
Click me to see the sample solution
201. Write a NumPy program to create a 90x30 array filled with random point numbers, increase the number of items (10 edge elements) shown by the print statement. Go to the editor
Sample Output:
Original array:
[[1 8 3 ... 9 5 8]
[1 9 6 ... 6 5 0]
[0 0 8 ... 6 9 8]
...
[2 3 9 ... 4 0 1]
[2 6 8 ... 8 3 4]
[1 2 9 ... 7 4 8]]
Increase the number of items (10 edge elements) shown by the print statement:
[[1 8 3 0 1 2 8 0 0 5 ... 1 3 8 1 1 7 5 9 5 8]
[1 9 6 1 0 7 7 3 8 6 ... 5 9 9 9 9 7 2 6 5 0]
[0 0 8 1 4 2 1 3 2 9 ... 5 6 4 1 4 9 0 6 9 8]
[7 2 7 1 7 2 4 7 2 0 ... 4 2 6 2 6 4 4 0 6 7]
[3 6 0 0 2 1 6 6 2 9 ... 3 4 1 9 5 7 4 4 6 7]
[9 5 2 6 9 2 7 7 9 6 ... 4 3 5 2 7 0 6 0 2 7]
[5 5 4 1 9 2 5 3 5 2 ... 7 4 7 5 5 7 3 0 1 8]
[5 9 9 9 6 3 5 0 9 4 ... 6 4 9 7 8 7 2 5 9 4]
[6 1 0 8 9 8 0 6 6 3 ... 5 3 1 6 1 5 2 9 8 3]
[3 0 3 1 4 1 4 1 9 6 ... 3 9 4 4 5 0 2 0 8 0]
...
[3 9 5 0 3 8 5 3 1 1 ... 1 2 4 6 2 4 5 0 0 5]
[7 9 7 0 1 3 0 1 8 4 ... 5 7 5 4 7 2 0 2 3 0]
[4 2 6 4 7 2 3 9 7 9 ... 2 7 1 2 5 7 5 4 0 2]
[7 8 9 7 3 3 1 2 8 5 ... 1 9 9 5 5 2 3 5 0 1]
[3 7 6 8 3 2 5 0 5 4 ... 0 4 9 8 5 3 2 1 3 3]
[3 3 8 4 0 4 8 4 7 0 ... 7 5 0 9 7 8 2 0 7 9]
[8 4 4 7 3 8 2 0 8 9 ... 5 5 8 1 5 6 0 6 5 0]
[2 3 9 9 9 8 6 5 4 4 ... 4 0 7 1 8 6 7 4 0 1]
[2 6 8 7 3 3 8 4 1 6 ... 1 3 9 5 0 1 4 8 3 4]
[1 2 9 5 1 2 9 9 5 4 ... 3 1 2 6 1 4 4 7 4 8]]
Click me to see the sample solution
202. Write a NumPy program to calculate the arithmetic means of corresponding elements of two given arrays of same size. Go to the editor
Sample Output:
Array1:
[[2 5 2]
[1 5 5]]
Array2:
[[5 3 4]
[3 2 5]]
Arithmetic means of corresponding elements of said two arrays:
[[3.5 4. 3. ]
[2. 3.5 5. ]]
Click me to see the sample solution
203. Write a NumPy program to create a 11x3 array filled with student information (id, class and name) and shuffle the said array rows starting from 3rd to 9th. Go to the editor
Sample Output:
Original array:
[['stident_id' 'Class' 'Name']
['01' 'V' 'Debby Pramod']
['02' 'V' 'Artemiy Ellie']
['03' 'V' 'Baptist Kamal']
['04' 'V' 'Lavanya Davide']
['05' 'V' 'Fulton Antwan']
['06' 'V' 'Euanthe Sandeep']
['07' 'V' 'Endzela Sanda']
['08' 'V' 'Victoire Waman']
['09' 'V' 'Briar Nur']
['10' 'V' 'Rose Lykos']]
Shuffle the said array rows starting from 3rd to 9th
[['stident_id' 'Class' 'Name']
['01' 'V' 'Debby Pramod']
['02' 'V' 'Artemiy Ellie']
['03' 'V' 'Baptist Kamal']
['07' 'V' 'Endzela Sanda']
['04' 'V' 'Lavanya Davide']
['06' 'V' 'Euanthe Sandeep']
['05' 'V' 'Fulton Antwan']
['08' 'V' 'Victoire Waman']
['09' 'V' 'Briar Nur']
['10' 'V' 'Rose Lykos']]
Click me to see the sample solution
204. Write a NumPy program to extract all the rows from a given array where a specific column starts with a given character. Go to the editor
Sample Output:
Original array:
[['01' 'V' 'Debby Pramod']
['02' 'V' 'Artemiy Ellie']
['03' 'V' 'Baptist Kamal']
['04' 'V' 'Lavanya Davide']
['05' 'V' 'Fulton Antwan']
['06' 'V' 'Euanthe Sandeep']
['07' 'V' 'Endzela Sanda']
['08' 'V' 'Victoire Waman']
['09' 'V' 'Briar Nur']
['10' 'V' 'Rose Lykos']]
Student name starting with E :
[['06' 'V' 'Euanthe Sandeep']
['07' 'V' 'Endzela Sanda']]
Student id starting with 1 :
[['10' 'V' 'Rose Lykos']]
Click me to see the sample solution
205. Write a NumPy program to extract all the rows to compute the student weight from a given array (student information) where a specific column starts with a given character. Go to the editor
Sample Output:
Original array:
[['01' 'V' 'Debby Pramod' '30.21']
['02' 'V' 'Artemiy Ellie' '29.32']
['03' 'V' 'Baptist Kamal' '31.0']
['04' 'V' 'Lavanya Davide' '30.22']
['05' 'V' 'Fulton Antwan' '30.21']
['06' 'V' 'Euanthe Sandeep' '31.0']
['07' 'V' 'Endzela Sanda' '32.0']
['08' 'V' 'Victoire Waman' '29.21']
['09' 'V' 'Briar Nur' '30.0']
['10' 'V' 'Rose Lykos' '32.0']]
Total weight, where student name starting with E
63.0
Total weight, where student name starting with D
30.21
Click me to see the sample solution
Python-Numpy Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
Test your Python skills with w3resource's quiz
Python: Tips of the Day
Find current directory and file's directory:
To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The os and os.path modules.
- The __file__ constant
- os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
- os.path.dirname(path) (returns "the directory name of pathname path")
- os.getcwd() (returns "a string representing the current working directory")
- os.chdir(path) ("change the current working directory to path")
Ref: https://bit.ly/3fy0R6m
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework