版权声明:2018/4/10重启blog;转载请注明出处 https://blog.csdn.net/zhaiqiming2010/article/details/86539810
import numpy as np
In [2]:
x = np.arange(16)
In [3]:
x
Out[3]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
In [4]:
x[3:9:2]
Out[4]:
array([3, 5, 7])
In [5]:
# 要返回 2 , 5 , 8 位置的元素
[x[3], x[5], x[8]]
Out[5]:
[3, 5, 8]
In [6]:
ind= [3, 5, 8]
x[ind]
Out[6]:
array([3, 5, 8])
In [7]:
ind = np.array([[0, 2],[1,3]])
ind
Out[7]:
array([[0, 2],
[1, 3]])
In [8]:
x[ind]
Out[8]:
array([[0, 2],
[1, 3]])
In [10]:
X = x.reshape(4, -1)
In [11]:
X
Out[11]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
In [12]:
row = np.array([0, 1, 2])
col = np.array([1, 2, 3])
X[row,col]
Out[12]:
array([ 1, 6, 11])
In [13]:
X[0,col]
Out[13]:
array([1, 2, 3])
In [14]:
X[:2,col]
Out[14]:
array([[1, 2, 3],
[5, 6, 7]])
In [17]:
# 布尔索引
col = [True,False,True,True] # false是我们不感兴趣的地方
X[1:3,col]
Out[17]:
array([[ 4, 6, 7],
[ 8, 10, 11]])
In [16]:
X
Out[16]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
In [18]:
x
Out[18]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
In [19]:
x < 3
Out[19]:
array([ True, True, True, False, False, False, False, False, False,
False, False, False, False, False, False, False])
In [20]:
x > 3
Out[20]:
array([False, False, False, False, True, True, True, True, True,
True, True, True, True, True, True, True])
In [21]:
x ==3
Out[21]:
array([False, False, False, True, False, False, False, False, False,
False, False, False, False, False, False, False])
In [22]:
2* x == 24 - 4*x
Out[22]:
array([False, False, False, False, True, False, False, False, False,
False, False, False, False, False, False, False])
In [23]:
X <6
Out[23]:
array([[ True, True, True, True],
[ True, True, False, False],
[False, False, False, False],
[False, False, False, False]])
In [24]:
x
Out[24]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
In [25]:
np.sum(x<=3)
Out[25]:
4
In [26]:
np.count_nonzero(x<=3)
Out[26]:
4
In [27]:
np.any(x ==0)
Out[27]:
True
In [28]:
np.any(x<0)
Out[28]:
False
In [29]:
np.all(x>=0)
Out[29]:
True
In [30]:
np.all(x>0)
Out[30]:
False
In [32]:
np.sum(X%2 == 0)
Out[32]:
8
In [33]:
X
Out[33]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
In [34]:
np.sum(X%2==0, axis=1) # 看每行有多少个偶数
Out[34]:
array([2, 2, 2, 2])
In [35]:
np.sum(X%2==0, axis=0) ## 看每列有多少个偶数
Out[35]:
array([4, 0, 4, 0])
In [37]:
np.all(X>0, axis=1) #
Out[37]:
array([False, True, True, True])
In [38]:
np.sum((x>3)&&(x<10))
File "<ipython-input-38-3a57998567a8>", line 1
np.sum((x>3)&&(x<10))
^
SyntaxError: invalid syntax
In [39]:
np.sum((x>3)&(x<10))
Out[39]:
6
In [40]:
np.sum((x%2 ==0)|(x>10))
Out[40]:
11
In [41]:
# 非运算
np.sum(~(x==0))
Out[41]:
15
In [42]:
x
Out[42]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
In [43]:
x<5
Out[43]:
array([ True, True, True, True, True, False, False, False, False,
False, False, False, False, False, False, False])
In [44]:
x[x<5]
Out[44]:
array([0, 1, 2, 3, 4])
In [45]:
x[x%2==0]
Out[45]:
array([ 0, 2, 4, 6, 8, 10, 12, 14])
In [48]:
X[X[:,3]%3 == 0,:]
Out[48]:
array([[ 0, 1, 2, 3],
[12, 13, 14, 15]])
In [46]:
#panads
Out[46]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
In [47]:
X[:,3]
Out[47]:
array([ 3, 7, 11, 15])