【numpy】维度相关np.newaxis()、ravel()、flatten()、squeeze()

目录

ravel()、flatten()、squeeze() 降维

numpy.ravel() vs numpy.flatten() 

np.squeeeze()

reshape(-1)

np.newaxis() 升维


ravel()、flatten()、squeeze() 降维

numpy中的ravel()、flatten()、squeeze()都有将多维数组转换为一维数组的功能,

区别:

  • ravel():如果没有必要,不会产生源数据的副本
  • flatten():返回源数据的副本
  • squeeze():只能对维数为1的维度降维

另外,reshape(-1)也可以“拉平”多维数组


"""
拉平
"""
arr = np.arange(12).reshape(3,4)
print(arr)
"""
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
"""
print(arr.ravel()) # [ 0  1  2  3  4  5  6  7  8  9 10 11]
print(arr.flatten()) # [ 0  1  2  3  4  5  6  7  8  9 10 11]
print(arr.reshape(-1)) # [ 0  1  2  3  4  5  6  7  8  9 10 11]

arr1 = np.arange(3).reshape(3,1)
print(arr1)
"""
[[0]
 [1]
 [2]]
"""
print(np.squeeze(arr1)) # [0 1 2]

numpy.ravel() vs numpy.flatten() 

首先声明两者所要实现的功能是一致的(将多维数组降位一维),两者的区别在于返回拷贝(copy)还是返回视图(view),numpy.flatten()返回一份拷贝,对拷贝所做的修改不会影响(reflects)原始矩阵,而numpy.ravel()返回的是视图(view,也颇有几分C/C++引用reference的意味),会影响(reflects)原始矩阵。

  • 两者的功能

>>> x = np.array([[1, 2], [3, 4]])
>>> x
array([[1, 2],
       [3, 4]])
>>> x.flatten()
array([1, 2, 3, 4])
>>> x.ravel()
array([1, 2, 3, 4])
                    两者默认均是行序优先
>>> x.flatten('F')
array([1, 3, 2, 4])
>>> x.ravel('F')
array([1, 3, 2, 4])

>>> x.reshape(-1)
array([1, 2, 3, 4])
>>> x.T.reshape(-1)
array([1, 3, 2, 4])
  •  两者的区别

>>> x = np.array([[1, 2], [3, 4]])
>>> x.flatten()[1] = 100
>>> x
array([[1, 2],
       [3, 4]])            # flatten:返回的是拷贝
>>> x.ravel()[1] = 100
>>> x
array([[  1, 100],
       [  3,   4]])

np.squeeeze()

squeeze 函数:从数组的形状中删除单维度条目,即把shape中为1的维度去掉

用法:numpy.squeeze(a,axis = None)

1)a表示输入的数组;
2)axis用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;
3)axis的取值可为None 或 int 或 tuple of ints, 可选。若axis为空,则删除所有单维度的条目;
4)返回值:数组
5) 不会修改原数组;

import numpy as np

x = np.array([[[0], [1], [2]]])
print(x)
"""
x=

[[[0]
  [1]
  [2]]]
"""
print(x.shape)  # (1, 3, 1)

x1 = np.squeeze(x)  # 从数组的形状中删除单维条目,即把shape中为1的维度去掉


print(x1)  # [0 1 2]
print(x1.shape)  # (3,)

reshape(-1)

数组新的shape属性应该要与原来的配套,如果等于-1的话,那么Numpy会根据剩下的维度计算出数组的另外一个shape属性值。

举几个例子或许就清楚了,有一个数组z,它的shape属性是(4, 4)

z = np.array([[1, 2, 3, 4],
          [5, 6, 7, 8],
          [9, 10, 11, 12],
          [13, 14, 15, 16]])
z.shape
(4, 4)

z.reshape(-1) 

z.reshape(-1)
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])

z.reshape(-1, 1)

也就是说,先前我们不知道z的shape属性是多少,但是想让z变成只有1列行数不知道多少,通过`z.reshape(-1,1)`,Numpy自动计算出有16行,新的数组shape属性为(16, 1),与原来的(4, 4)配套。

z.reshape(-1,1)
 array([[ 1],
        [ 2],
        [ 3],
        [ 4],
        [ 5],
        [ 6],
        [ 7],
        [ 8],
        [ 9],
        [10],
        [11],
        [12],
        [13],
        [14],
        [15],
        [16]])

z.reshape(-1, 2)

newshape等于-1,列数等于2,行数未知,reshape后的shape等于(8, 2)

 z.reshape(-1, 2)
 array([[ 1,  2],
        [ 3,  4],
        [ 5,  6],
        [ 7,  8],
        [ 9, 10],
        [11, 12],
        [13, 14],
        [15, 16]])

同理,只给定行数,newshape等于-1,Numpy也可以自动计算出新数组的列数。

Python中reshape函数参数-1的意思? - 伊豆的回答 - 知乎

在用tensorflow做卷积神经网络入门的手写数字识别时。
源码:x_image = tf.reshape(x, [-1, 28, 28, 1])
这里是将一组图像矩阵x重建为新的矩阵,该新矩阵的维数为(a,28,28,1),其中-1表示a由实际情况来定。例如,x是一组图像的矩阵(假设是50张,大小为56×56),则执行
x_image = tf.reshape(x, [-1, 28, 28, 1])
可以计算a=(50×56×56)/28/28/1=200。即x_image的维数为(200,28,28,1)

np.newaxis() 升维

np.newaxis的作用就是在这一位置增加一个一维,这一位置指的是np.newaxis所在的位置,比较抽象,需要配合例子理解。

x1 = np.array([1, 2, 3, 4, 5])
# the shape of x1 is (5,)
x1_new = x1[:, np.newaxis]
# now, the shape of x1_new is (5, 1)
# array([[1],
#        [2],
#        [3],
#        [4],
#        [5]])
x1_new = x1[np.newaxis,:]
# now, the shape of x1_new is (1, 5)
# array([[1, 2, 3, 4, 5]])
In [124]: arr = np.arange(5*5).reshape(5,5)

In [125]: arr.shape
Out[125]: (5, 5)

# promoting 2D array to a 5D array
In [126]: arr_5D = arr[np.newaxis, ..., np.newaxis, np.newaxis]

In [127]: arr_5D.shape
Out[127]: (1, 5, 5, 1, 1)

参考:

numpy中的ravel()、flatten()、squeeze()的用法与区别

np.newaxis

numpy 辨异 (五)—— numpy.ravel() vs numpy.flatten()

numpy中的reshape中参数为-1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值