numpy数组堆叠,连接,转list
1.numpy 数组->list
- 直接用list()函数 # 转换成 第0维的每一个元素(Numpy.array) 组成的list
- 用array.tolist()函数 # 与原来的array的数据形式是一样的,只不过每一维度都是一个list
总之:tolist() 方法转换的更彻底,list()方法只转换了源数据的第一个维度.
如果np.array是一维,两者没有区别。但如果是二维结果是不同的。
>>> import numpy
>>> a1=numpy.random.rand(3)
>>>>> a1
array([0.26546725, 0.27424911, 0.18618962])
>>> list(a1)
[0.26546725247934855, 0.27424910895802035, 0.18618962270208705]
>>> a1.tolist()
[0.26546725247934855, 0.27424910895802035, 0.18618962270208705]
>>> a2
array([[0.25176667, 0.78656379, 0.17814966],
[0.38749959, 0.195838 , 0.91929009]])
>>> list(a2)
[array([0.25176667, 0.78656379, 0.17814966]), array([0.38749959, 0.195838 , 0.91929009])]
>>> a2.tolist()
[[0.25176666870818276, 0.7865637882478266, 0.17814966253473885], [0.3874995863899837, 0.19583799515418743, 0.9192900894591074]]
>>>
参考博文:https://www.cnblogs.com/wxiaoli/p/9550382.html
2.拼接多个格式相同的numpy.array->list
>>> a1=numpy.random.randn(2,3)
>>> a2=numpy.random.randn(2,3)
>>> a3=numpy.random.randn(2,3)
>>> a=[]
>>> a.append(a1)
>>> a.append(a2)
>>> a.append(a3)
>>> numpy.concatenate(a,0)
array([[ 0.57881339, -0.4272588 , 1.75494623],
[ 0.12732501, 0.26448999, 0.65867156],
[-1.39516973, 1.07828099, 0.1951344 ],
[ 1.65719601, -0.50482074, 0.64750346],
[-0.32106765, 0.59423275, -0.87997188],
[ 0.72571895, -1.03586002, -0.74946671]])
>>> b=numpy.concatenate(a,0)
>>> list(b)
[array([ 0.57881339, -0.4272588 , 1.75494623]), array([0.12732501, 0.26448999, 0.65867156]), array([-1.39516973, 1.07828099, 0.1951344 ]), array([ 1.65719601, -0.50482074, 0.64750346]), array([-0.32106765, 0.59423275, -0.87997188]), array([ 0.72571895, -1.03586002, -0.74946671])]
3.np.vstack(),np.hstack()
np.vstack()沿着第0维度堆叠,np.hstack()沿着第维度堆叠
只有两个维度:
np.vstack(tuple)垂直方向堆叠成numpy.array
np.hstack(tuple)水平方向堆叠成numpy.array
注意:
tuple=(a1,a2,a3,…an)
a1,a2,a3,…an除了堆叠的那个维度,剩余的维度尺寸要完全一致.
>>> a1=numpy.random.randn(2,2)
>>> a2=numpy.random.randn(3,2)
>>> a=numpy.vstack((a1,a2))
>>> a
array([[ 0.67667278, -0.3318424 ],
[-0.2550355 , -0.74132559],
[ 0.43534239, 1.46399303],
[-0.86049107, 2.03871322],
[-0.01824614, 0.46310639]])
>>> b1=numpy.random.randn(2,3)
>>> b2=numpy.random.randn(2,1)
>>> b=numpy.hstack((b1,b2))
>>> b
array([[-0.69216195, 0.43455353, -0.5628851 , 1.98854944],
[ 1.73648473, 1.11249471, -0.8067703 , -0.53433626]])
>>> import numpy
>>> a1=numpy.random.randn(2,2,2)
>>> a2=numpy.random.randn(2,2,2)
>>> numpy.vstack((a1,a2))
array([[[ 0.06585097, -0.80433501],
[ 1.77412345, -0.5875084 ]],
[[ 1.0323896 , -1.58661562],
[-0.54994428, -0.29831369]],
[[ 1.25667368, 0.51978405],
[-1.01008035, 1.31148134]],
[[-0.05051251, 0.26415813],
[-1.12383927, 0.80728397]]])
>>> a=numpy.vstack((a1,a2))
>>> a[0]
array([[ 0.06585097, -0.80433501],
[ 1.77412345, -0.5875084 ]])
>>> b=numpy.hstack((a1,a2))
>>> b
array([[[ 0.06585097, -0.80433501],
[ 1.77412345, -0.5875084 ],
[ 1.25667368, 0.51978405],
[-1.01008035, 1.31148134]],
[[ 1.0323896 , -1.58661562],
[-0.54994428, -0.29831369],
[-0.05051251, 0.26415813],
[-1.12383927, 0.80728397]]])
>>> b.size()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> b.size
16
>>> b.shape
(2, 4, 2)
>>> a.shape
(4, 2, 2)
参考博文:https://blog.csdn.net/nanhuaibeian/article/details/100597342
3.numpy.stack()
stack() 函数是vstack(),与hstack()结合升级.
Parameters:
arrays : sequence of array_like
Each array must have the same shape.
axis : int, optional
The axis in the result array along which the input arrays are stacked.
out : ndarray, optional
If provided, the destination to place the result. The shape must be correct, matching that of what stack would have returned if no out argument were specified.
Returns:
stacked : ndarray
The stacked array has one more dimension than the input arrays.
np.stack(tuple, axis=0) 等价于 np.vstack(tuple)
np.stack(tuple, axis=1) 等价于 np.hstack(tuple)
参考博文:https://blog.csdn.net/u013019431/article/details/79768219
来源:CSDN
作者:sinat_40624829
链接:https://blog.csdn.net/sinat_40624829/article/details/103601901