numpy.power(x1, x2)
数组的元素分别求n次方。x2可以是数字,也可以是数组,但是x1和x2的列数要相同。
x1 = range(6)
>>> x1
[0, 1, 2, 3, 4, 5]
>>> np.power(x1, 3)
array([ 0, 1, 8, 27, 64, 125])
>>> x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]
>>> np.power(x1, x2)
array([ 0., 1., 8., 27., 16., 5.])
>>> x2 = np.array([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]])
>>> x2
array([[1, 2, 3, 3, 2, 1],
[1, 2, 3, 3, 2, 1]])
>>> np.power(x1, x2)
array([[ 0, 1, 8, 27, 16, 5],
[ 0, 1, 8, 27, 16, 5]])
np.newaxis,增加维度
np.linspace(1, 10, 10)
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
np.linspace(1, 10, 10)[np.newaxis,:]
array([[ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]])
np.linspace(1, 10, 10)[:,np.newaxis]
array
([[ 1.],
[ 2.],
[ 3.],
[ 4.],
[ 5.],
[ 6.],
[ 7.],
[ 8.],
[ 9.],
[ 10.]])
In [4]: np.linspace(1, 10, 10).shape
Out[4]: (10,)%数组
In [5]: np.linspace(1, 10, 10)[np.newaxis,:].shape
Out[5]: (1, 10)%矩阵
In [6]: np.linspace(1, 10, 10)[:,np.newaxis].shape
Out[6]: (10, 1)