+ 1

i want to select the president whose tall more than 182 and age more than 50 what is the error in my code

import numpy as np heights_arr = np.array([189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191]) ages_arr = np.array([57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70]).reshape((-1,1)) heights_arr = heights_arr.reshape((45,1)) height_age_arr = np.hstack((heights_arr, ages_arr)) mask = height_age_arr[:, 0] >= 182 tall_presidents = (height_age_arr[mask, ]) talll = (height_age_arr[mask,1] >= 50) wt = (mask[ ,talll]) print(tall_presidents) print(wt) tall_presidents.shape print(tall_presidents.shape)

22nd Oct 2020, 7:40 AM
Amr Asfoor
Amr Asfoor - avatar
1 ответ
+ 1
Error In you code: At line 12, wt = (mask[ ,talll]) First the syntax is wrong and second size of "talll" array is smaller than "mask" array(so don't have enough corresponding Boolean for number of elements in mask) Right Code: import numpy as np heights_arr = np.array([189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177, 185, 188, 188, 182, 185, 191]) ages_arr = np.array([57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70]).reshape((-1,1)) heights_arr = heights_arr.reshape((45,1)) height_age_arr = np.hstack((heights_arr, ages_arr)) #All taller than 182 talls = heights_arr > 182 #All older than 50 olds = ages_arr > 50 #Adding both Boolean array mask = np.logical_and(talls,olds).reshape(45,) print(height_age_arr[mask]) Hope It Helps
22nd Oct 2020, 5:13 PM
Hacker Badshah
Hacker Badshah - avatar