numpy array using data processing

numpy array using data processing

meshgrid function

understanding:

Two-dimensional coordinate system, X-axis can take three values 1,2,3, Y-axis can take three values 7,8, ask how many points the coordinates can be obtained?
Apparent is 6:
(1,7) ( 2,7) (3,7)
(1,8) (2,8) (3,8)

>>> import numpy as np#导入numpy
>>> a=np.array([1,2,3])#创建一维数组
>>> b=np.array([7,8])
>>> res=np.meshgrid(a,b)#获取所有点的横坐标和纵坐标
>>> res#返回list,有两个元素,第一个元素是X轴的取值,第二个元素是Y轴的取值
[array([[1, 2, 3],
       [1, 2, 3]]), array([[7, 7, 7],
       [8, 8, 8]])]

Generating a rendered image coordinates:

>>> import numpy as np
>>> points=np.arange(-5,5,0.01)#从-5到5步长为0.01
>>> xs,ys=np.meshgrid(points,points)
>>> ys
array([[-5.  , -5.  , -5.  , ..., -5.  , -5.  , -5.  ],
       [-4.99, -4.99, -4.99, ..., -4.99, -4.99, -4.99],
       [-4.98, -4.98, -4.98, ..., -4.98, -4.98, -4.98],
       ...,
       [ 4.97,  4.97,  4.97, ...,  4.97,  4.97,  4.97],
       [ 4.98,  4.98,  4.98, ...,  4.98,  4.98,  4.98],
       [ 4.99,  4.99,  4.99, ...,  4.99,  4.99,  4.99]])
>>> z=np.sqrt(xs**2+ys**2)#函数的求值运算
>>> z
array([[7.07106781, 7.06400028, 7.05693985, ..., 7.04988652, 7.05693985,
        7.06400028],
       [7.06400028, 7.05692568, 7.04985815, ..., 7.04279774, 7.04985815,
        7.05692568],
       [7.05693985, 7.04985815, 7.04278354, ..., 7.03571603, 7.04278354,
        7.04985815],
       ...,
       [7.04988652, 7.04279774, 7.03571603, ..., 7.0286414 , 7.03571603,
        7.04279774],
       [7.05693985, 7.04985815, 7.04278354, ..., 7.03571603, 7.04278354,
        7.04985815],
       [7.06400028, 7.05692568, 7.04985815, ..., 7.04279774, 7.04985815,
        7.05692568]])
>>> import matplotlib.pyplot as plt
>>> plt.imshow(z, cmap=plt.cm.gray); plt.colorbar()
<matplotlib.image.AxesImage object at 0x00000000087D6EB8>
<matplotlib.colorbar.Colorbar object at 0x000000000D8F4FD0>
>>> plt.title("Image plot of $\sqrt{x^2 + y^2}$ for a grid of values")
Text(0.5, 1.0, 'Image plot of $\\sqrt{x^2 + y^2}$ for a grid of values')
>>>plt.show()

The conditional logic array operation expressed as

According to the selection of the array of values ​​cond, cond if the value is True, then select the corresponding value x, otherwise selecting a corresponding value of y, such cond [0] = True, then the result [0] It should be placed at yarr [0] value

>>> xarr=np.array([1.1,1.2,1.3,1.4,1.5])
>>> yarr=np.array([2.1,2.2,2.3,2.4,2.5])
>>> cond=np.array([True,False,True,True,False])
>>> result=[(x if c else y) for x,y,c in zip(xarr,yarr,cond)]
>>> result
[1.1, 2.2, 1.3, 1.4, 2.5]

where method

Use vector

where the above methods may be substituted wording

>>> result=np.where(cond,xarr,yarr)
>>> result
array([1.1, 2.2, 1.3, 1.4, 2.5])

Scalar

np.where second and third parameters need not be an array, which can be a scalar value. In the data analysis, where typically for generating a new array in accordance with another array. Suppose there is a matrix composed of random data, you wish to replace all positive values ​​2, -2 replace all negative values. If the use of np.where, will be very simple:

>>> arr=np.random.randn(4,4)
>>> arr
array([[-0.02461196, -0.11867552,  0.49004256, -0.25236766],
       [ 1.66484493, -0.41180743, -0.34837293, -2.61422661],
       [ 0.51476568, -0.41451909,  1.28553372,  0.13866799],
       [-0.94795594, -1.49302099,  1.18480636,  0.20310613]])
>>> arr>0
array([[False, False,  True, False],
       [ True, False, False, False],
       [ True, False,  True,  True],
       [False, False,  True,  True]])
>>> np.where(arr>0,2,-2)
array([[-2, -2,  2, -2],
       [ 2, -2, -2, -2],
       [ 2, -2,  2,  2],
       [-2, -2,  2,  2]])

Scalar Vector bound

If the corresponding location is True, the new array corresponds to position 2, otherwise it is the original value

>>> np.where(arr>0,2,arr)
array([[-0.02461196, -0.11867552,  2.        , -0.25236766],
       [ 2.        , -0.41180743, -0.34837293, -2.61422661],
       [ 2.        , -0.41451909,  2.        ,  2.        ],
       [-0.94795594, -1.49302099,  2.        ,  2.        ]])

Statistical Methods

mean function

Averaging all the elements

>>> import numpy as np
>>> arr=np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> arr.mean()
5.0
>>> np.mean(arr)
5.0

sum function

Sum of all the elements

>>> arr.sum()
45

Alternatively axis evaluation

>>> arr.mean(axis=1)#列求平均值
array([2., 5., 8.])
>>> arr.mean(axis=0)#行求平均值
array([4., 5., 6.])
>>> arr.sum(axis=0)
array([12, 15, 18])#行求和
>>> arr.sum(axis=1)
array([ 6, 15, 24])#列求和

cumsum function

1. The one-dimensional input a (List may be, may be Array, assuming a = [1, 2, 3, 4, 5, 6, 7], and is added to the current column prior to the current row, as follows:

>>>import numpy as np
>>> a=[1,2,3,4,5,6,7]
>>> np.cumsum(a)
array([  1,   3,   6,  10,  15,  21,  28,  36,  45,  55,  75, 105])

2. For the two-dimensional input a, axis = 0 (line 1 does not move, the accumulated row to another row 1); axis = 1 (into the innermost layer, is converted into the column processing does not move the first column, the first. an additive to other columns), as follows:

>>>import numpy as np
>>> c=[[1,2,3],[4,5,6],[7,8,9]]
>>> np.cumsum(c,axis=0)
array([[ 1,  2,  3],
       [ 5,  7,  9],
       [12, 15, 18]])
>>> np.cumsum(c,axis=1)
array([[ 1,  3,  6],
       [ 4,  9, 15],
       [ 7, 15, 24]])

cumprod function

Similarly multiplicative method

>>> arr.cumprod(axis=1)
array([[  1,   2,   6],
       [  4,  20, 120],
       [  7,  56, 504]], dtype=int32)

Guess you like

Origin www.cnblogs.com/mengxiaoleng/p/11620764.html