Numpy函数使用笔记

1、np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

     函数说明:设定起始值与结束值以及数量值生成等间隔的numpy一维数组

In [3]: np.linspace(start=1, stop=5, num=4)
Out[3]: array([1.        , 2.33333333, 3.66666667, 5.        ])

In [4]: np.linspace(start=1, stop=5, num=4, endpoint=False)
Out[4]: array([1., 2., 3., 4.])

2、np.expand_dims(a, axis)

     函数说明:输入numpy数组与指定轴线,对指定的轴线进行维度扩充

     注:可指定的维度axis比输入的数组多一维,如输入的数组为二维矩阵,既有维度为2(0,1),则可指定的轴线为3(0,1,2)

In [11]: m = np.array([[1,1], [2,2]])
In [12]: print(m,  m.shape)
[[1 1]
 [2 2]] (2, 2)

# axis=0
In [13]: m_0 = np.expand_dims(m, axis=0)
In [14]: print(m_0, m_0.shape)
[[[1 1]
  [2 2]]] (1, 2, 2)

# axis=1
In [15]: m_1 = np.expand_dims(m, axis=1)
In [16]: print(m_1,  m_1.shape)
[[[1 1]]
 [[2 2]]] (2, 1, 2)

# axis=2
In [17]: m_2 = np.expand_dims(m, axis=2)

In [18]: print(m_2,  m_2.shape)
[[[1]
  [1]]
 [[2]
  [2]]] (2, 2, 1)

3、np.transpose(a, axes=None)

     函数说明:输入numpy多维数组与新的轴线顺序元组,生成新的轴线下的numpy数组

     当输入的数组为二维数组与.T等效,且可以省略axes参数

In [14]: m = np.array([[1,1], [2,2]])
In [15]: m
Out[15]: 
array([[1, 1],
       [2, 2]])

In [16]: np.transpose(m)
Out[16]: 
array([[1, 2],
       [1, 2]])

In [17]: np.transpose(m, (1, 0))
Out[17]: 
array([[1, 2],
       [1, 2]])

In [18]: m.T
Out[18]: 
array([[1, 2],
       [1, 2]])

     当输入的数组维度大于二维,axes参数不可省略且需要指定所有维度

In [44]: m2 = np.array([[[1,2,3], [1,2,3]], [[4,5,6],[4,5,6]]])
In [45]: m2
Out[45]: 
array([[[1, 2, 3],
        [1, 2, 3]],
       [[4, 5, 6],
        [4, 5, 6]]])
In [46]: m2.shape
Out[46]: (2, 2, 3)

In [48]: m2_021 = np.transpose(m2, (0, 2, 1))
In [49]: m2_021
Out[49]:        
array([[[1, 1], 
        [2, 2], 
        [3, 3]],
       [[4, 4],
        [5, 5],
        [6, 6]]])
In [50]: m2_021.shape
Out[50]: (2, 3, 2)

In [51]: m2_210 = np.transpose(m2, (2, 1, 0))
In [52]: m2_210
Out[52]: 
array([[[1, 4],
        [1, 4]],
       [[2, 5],
        [2, 5]],
       [[3, 6],
        [3, 6]]])
In [53]: m2_210.shape
Out[53]: (3, 2, 2)

4、np.meshgrid(*xi, copy=True, sparse=False, indexing='xy')

     函数说明:输入多维不同轴线的坐标值向量,返回不同轴线全排列构建的多维坐标矩阵,其中返回的坐标矩阵,各个维度分开

     输入维度为两维(x,y),返回[X,Y]

In [1]: import numpy as np

In [2]: x = np.array([1,3,2,4])

In [3]: y = np.array([2,3,4,1,5])

In [4]: [X, Y] = np.meshgrid(x, y)

In [5]: print(X, "\n\n", Y)
[[1 3 2 4]
 [1 3 2 4]
 [1 3 2 4]
 [1 3 2 4]
 [1 3 2 4]]

 [[2 2 2 2]
 [3 3 3 3]
 [4 4 4 4]
 [1 1 1 1]
 [5 5 5 5]]

     输入维度为三维(x,y,z),返回[X,Y,Z]

In [8]: z = np.array([2,3,6])

In [9]: [X, Y, Z] = np.meshgrid(x, y, z)

In [10]: print(X, "\n\n", Y, "\n\n", Z)
[[[1 1 1]
  [3 3 3]
  [2 2 2]
  [4 4 4]]
 [[1 1 1]
  [3 3 3]
  [2 2 2]
  [4 4 4]]
 [[1 1 1]
  [3 3 3]
  [2 2 2]
  [4 4 4]]
 [[1 1 1]
  [3 3 3]
  [2 2 2]
  [4 4 4]]
 [[1 1 1]
  [3 3 3]
  [2 2 2]
  [4 4 4]]]

 [[[2 2 2]
  [2 2 2]
  [2 2 2]
  [2 2 2]]
 [[3 3 3]
  [3 3 3]
  [3 3 3]
  [3 3 3]]
 [[4 4 4]
  [4 4 4]
  [4 4 4]
  [4 4 4]]
 [[1 1 1]
  [1 1 1]
  [1 1 1]
  [1 1 1]]
 [[5 5 5]
  [5 5 5]
  [5 5 5]
  [5 5 5]]]

 [[[2 3 6]
  [2 3 6]
  [2 3 6]
  [2 3 6]]
 [[2 3 6]
  [2 3 6]
  [2 3 6]
  [2 3 6]]
 [[2 3 6]
  [2 3 6]
  [2 3 6]
  [2 3 6]]
 [[2 3 6]
  [2 3 6]
  [2 3 6]
  [2 3 6]]
 [[2 3 6]
  [2 3 6]
  [2 3 6]
  [2 3 6]]]

5、np.where(稍后补充)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值