numpy.newaxis

从字面上是插入新的维度的意思

import  numpy as np

b = np.array([1, 2, 3, 4, 5, 6])
print("b.shape:",b.shape)
print("b[np.newaxis]:",b[np.newaxis])
print("b[:,np.newaxis]:",b[:,np.newaxis])
print("b[:,np.newaxis].shape:",b[:,np.newaxis].shape)

x_data = np.linspace(-0.5,0.5,9)
print("x_data[:,np.newaxis]:",x_data[:,np.newaxis])

print("x_data.shape:",x_data.shape)
noise = np.random.normal(0,0.02,(9,1))
print("==========")
print('x_data:',x_data)
print("noise:",noise)
b.shape: (6,)
b[np.newaxis]: [[1 2 3 4 5 6]]
b[:,np.newaxis]: [[1]
 [2]
 [3]
 [4]
 [5]
 [6]]
b[:,np.newaxis].shape: (6, 1)
x_data[:,np.newaxis]: [[-0.5  ]
 [-0.375]
 [-0.25 ]
 [-0.125]
 [ 0.   ]
 [ 0.125]
 [ 0.25 ]
 [ 0.375]
 [ 0.5  ]]
x_data.shape: (9,)
==========
x_data: [-0.5   -0.375 -0.25  -0.125  0.     0.125  0.25   0.375  0.5  ]
noise: [[ 0.00937875]
 [-0.01113971]
 [-0.00603901]
 [ 0.01048417]
 [-0.01808899]
 [-0.00172619]
 [ 0.03479479]
 [-0.02663055]
 [ 0.00474127]]

Process finished with exit code 0

看一下转置的时候有什么区别

print(np.transpose(b))
print(np.transpose(b[np.newaxis]))
[1 2 3 4 5 6]
[[1]
 [2]
 [3]
 [4]
 [5]
 [6]]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值