美文网首页
3-10 Numpy中的比较和FancyIndexing

3-10 Numpy中的比较和FancyIndexing

作者: SRFHolmes | 来源:发表于2018-07-20 21:15 被阅读0次

FancyIndexing

import numpy as np
x=np.arange(16)

#一维
[x[3],x[5],x[8]]
=
ind=[3,5,8]
x[ind]

#二维
ind=np.array([[0,2],
            [1,3]])
x[ind]
#注意array中的[]

X=x.reshape(4,-1)
X
'''
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
'''

#坐标做索引
row=np.array([0,1,2])
col=np.array([1,2,3])
X[row,col]
#array([ 1,  6, 11])

X[0,col]
#array([1, 2, 3])

X[:2,col]
'''
array([[1, 2, 3],
      [5, 6, 7]])
'''

col=[True,False,True,True]
X[1:3,col]
'''
array([[ 4,  6,  7],
      [ 8, 10, 11]])
'''
#True的显示False的不显示

numpy.array的比较

x<3
'''
array([ True,  True,  True, False, False, #False, False, False, False,
False, False, False, False, False,
False, False])
'''

2*x==24-4*x
'''
array([False, False, False, False,  True, #False, False, False, False,
       False, False, False, False, False, #False, False])
'''

np.sum(x<=3)
=
np.count_nonzero(x<=3)

np.any()
#任意一个是True 就返回True
np.all()
#所有返回True才返回True
np.any(x==0)

np.sum(X%2==0)
#8
np.sum(X%2==0,axis=1)
#array([2, 2, 2, 2])

np.sum((x>3)&(x<10))
#此处为位与运算符 &&会报错

np.sum((x%2==0)|(x>10))

#非运算符  
np.sum(~(x==0))

x[x<5]

X[:,1]
#所有行 第一列

X[X[:,3]%3==0,:]

Pandas

相关文章

网友评论

      本文标题:3-10 Numpy中的比较和FancyIndexing

      本文链接:https://www.haomeiwen.com/subject/pqaqmftx.html