从字面上是插入新的维度的意思
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]]