逻辑运算

初始化一个股票涨幅变化的数组

stock_change = np.random.normal(loc=0, scale=1, size=(8, 10))

将其输出出来看看

array([[ 0.89645874,  1.45037927, -0.01620999,  2.09949884,  1.85993243,
        -0.37292596,  1.71902613, -0.52602534, -0.83549712, -1.26605677],
       [-0.72969931,  0.01288957,  0.88282703,  0.87208188,  0.50612155,
         1.58175501,  0.04236072,  0.26521438,  0.3042681 , -0.34315946],
       [-0.23597763,  0.89052316,  0.71693436,  0.96146532, -1.2500618 ,
        -0.45546587, -1.4015062 , -0.32708862,  0.16733823, -1.35628827],
       [-0.54845383,  0.29204192, -0.48656491,  1.80355475, -1.37148035,
        -0.36061307, -0.97104768,  0.04412891, -1.41602417, -1.04087923],
       [ 0.30773055, -0.41619138,  2.18740321, -0.52734335, -0.78613636,
         0.64381646,  1.66345618, -0.47272867,  0.03438599, -0.34474344],
       [ 1.28033718,  0.98570696,  0.90548589,  0.08737387,  0.76172384,
         2.26676882, -1.05478047,  0.8607113 , -1.50994433, -0.97010282],
       [ 1.08197209, -0.93683095,  1.45637994,  0.95161678, -0.52549897,
        -0.11531663,  0.54464256,  0.88813627,  0.93374751, -0.24459157],
       [ 0.15776581, -0.57931045,  0.96262219, -0.487419  ,  0.47749288,
        -1.19925145, -0.90684539,  2.32473607, -0.85620056, -0.01211961]])

进行逻辑判断

# 逻辑判断, 如果涨跌幅大于0.5就标记为True 否则为False
stock_change > 0.5

输出为这样

array([[ True,  True, False,  True,  True, False,  True, False, False,
        False],
       [False, False,  True,  True,  True,  True, False, False, False,
        False],
       [False,  True,  True,  True, False, False, False, False, False,
        False],
       [False, False, False,  True, False, False, False, False, False,
        False],
       [False, False,  True, False, False,  True,  True, False, False,
        False],
       [ True,  True,  True, False,  True,  True, False,  True, False,
        False],
       [ True, False,  True,  True, False, False,  True,  True,  True,
        False],
       [False, False,  True, False, False, False, False,  True, False,
        False]])

也可以选择给大于0.5的地方重新赋值

stock_change[stock_change > 0.5] = 1.1
array([[ 1.1       ,  1.1       , -0.01620999,  1.1       ,  1.1       ,
        -0.37292596,  1.1       , -0.52602534, -0.83549712, -1.26605677],
       [-0.72969931,  0.01288957,  1.1       ,  1.1       ,  1.1       ,
         1.1       ,  0.04236072,  0.26521438,  0.3042681 , -0.34315946],
       [-0.23597763,  1.1       ,  1.1       ,  1.1       , -1.2500618 ,
        -0.45546587, -1.4015062 , -0.32708862,  0.16733823, -1.35628827],
       [-0.54845383,  0.29204192, -0.48656491,  1.1       , -1.37148035,
        -0.36061307, -0.97104768,  0.04412891, -1.41602417, -1.04087923],
       [ 0.30773055, -0.41619138,  1.1       , -0.52734335, -0.78613636,
         1.1       ,  1.1       , -0.47272867,  0.03438599, -0.34474344],
       [ 1.1       ,  1.1       ,  1.1       ,  0.08737387,  1.1       ,
         1.1       , -1.05478047,  1.1       , -1.50994433, -0.97010282],
       [ 1.1       , -0.93683095,  1.1       ,  1.1       , -0.52549897,
        -0.11531663,  1.1       ,  1.1       ,  1.1       , -0.24459157],
       [ 0.15776581, -0.57931045,  1.1       , -0.487419  ,  0.47749288,
        -1.19925145, -0.90684539,  1.1       , -0.85620056, -0.01211961]])

也可以选择一部分进行判断

# 判断stock_change[0:2, 0:5]是否全是上涨的
stock_change[0:2, 0:5] > 0
np.all(stock_change[0:2, 0:5] > 0)
# 输出为False
# 判断前5只股票这段期间是否有上涨的
np.any(stock_change[:5, :] > 0)
# 输出为True

Last updated