2) NumPy Array
2) NumPy Array
10. Write a NumPy program to create a 8x8 matrix and fill it with a
checkerboard pattern.
Checkerboard pattern:
[[0 1 0 1 0 1 0 1]
..........
PYTHON 8802551718/9990951718 NumPy Array
NUMPY ARRAY
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]]
11. Write a NumPy program to convert a list and tuple into arrays.
List to array:
[1 2 3 4 5 6 7 8]
Tuple to array:
[[8 4 6]
[1 2 3]]
15. 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]
17. Write a NumPy program to test whether each element of a 1-D array
is also present in a second array.
Expected Output:
Array1: [ 0 10 20 40 60]
Array2: [0, 40]
Compare each element of array1 and array2
[ True False False True False]
18. Write a NumPy program to find common values between two arrays.
Expected Output:
Array1: [ 0 10 20 40 60]
Array2: [10, 30, 40]
Common values between two arrays:
[10 40]
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]
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.
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]
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.
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]
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.
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]
24. Write a NumPy program to test whether any array element along a
given axis evaluates to True.
Note: 0 evaluates to False in NumPy.
27. Write a NumPy program to find the indices of the maximum and
minimum values along the given axis of an array.
Original array: [1 2 3 4 5 6]
Maximum Values: 5
Minimum Values: 0
a<b
[ True True]
a <= b
[ True True]
29. Write a NumPy program to sort an along the first, last axis of an
array.
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]]
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).
first_names = (Betsey, Shelley, Lanell, Genesis, Margery)
last_names = (Battle, Brien, Plotner, Stahl, Woolum)
Expected Output:
[1 3 2 4 0]
31. 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]))
33. Write a NumPy program to find the memory size of a NumPy array.
Expected Output:
128 bytes
[10 20 30 20 40 50]
(2, 3)
int32
40. 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]
PYTHON 8802551718/9990951718 NumPy Array
NUMPY ARRAY
[2 2 2 2 2]]
41. Write a NumPy program to create an array of 10's with the same
shape and type of a given array.
Sample array: x = np.arange(4, dtype=np.int64)
Expected Output:
[10 10 10 10]
42. Write a NumPy program to create a 3-D array with ones on a diagonal
and zeros elsewhere.
Expected Output:
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
43. Write a NumPy program to create a 2-D array whose diagonal equals
[4, 5, 6, 8] and 0's elsewhere.
Expected Output:
[[4 0 0 0]
[0 5 0 0]
[0 0 6 0]
[0 0 0 8]]
44. Write a NumPy program to create a 1-D array going from 0 to 50 and
an array from 10 to 50.
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]
47. Write a NumPy program to create an array which looks like below
array.
Expected Output:
[[ 0. 0. 0.]
...........
[ 1. 1. 1.]]
48. Write a NumPy program to create an array which looks like below
array.
Expected Output:
[[ 2 3 4]
[ 5 6 7]
[ 0 9 10]
[ 0 0 13]]
49. Write a NumPy program to collapse a 3-D array into one dimension
array.
Expected Output:
3-D array:
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
PYTHON 8802551718/9990951718 NumPy Array
NUMPY ARRAY
50. Write a NumPy program to find the 4th element of a specified array.
Expected Output:
[[ 2 4 6]
[ 6 8 10]]
Forth e1ement of the array:
6
53. Write a NumPy program to move the specified axis backwards, until it
lies in a given position.
Move the following 3rd array axes to first position.
(2,3,4,5)
Sample Expected Output:
(2, 5, 3, 4)
55. Write a NumPy program to view inputs as arrays with at least two
dimensions, three dimensions.
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.]]]
56. Write a NumPy program to insert a new axis within a 2-D array.
2-D array of shape (3, 4).
Expected Output:
New shape will be will be (3, 1, 4).
59. Write a NumPy program to convert 1-D arrays as columns into a 2-D
array.
Sample array: (10,20,30), (40,50,60)
Expected Output:
PYTHON 8802551718/9990951718 NumPy Array
NUMPY ARRAY
[[10 40]
[20 50]
[30 60]]
60. Write a NumPy program to convert (in sequence depth wise (along
third axis)) two 1-D arrays into a 2-D array.
Sample array: (10,20,30), (40,50,60)
Expected Output:
[[[10 40]]
[[20 50]]
[[30 60]]]
62. Write a NumPy program to split of an array of shape 4x4 it into two
arrays along the second axis.
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)]
Expected Output:
Original array:
[[ 0 10 20]
[20 30 40]]
Number of non zero elements in the above array:
5
64. Write a NumPy program to create a 5x5 matrix with row values
ranging from 0 to 4.
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.]]
65. Write a NumPy program to test whether specified values are present
in an array.
Expected Output:
Original array:
[[ 1.12 2. 3.45]
[ 2.33 5.12 6. ]]
True
False
True
False
True
68. Write a NumPy program (using NumPy) to sum of all the multiples of
3 or 5 below 100.
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
[4 5 6 7]]
0:0
1:1
2:2
3:3
0:4
1:5
2:6
3:7
75. Write a NumPy program to create an array of zeros and three column
types (integer, float, character).
Expected Output:
[(1, 2., b'Albert Einstein') (2, 2., b'Edmond Halley')
(3, 3., b'Gertrude B. Elion')]
76. Write a NumPy program to create a function cube which cubes all the
elements of an array.
Expected Output:
[ 1 8 27]
78. Write a NumPy program to create a record array from a (flat) list of
arrays.
Sample arrays: [1,2,3,4], ['Red', 'Green', 'White', 'Orange'],
PYTHON 8802551718/9990951718 NumPy Array
NUMPY ARRAY
[12.20,15,20,40]
Expected Output:
(1, 'Red', 12.2)
(2, 'Green', 15.0)
(3, 'White', 20.0)
80. Write a NumPy program to convert a NumPy array into Python list
structure.
Expected Output:
Original array elements:
[[0 1]
[2 3]
[4 5]]
Array to list:
[[0, 1], [2, 3], [4, 5]]
[0 1]
Second column:
[2 3]
Third column:
[4 5]
Expected Output:
[0 1 2 3 4 5 6 7 8 9]
Expected Output:
[[ 10 20 30 100]
[ 40 50 60 200]]
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]]
88. Write a NumPy program to replace all elements of NumPy array that
are greater than specified array.
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]
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]
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]
91. Write a NumPy program to remove all rows in a NumPy array that
contain non-numeric values.
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.]]
95. Write a NumPy program to check whether the NumPy array is empty
or not.
Expected Output:
2
0
Original array:
[[20 20 20]
[30 30 30]
[40 40 40]]
Vector:
[20 30 40]
[[ 1. 1. 1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]
6000.0
100. Write a NumPy program to take values from a source array and put
them at specified indices of another array.
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.]
101. Write a NumPy program to print the full NumPy array, without
truncation.
Truncated output:
[ 0 1 2 ... 1997 1998 1999]
102. Write a NumPy program to convert a NumPy array into a csv file.
[8 9]]
105. Write a NumPy program to read a CSV data file and store records in
an array.
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)]
110. Write a NumPy program to remove nan values from a given array.
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.]
Sample Output:
[[1 4]
......
[3 5]]
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]
117. Write a NumPy program to compute the line graph of a set of data.
Sample Output:
Sample Output:
Original array:
[-6 -5 -4 -3 -2 -1 0 1 2 3 4 5]
Position of the index:
9
[3]]
[[2]
[3]
[4]]
Sequence of arrays along a new axis:
[[1]
[2]
[3]
[2]
[3]
[4]]
122. Write a NumPy program to find the index of the sliced elements as
follows from a given 4x4 array.
Sample Output:
Original arrays:
[[ 0 1 2 3]
........
[12 13 14 15]]
Sliced elements:
[ 0 5 11]
123. Write a NumPy program to create two arrays of size bigger and
smaller than a given array.
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]]
[[ 0 1 2 3]
...........
[20 21 22 23]]
After reverse the dimensions:
[[ 0 4 8 12 16 20]
............
[ 3 7 11 15 19 23]]
131. Write a NumPy program to split a given array into multiple sub-
arrays vertically (row-wise).
Sample Output:
Original arrays:
[[ 0. 1. 2. 3.]
.............
[12. 13. 14. 15.]]
Split an array into multiple sub-arrays vertically:
PYTHON 8802551718/9990951718 NumPy Array
NUMPY ARRAY
132. Write a NumPy program to split array into multiple sub-arrays along
the 3rd axis.
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.]]])]
134. Write a NumPy program to extract all the elements of the first row
from a given (4x4) array.
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]
135. Write a NumPy program to extract all the elements of the second
row from a given (4x4) array.
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]
136. Write a NumPy program to extract all the elements of the third
column from a given (4x4) array.
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]
137. Write a NumPy program to extract first and second elements of the
first and second rows from a given (4x4) array.
Sample Output:
PYTHON 8802551718/9990951718 NumPy Array
NUMPY ARRAY
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]]
138. Write a NumPy program to extract third and fourth elements of the
first and second rows from a given (4x4) array.
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]]
139. Write a NumPy program to extract first and third elements of the
first and third rows from a given (4x4) array.
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]]
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]]
141. Write a NumPy program to extract all the elements of the second
and third columns from a given (4x4) array.
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]]
142. Write a NumPy program to extract all the elements of the first and
fourth columns from a given (4x4) array.
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]
PYTHON 8802551718/9990951718 NumPy Array
NUMPY ARRAY
[12 15]]
143. Write a NumPy program to extract first element of the second row
and fourth element of fourth row from a given (4x4) array.
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]
144. Write a NumPy program to extract all the elements of the second
and third columns from a given (4x4) array.
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]]
145. Write a NumPy program to extract first, third and fifth elements of
the third and fifth rows from a given (6x6) array.
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]]
146. Write a NumPy program to add two arrays A and B of sizes (3,3)
and (,3).
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.]]
147. Write a NumPy program to create an array that represents the rank
of each item of a given array.
Sample Output:
Original array:
[24 27 30 29 18 14]
Rank of each item of the said array:
[2 3 5 4 1 0]
148. Write a NumPy program to copy data from a given array to another
array.
Sample Output:
Original array:
[24 27 30 29 18 14]
Copy of the said array:
[24 27 30 29 18 14]
149. Write a NumPy program to find elements within range from a given
array of numbers.
Sample Output:
PYTHON 8802551718/9990951718 NumPy Array
NUMPY ARRAY
Original array:
[ 1 3 7 9 10 13 14 17 29]
Elements within range: index position
(array([2, 3, 4, 5, 6, 7]),)
151. Write a NumPy program to get the row numbers in given array
where at least one item is larger than a specified value.
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]),)
[54 58 62 66 70 74 78 82 86]
154. Write a NumPy program to get a copy of a matrix with the elements
below the k-th diagonal zeroed.
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]]
True
False
True
157. Write a NumPy program to create a new array which is the average
of every consecutive triplet of elements of a given array.
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.]
Original arrays:
[[ 11 22 33 44 55]
[ 66 77 88 99 100]]
New array:
[[ 22 44 11 55 33]
[ 77 99 66 100 88]]
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
PYTHON 8802551718/9990951718 NumPy Array
NUMPY ARRAY
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.
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
164. Write a NumPy program to save as text a matrix which has in each
row 2 float and 1 string at the end.
Sample Output:
string
1 0 aaa
0 1 bbb
0 1 ccc
166. Write a NumPy program to combine last element with first element
of two given ndarray with different shapes.
Original arrays:
['PHP', 'JS', 'C++']
['Python', 'C#', 'NumPy']
After Combining:
['PHP' 'JS' 'C++Python' 'C#' 'NumPy']
Panda's DataFrame:
ABC
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'>
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.
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]]
173. Write a NumPy program to set zero to lower triangles along the last
two axes of a three-dimensional of a given array.
Original array:
[[[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
..............
PYTHON 8802551718/9990951718 NumPy Array
NUMPY ARRAY
[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.]]]
[ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38]
176. Write a NumPy program to create an array of 4,5 shape and swap
column1 with column4.
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]]
178. Write a NumPy program to replace all the nan (missing values) of a
given array with the mean of another array.
Original arrays:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
PYTHON 8802551718/9990951718 NumPy Array
NUMPY ARRAY
[[ 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]]
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.
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]
182. Write a NumPy program to subtract the mean of each row of a given
matrix.
PYTHON 8802551718/9990951718 NumPy Array
NUMPY ARRAY
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]]
183. Write a NumPy program to test whether a given 2D array has null
columns or not.
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
New array:
[1. 0. 0. 2. 0. 0. 3. 0. 0. 4. 0. 0. 5. 0. 0. 6. 0. 0. 7. 0. 0. 8.]
188. Write a NumPy program to extract rows with unequal values (e.g.
[1,1,2]) from 10x3 matrix.
Original vector:
[[3 2 0]
........
[3 0 2]]
Rows with unequal values:
[[3 2 0]
[2 3 1]
.........
PYTHON 8802551718/9990951718 NumPy Array
NUMPY ARRAY
[3 0 2]]
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).
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]
191. Write a NumPy program to get the block-sum (block size is 5x5)
from a given array of shape 25x25.
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.]
PYTHON 8802551718/9990951718 NumPy Array
NUMPY ARRAY
192. Write a NumPy program to extract all the contiguous 4x4 blocks
from a given random 12x12 matrix.
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]]]]