暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Numpy数组形状调整相关函数总结

漫谈大数据与数据分析 2020-04-27
664

数组形状
In [1]: %pylab
Using matplotlib backend: TkAgg
Populating the interactive namespace from numpy and matplotlib

修改数组的形状

In [2]:
a = arange(6)
a
Out[2]:
array([0, 1, 2, 3, 4, 5])

将形状修改为2乘3:

In [3]:
a.shape = 2,3
a


Out[3]:
array([[0, 1, 2],
[3, 4, 5]])

与之对应的方法是 reshape
 ,但它不会修改原来数组的值,而是返回一个新的数组:

In [4]:
a.reshape(3,2)
Out[4]:
array([[0, 1],
[2, 3],
[4, 5]])




In [5]:a
Out[5]:array([[0, 1, 2],
[3, 4, 5]])

shape
 和 reshape
 方法不能改变数组中元素的总数,否则会报错:

例如a.reshape(4,2)会报出:

ValueError: cannot reshape array of size 3 into shape (4,2)


使用 newaxis 增加数组维数

In [7]:
a = arange(3)
shape(a)
Out[7]: (3,)
In [8]:
y = a[newaxis, :]
shape(y)
Out[8]: (1, 3)

根据插入位置的不同,可以返回不同形状的数组:

In [9]:
y = a[:, newaxis]
shape(y)
Out[9]:
(3, 1)

插入多个新维度:

In [10]:
y = a[newaxis, newaxis, :]
shape(y)
Out[10]:(1, 1, 3)


squeeze 方法去除多余的轴

In [11]:
a = arange(6)
a.shape = (2,1,3)
In [12]:
b = a.squeeze()
b.shape
Out[12]:
(2, 3)

squeeze 返回一个将所有长度为1的维度去除的新数组。


数组转置

使用 transpose
 返回数组的转置,本质上是将所有维度反过来:

In [13]: a
Out[13]:
array([[[0, 1, 2]],
[[3, 4, 5]]])

对于二维数组,这相当于交换行和列:

In [14]: a.transpose()
Out[14]:
array([[[0, 3]],
[[1, 4]],
[[2, 5]]])

或者使用缩写属性:

In [15]: a.T
Out[15]:
array([[[0, 3]],
[[1, 4]],
[[2, 5]]])

注意:

  • 对于复数数组,转置并不返回复共轭,只是单纯的交换轴的位置

  • 转置可以作用于多维数组

In [16]:
a = arange(60)
a
Out[16]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59])


In [17]:
a.shape = 3,4,5
a
Out[17]:
array([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],
[[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39]],
[[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59]]])


In [18]:
b = a.T
b.shape
Out[18]:
(5, 4, 3)

转置只是交换了轴的位置。

另一方面,转置返回的是对原数组的另一种view,所以改变转置会改变原来数组的值。

In [19]:
a = arange(6)
a.shape = (2,3)
a
Out[19]:
array([[0, 1, 2],
[3, 4, 5]])

修改转置:

In [20]:
b = a.T
b[0,1] = 30

原数组的值也改变:

In [21]: a
Out[21]:
array([[ 0, 1, 2],
[30, 4, 5]])


数组连接

有时我们需要将不同的数组按照一定的顺序连接起来:

concatenate((a0,a1,...,aN), axis=0)

注意,这些数组要用 ()
 包括到一个元组中去。

除了给定的轴外,这些数组其他轴的长度必须是一样的。

In [22]:
x = array([
[0,1,2],
[10,11,12]
])
y = array([
[50,51,52],
[60,61,62]
])
x.shape
Out[22]:
(2, 3)


In [23]:
y.shape
Out[23]:
(2, 3)

默认沿着第一维进行连接:

In [24]:
z = concatenate((x,y))
z
Out[24]:
array([[ 0, 1, 2],
[10, 11, 12],
[50, 51, 52],
[60, 61, 62]])


In [25]: z.shape
Out[25]: (4, 3)

沿着第二维进行连接:

In [26]:
z = concatenate((x,y), axis=1)
z
Out[26]:
array([[ 0, 1, 2, 50, 51, 52],
[10, 11, 12, 60, 61, 62]])


In [27]: z.shape
Out[27]: (2, 6)

注意到这里 x
 和 y
 的形状是一样的,还可以将它们连接成三维的数组,但是 concatenate
 不能提供这样的功能,不过可以这样:

In [28]: z = array((x,y))
In [29]: z.shape
Out[29]: (2, 2, 3)

事实上,Numpy提供了分别对应这三种情况的函数:

  • vstack

  • hstack

  • dstack

In [30]: vstack((x, y)).shape
Out[30]: (4, 3)
In [31]: hstack((x, y)).shape
Out[31]: (2, 6)
In [32]: dstack((x, y)).shape
Out[32]: (2, 3, 2)


Flatten 数组

flatten
 方法的作用是将多维数组转化为1维数组:

In [33]:
a = array([[0,1],
[2,3]])
b = a.flatten()
b
Out[33]:
array([0, 1, 2, 3])

返回的是数组的复制,因此,改变 b
 并不会影响 a
 的值:

In [34]:
b[0] = 10
b
Out[34]:
array([10, 1, 2, 3])


In [35]:
a
Out[35]:
array([[0, 1],
[2, 3]])


flat 属性

还可以使用数组自带的 flat
 属性:

In [36]:
a.flat
Out[36]:
<numpy.flatiter at 0xc9e4d40>

a.flat
 相当于返回了所有元组组成的一个迭代器:

In [37]:
b = a.flat
b[0]
Out[37]:
0

但此时修改 b
 的值会影响 a
 :

In [38]:
b[0] = 10
a
Out[38]:
array([[10, 1],
[ 2, 3]])

In [39]: a.flat[:]
Out[39]: array([10, 1, 2, 3])


ravel 方法

除此之外,还可以使用 ravel
 方法,ravel
 使用高效的表示方式:

In [40]:
a = array([[0,1],
[2,3]])
b = a.ravel()
b
Out[40]:
array([0, 1, 2, 3])

修改 b
 会改变 a
 :

In [41]:
b[0] = 10
a
Out[41]:
array([[10, 1],
[ 2, 3]])

但另一种情况下:

In [42]:
a = array([[0,1],
[2,3]])
aa = a.transpose()
b = aa.ravel()
b
Out[42]:
array([0, 2, 1, 3])


In [43]: b[0] = 10
In [44]: aa
Out[44]:
array([[0, 2],
[1, 3]])

In [45]: a
Out[45]:
array([[0, 1],
[2, 3]])

可以看到,在这种情况下,修改 b
 并不会改变 aa
 的值,原因是我们用来 ravel
 的对象 aa
 本身是 a
 的一个view。


atleast_xd 函数

保证数组至少有 x
 维:

In [46]:
x = 1
atleast_1d(x)
Out[46]: array([1])


In [47]:
a = array([1,2,3])
b = atleast_2d(a)
b.shape
Out[47]: (1, 3)


In [48]: b
Out[48]: array([[1, 2, 3]])


In [49]: c = atleast_3d(b)
In [50]: c.shape
Out[50]: (1, 3, 1)

x
 可以取值 1,2,3。

Scipy库中,这些函数被用来保证输入满足一定的条件:“

用法Scipy中出现次数
value.flaten()
value.flat
value.ravel()
~2000次
atleast_1d(value)
atleast_2d(value)
~700次
asarray(value)~4000次


 


文章转载自漫谈大数据与数据分析,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论