np.linspace,np.newaxis

>>> import numpy as np

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

在指定的间隔内返回均匀间隔的数字。在[start, stop]这个区间的端点可以任意的被排除在外,默认包含端点;retstep=True时,显示间隔长度。

>>> np.linspace(2.0,3.0,num=5)
array([2.  , 2.25, 2.5 , 2.75, 3.  ])
>>> np.linspace(2.0,3.0,num=5,endpoint=False)
array([2. , 2.2, 2.4, 2.6, 2.8])
>>> np.linspace(2.0,3.0,num=5,retstep=True)
(array([2.  , 2.25, 2.5 , 2.75, 3.  ]), 0.25)

numpy.newaxis None的方便别名,对于索引数组是有用的。

>>> np.newaxis is None
True
>>> x = np.array([1,2,3]) # 一维数组[1,2,3]
>>> x
array([1, 2, 3])
>>> x.shape
(3,)
>>> x[:,np.newaxis]# 二维数组
array([[1],
       [2],
       [3]])
>>> x[:,np.newaxis].shape
(3, 1)
>>> x[np.newaxis,:]# 二维数组
array([[1, 2, 3]])
>>> x[np.newaxis,:].shape
(1, 3)
>>> x[:,np.newaxis,np.newaxis]# 三维数组
array([[[1]],

       [[2]],

       [[3]]])
>>> x[:,np.newaxis,np.newaxis].shape
(3, 1, 1)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值