美文网首页我爱编程
numpy的轴(axis)与维度(shape)问题

numpy的轴(axis)与维度(shape)问题

作者: 妙蛙种子123 | 来源:发表于2018-03-10 02:18 被阅读0次

对numpy维度有时不是太清楚,经常搞错,写几个例子看一遍就懂了

import numpy as np
a = np.arange(6).reshape([3,2])
print(a)
[[0 1]
 [2 3]
 [4 5]]

print(a.shape)
(3, 2)
(n,)数组与(n,1)数组的互转

reshape进行维度的转换

a = np.array([1,2,3])
print(a.shape)
(3,)

b = a.reshape(-1,1)
print(b.shape)
(3, 1)

把shape中为1的维度去掉

a = np.array([[1],[2],[3]])
print(a.shape)
(3, 1)

b = np.squeeze(a)
print(b.shape)
(3,)

添加新维度

a = np.array([1,2,3])
b = a[np.newaxis,:]
print(b)
[[1 2 3]]
print(b.shape)
(1, 3)

c = a[:,np.newaxis]
print(c)
[[1]
 [2]
 [3]]
print(c.shape)
(3, 1)
矩阵乘法

(n,)数组求内积

a = np.array([1,2,3])
print(a.dot(a))
14

(n,1)数组求内积

a = np.array([[1],[2],[3]])
print(a.dot(a))

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-be6ff70f8c84> in <module>()
      1 a = np.array([[1],[2],[3]])
----> 2 print(a.dot(a))

ValueError: shapes (3,1) and (3,1) not aligned: 1 (dim 1) != 3 (dim 0)
numpy的转置
>>> x = np.arange(4).reshape((2,2))
>>> x
array([[0, 1],
       [2, 3]])
>>> np.transpose(x)
array([[0, 2],
       [1, 3]])

>>> x = np.ones((1, 2, 3))
>>> np.transpose(x, (1, 0, 2)).shape
(2, 1, 3)
numpy数组展平
a = np.arange(24).reshape(2,3,4)
print(a.shape)
(2, 3, 4)
print(a.flatten().shape)
(24,)
print(a.flatten())
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
np.sum的axis
a = np.array([[1,2,3],[4,5,6]])

# 对所有数字求和
print(np.sum(a))
21

# 对列求和
print(np.sum(a,axis=0))
[5 7 9]

# 对行求和
print(np.sum(a,axis=1))
[ 6 15]

# 对最后一个维度求和
print(np.sum(a,axis=-1))
[ 6 15]
np.sum的keepdims
a = np.array([[1,2,3],[4,5,6]])
print(np.sum(a,axis=1))
[ 6 15]
print(np.sum(a,axis=1,keepdims=True))
[[ 6]
 [15]]
>>> x=np.array([[1001, 1002], [3, 4]])
>>> x -= np.max(x)
>>> x
array([[  -1,    0],
       [-999, -998]])


>>> x=np.array([[1001, 1002], [3, 4]])
>>> x -= np.max(x, axis=1)
>>> x
array([[  -1,  998],
       [-999,    0]])


>>> import numpy as np
>>> x=np.array([[1001, 1002], [3, 4]])
>>> x -= np.max(x, axis=1, keepdims=True)
>>> x
array([[-1,  0],
       [-1,  0]])

相关文章

  • numpy的轴(axis)与维度(shape)问题

    对numpy维度有时不是太清楚,经常搞错,写几个例子看一遍就懂了 (n,)数组与(n,1)数组的互转 reshap...

  • numpy

    import numpy as np轴(axis):保存数据的维度秩序(rank):轴的数量

  • python数据处理

    一、Numpy库入门 ndarray对象的属性.ndim 秩,即轴的数量或维度的数量.shape ndarray对...

  • ndarray数组相关

    维度称为轴(axis),轴的个数称为秩(rank)axis=0表示沿着第0轴操作,表示对每一列进行操作axis=1...

  • np.expand_dims函数

    即扩展维度,np.expand_dims(a,axis=)即在相应的axis轴上扩展维度a = np.array(...

  • tensorflow一些常用的api

    tf.shape(a) 获取张量a各个方向上的维度 tf.unstack(a,axis=) 将张量a根据axis从...

  • Numpy.argmax()

    numpy.argmax(a, axis=None, out=None) 返回沿轴axis最大值的索引。 Para...

  • NumPy的统计函数

    NumPy的统计函数(1) axis=0表示第一层的维度 axis=1表示第二层 NumPy的统计函数(2)

  • 【Python学习笔记】numpy初学笔记

    1. Numpy数组的创建 2. Numpy数组的属性 ndim : 数组的维度 shape : 数组每个维度的大...

  • 理解axis参数和dot函数

    理解axis看下面的例子 多维数组的轴(axis=)是和该数组的size(或者shape)的元素是相对应的; 对于...

网友评论

    本文标题:numpy的轴(axis)与维度(shape)问题

    本文链接:https://www.haomeiwen.com/subject/lfomfftx.html