numpy教程:
https://numpy.org/devdocs/user/quickstart.html#array-creation
https://numpy.org/devdocs/user/index.html
python展开代码import numpy as np
a = np.arange(15).reshape(3, 5) #np.arange
a = np.arange( 10, 30, 5 ) 
start:可忽略不写,默认从0开始;起始值
stop:结束值;生成的元素不包括结束值
step:可忽略不写,默认步长为1;步长
dtype:默认为None,设置显示元素的数据类型
a = np.array([2,3,4]) #列表导入
b = np.array([(1.5,2,3), (4,5,6)])  #多维
c = np.array( [ [1,2], [3,4] ], dtype=complex ) #显式给出类型
a =  np.zeros( (3,4) )
a =  np.ones( (2,3,4), dtype=np.int16 ) 
a =  np.empty( (2,3) )
# shape和矩阵a一样的 全0矩阵b
b =  np.zeros_like(a)
# np.linspace函数
a =  np.linspace( 0, 2, 9 )                 # 9 numbers from 0 to 2
>>> u = np.eye(2) # unit 2x2 matrix; "eye" represents "I"
>>> u
array([[ 1.,  0.],
       [ 0.,  1.]])
# 服从“0~1”均匀分布的随机样本值。随机样本取值范围是[0,1),不包括1
a =  np.random.rand(3,2)
array([[ 0.14022471,  0.96360618],  #random
       [ 0.37601032,  0.25528411],  #random
       [ 0.49313049,  0.94909878]]) #random
# 返回服从正态分布X ~ N(3, 6.25)  来源于 (x-mu)/sigma ~ N(0,1)
# np.random.randn(2, 4)返回标准正态分布
a =  6.25 * np.random.randn(2, 4) + 3
# 返回随机的整数,位于半开区间 [low, high) 
a =   np.random.randint(5, size=(2, 4)) #区间[0, 5) 
a =   np.random.randint(3,5, size=(2, 4)) #区间[3, 5) 
# ranf = random = sample = random_sample 返回在半开区间 [0.0, 1.0)
np.random.random((3, 2))
python展开代码np.random.shuffle(arr) #洗牌
python展开代码a.ravel():# 引用  返回的是视图  (3,)
         # 修改引用 就是修改原数据
a.flatten():# 深拷贝  返回副本  (3,)
a.squeeze():# 删除一条维度  (3,)或者(3, 1)
a.reshape((3,4))
np.vstack() #在竖直方向上堆叠
np.hstack() #在水平方向上平铺
python展开代码>>> a = np.array((1,2,3))
>>> b = np.array((2,3,4))
>>> np.hstack((a,b))
array([1, 2, 3, 2, 3, 4])
>>> a = np.array([[1],[2],[3]])
>>> b = np.array([[2],[3],[4]])
>>> np.hstack((a,b))
array([[1, 2],
       [2, 3],
       [3, 4]])
**np.concatenate **沿着现有的轴连接一系列数组。
python展开代码>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> np.concatenate((a, b.T), axis=1)
array([[1, 2, 5],
       [3, 4, 6]])
>>> np.concatenate((a, b), axis=None)
array([1, 2, 3, 4, 5, 6])
numpy.array_split(ary, indices_or_sections, axis=0) 拆分,不均等就报错**
python展开代码>>> x = np.arange(8.0)
>>> np.array_split(x, 3)
    [array([ 0.,  1.,  2.]), array([ 3.,  4.,  5.]), array([ 6.,  7.])]
numpy.split(ary, indices_or_sections, axis=0) 拆分, 可接受列表
python展开代码>>> x = np.arange(9.0)
>>> np.split(x, 3)
[array([ 0.,  1.,  2.]), array([ 3.,  4.,  5.]), array([ 6.,  7.,  8.])]
>>> x = np.arange(8.0)
>>> np.split(x, [3, 5, 6, 10])
[array([ 0.,  1.,  2.]),
 array([ 3.,  4.]),
 array([ 5.]),
 array([ 6.,  7.]),
 array([], dtype=float64)]
7.1 矩阵赋值技巧
代码
展开代码sequences=[[1,2],[8,2]] import numpy as np results = np.zeros((len(sequences), 10)) for i, sequence in enumerate(sequences): results[i, sequence] = 1. print(i,sequence) print(results)
结果
0 [1, 2]
1 [8, 2]
[[0. 1. 1. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0. 0. 0. 1. 0.]]
要点
矩阵results的某一行的某些索引进行赋值。
results[0,[1,2,5]]=1 表示把第0的第1、2、5处的值换成1


本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!