numpy數組-過濾數組

可使用一個numpy數組做爲索引數組去過濾原數組,索引數組裏爲true的值,保留,爲false的值去掉數組

import numpy as np

使用索引數組學習

a = np.array([1, 2, 3, 4])
b = np.array([True, True, False, False])
print a[b]                                       #[1 2]
print a[np.array([True, False, True, False])]    #[1 3]

經過對原數組進行向量化運算獲得索引數組code

a = np.array([1, 2, 3, 2, 1])
b = (a >= 2)
print a[b]            #[2 3 2]
print a[a >= 2]       #[2 3 2]

經過對某一數組進行向量化運算獲得索引數組索引

a = np.array([1, 2, 3, 4, 5])
b = np.array([1, 2, 3, 2, 1])
print b == 2      #[False True False True False]
print a[b == 2]   #[2 4]

一個例子:import

# 20個學生在課程上所花費的時間
time_spent = np.array([
       12.89697233,    0.        ,   64.55043217,    0.        ,
       24.2315615 ,   39.991625  ,    0.        ,    0.        ,
      147.20683783,    0.        ,    0.        ,    0.        ,
       45.18261617,  157.60454283,  133.2434615 ,   52.85000767,
        0.        ,   54.9204785 ,   26.78142417,    0.
])

# 20個學生參加學習的天數
days_to_cancel = np.array([
      4,   5,  37,   3,  12,   4,  35,  38,   5,  37,   3,   3,  68,
     38,  98,   2, 249,   2, 127,  35
])

def mean_time_for_paid_students(time_spent, days_to_cancel):
    '''
    計算參加課程大於等於7天的學平生均在課程上所花的時間
    '''
    index_array = days_to_cancel >= 7
    mean_time = time_spent[index_array].mean()
    return mean_time

print(mean_time_for_paid_students(time_spent, days_to_cancel))

# 結果: 41.0540034855
相關文章
相關標籤/搜索