numpy——ndarray运算(自生成股票数据案例)之逻辑运算、通用判断函数、三元运算符

一、逻辑运算

1.随机生成八只股票两周的交易日涨幅数据

>>> import numpy as np
>>> stock_change=np.random.normal(loc=0,scale=1,size=(8,10))
>>> stock_change
array([[-1.46202007,  0.95114736,  0.25165712, -1.80776718, -3.09513675,
        -1.8709032 ,  0.54687731,  0.2820287 ,  0.97948358,  0.20339662],
       [-1.27229295, -1.38005646, -0.60752081,  0.59927206,  0.53832373,
        -1.80358735, -0.14047816, -1.09983869,  0.51795043, -1.24475096],
       [-1.20619283, -1.99180927,  0.17533276, -0.19729808,  0.30560366,
        -1.75770429, -0.69314496, -1.74585604, -1.10605684,  1.3995043 ],
       [ 0.75374035,  1.76062662, -0.37416021, -0.4627814 , -1.28653669,
        -0.34524617, -1.07785026,  1.71138629,  1.49583181,  0.0420216 ],
       [-1.33479791, -0.93622978,  1.426623  ,  1.51883403, -0.18363733,
         0.08131291, -0.24989836,  0.15593843,  1.55480894,  1.31589355],
       [ 0.44094674, -0.84947326,  0.0424738 , -0.33424018,  0.26616388,
        -0.01324623, -0.3610848 , -1.17807392,  0.33983419, -0.7651107 ],
       [ 0.1741568 , -1.56346108,  1.12376595,  1.44652797,  1.10749741,
        -1.11022536, -0.83824722,  0.91788999,  0.82594165,  0.84196209],
       [-0.51520268, -0.62884247,  1.09889016, -0.53605015,  1.31925862,
        -0.73569019,  0.57139481, -0.43308124,  0.62886824,  0.75989891]])
>>> 

2.逻辑判断,涨跌幅大于0.5标记为True,否则为False

>>> stock_change>0.5
array([[False,  True, False, False, False, False,  True, False,  True,
        False],
       [False, False, False,  True,  True, False, False, False,  True,
        False],
       [False, False, False, False, False, False, False, False, False,
         True],
       [ True,  True, False, False, False, False, False,  True,  True,
        False],
       [False, False,  True,  True, False, False, False, False,  True,
         True],
       [False, False, False, False, False, False, False, False, False,
        False],
       [False, False,  True,  True,  True, False, False,  True,  True,
         True],
       [False, False,  True, False,  True, False,  True, False,  True,
         True]])

3.赋值

>>> stock_change[stock_change>0.5]=1
#stock_change[stock_change>0.5]称为布尔索引,即满足这个条件的值,赋值为1
>>> stock_change
array([[-1.46202007,  1.        ,  0.25165712, -1.80776718, -3.09513675,
        -1.8709032 ,  1.        ,  0.2820287 ,  1.        ,  0.20339662],
       [-1.27229295, -1.38005646, -0.60752081,  1.        ,  1.        ,
        -1.80358735, -0.14047816, -1.09983869,  1.        , -1.24475096],
       [-1.20619283, -1.99180927,  0.17533276, -0.19729808,  0.30560366,
        -1.75770429, -0.69314496, -1.74585604, -1.10605684,  1.        ],
       [ 1.        ,  1.        , -0.37416021, -0.4627814 , -1.28653669,
        -0.34524617, -1.07785026,  1.        ,  1.        ,  0.0420216 ],
       [-1.33479791, -0.93622978,  1.        ,  1.        , -0.18363733,
         0.08131291, -0.24989836,  0.15593843,  1.        ,  1.        ],
       [ 0.44094674, -0.84947326,  0.0424738 , -0.33424018,  0.26616388,
        -0.01324623, -0.3610848 , -1.17807392,  0.33983419, -0.7651107 ],
       [ 0.1741568 , -1.56346108,  1.        ,  1.        ,  1.        ,
        -1.11022536, -0.83824722,  1.        ,  1.        ,  1.        ],
       [-0.51520268, -0.62884247,  1.        , -0.53605015,  1.        ,
        -0.73569019,  1.        , -0.43308124,  1.        ,  1.        ]])

二、通用判断函数

1.np.all()
只要有一个False就返回False,只有全是True才返回True
判断stock_change[0:2,0:5]是否全是上涨的

>>> stock_change[0:2,0:5]>0
array([[False, False,  True, False,  True],
       [ True,  True, False, False,  True]])
>>> np.all(stock_change[0:2,0:5]>0)
False
>>> 

2.np.any()
只要有一个True就返回True
判断前五支股票是否上涨

>>> stock_change[0:5,:]>0
array([[False, False,  True, False,  True, False, False,  True,  True,
         True],
       [ True,  True, False, False,  True, False, False,  True, False,
        False],
       [ True,  True, False,  True, False,  True, False, False,  True,
        False],
       [ True,  True,  True, False, False,  True,  True, False,  True,
        False],
       [ True,  True, False, False,  True,  True, False,  True,  True,
         True]])
>>> np.any(stock_change[0:5,:]>0)
True
>>> 

三、三元运算符

1.np.where(布尔值,True的位置的值,False的位置的值)
判断前四个股票前四天的涨跌幅,大于0的变为1,否则为0。

>>> temp = stock_change[0:4,0:4]
>>> temp
array([[-1.46647737, -0.85875713,  1.52534704, -0.50528693],
       [ 0.34089158,  0.9204162 , -0.95259284, -0.49692041],
       [ 0.4610343 ,  0.6506377 , -0.89275792,  1.10992143],
       [ 0.92498422,  1.05165063,  0.9136756 , -1.01392671]])
>>> np.where(temp>0,1,0)
array([[0, 0, 1, 0],
       [1, 1, 0, 0],
       [1, 1, 0, 1],
       [1, 1, 1, 0]])

2.复杂判断
逻辑且,逻辑或
np.logical_and
np.logical_or

>>> np.logical_and(temp>0.5,temp<1)#大于0.5且小于1
array([[False, False, False, False],
       [False,  True, False, False],
       [False,  True, False, False],
       [ True, False,  True, False]])
>>> np.where(np.logical_and(temp>0.5,temp<1),1,0)#True的变成1,False的变成0
array([[0, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 1, 0, 0],
       [1, 0, 1, 0]])

你可能感兴趣的:(numpy,python,python,numpy)