import numpy as np
In [8]:
a = np.arange(15).reshape(3,5)
In [10]:
a
Out[10]:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
In [11]:
a.shape
Out[11]:
(3, 5)
In [12]:
a.dtype.name
Out[12]:
'int32'
In [16]:
np.empty((2,3))
Out[16]:
In [17]:
np.arange(10,30,5) # 10-30 步长为5
Out[17]:
In [18]:
np.arange(0,2,0.3) #支持浮点数
Out[18]:
In [19]:
np.linspace(0,2,9) # 生成 0 到 2 的 9 个数,
Out[19]:
In [21]:
from numpy import pi
In [27]:
x = np.linspace(0,2*pi,100) # 在有多个小数位时的使用
x
Out[27]:
In [28]:
f = np.sin(x)
f
Out[28]:
In [31]:
a = np.array([20,30,40,50])
b = np.arange(4)
c = a-b
c
Out[31]:
In [32]:
b**2
Out[32]:
In [33]:
10*np.sin(a)
Out[33]:
In [34]:
a<35
Out[34]:
In [35]:
A = np.array([[1,1],[0,1]])
B = np.array([[2,0],[3,4]])
A*B
Out[35]:
In [36]:
A.dot(B)
Out[36]:
In [37]:
np.dot(A,B)
Out[37]:
In [39]:
a = np.ones((2,3),dtype = int)
b = np.random.random((2,3))
a *= 3
a
Out[39]:
In [40]:
b += a
b
Out[40]:
In [41]:
a += b #TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('int32') with casting rule 'same_kind'
In [42]:
a = np.ones(3,dtype=np.int32)
b = np.linspace(0,pi,3)
b.dtype.name
Out[42]:
In [44]:
c = a+b
c
Out[44]:
In [45]:
a = np.random.random((2,3))
a
Out[45]:
In [46]:
a.sum()
Out[46]:
In [47]:
a.min()
Out[47]:
In [48]:
b = np.arange(12).reshape(3,4)
b
Out[48]:
In [49]:
b.sum(axis=0)
Out[49]:
In [50]:
b.min(axis=1)
Out[50]:
In [51]:
b.cumsum(axis=1)
Out[51]:
In [52]:
a = np.arange(10)**3
a
Out[52]:
In [54]:
a[:6:2] = -1000
a
Out[54]:
In [55]:
a[::-1]
Out[55]:
In [58]:
for i in a:
print(i**(1/3.))
In [59]:
def f(x,y):
return 10*x + y
b = np.fromfunction(f,(5,4),dtype=int)
b
Out[59]:
In [61]:
b[1,2] # [1,2] 里面是索引
Out[61]:
In [62]:
b[0:5,1] # 每行的第 2 个元素
Out[62]:
In [63]:
b[-1] # 最后一行,等价于 b[-1,:]
Out[63]:
In [64]:
c = np.array([[[0,1,2],[10,12,13]],[[100,101,102],[110,112,113]]])
c.shape
Out[64]:
In [65]:
c[1,...]
Out[65]:
In [68]:
c[1,:,:]
Out[68]:
In [69]:
c[...,2]
Out[69]:
In [75]:
a = np.arange(12).reshape(3,4) # bool切片需要保证 元素个数对应行(bool数组的个数等于被切数组的行)或者列
b1 = np.array([True,True,True])
b2 = np.array([True,True,False,True])
a[b1,b2] # 神奇的一幕,每行只取了一个元素
Out[75]:
In [76]:
for row in a:
print(row)
In [77]:
for element in a.flat: # flat属性,是数组元素的一个迭代器
print(element)
In [78]:
# 数组赋值
a = np.arange(5)
In [79]:
a
Out[79]:
In [80]:
a[[1,3,4]] = 0
In [81]:
a
Out[81]:
In [82]:
a = np.arange(5)
In [83]:
a[[0,0,2]] = [1,2,3]
a
Out[83]:
In [84]:
a = np.arange(5)
a[[0,0,2]] += 1
a # 注意,使用 += 的时候,即使索引0出现了两次,但是仅增加一次,这是因为Python要求 a+=1 和 a=a+1 等同
Out[84]:
In [87]:
# 数组的组合 和 拆分
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
a
Out[87]:
In [88]:
b
Out[88]:
In [92]:
np.vstack((a,b)) # 纵向拼接,增加样本个数
Out[92]:
In [93]:
np.hstack((a,b)) #横向拼接, 增加特征量
Out[93]:
In [100]:
# 函数column_stack:仅支持以序列顺序将多个一维数组或者一个二维数组按对位组合成新的二维数组
a = np.array((1,2,3))
b = np.array((2,3,4))
np.column_stack((a,b))
Out[100]:
In [95]:
a = np.floor(10*np.random.random((2,12)))
a
Out[95]:
In [96]:
np.hsplit(a,3) # 将数组a切割成3份
Out[96]:
In [97]:
np.hsplit(a,(3,4)) # 在数组a的第3索引前和第4索引前切割
Out[97]:
In [98]:
x = np.arange(9)
In [99]:
np.split(x,[3,5,6,10]) # 直接用split 更加灵活
Out[99]: