Numpy比较2个数组形状,如果不同,追加0以匹配形状

时间:2015-05-14 18:06:35

标签: python arrays numpy

我正在比较2个numpy数组,并希望将它们加在一起。但是,在这样做之前,我需要确保它们的大小相同。如果尺寸不相同,则采用较小尺寸的尺寸,并用零填充最后一行以匹配形状。 两个阵列都有16列和N行。我认为它应该非常直接,但我无法理解它。到目前为止,我能够比较2阵列形状。

import csv
import numpy as np
import sys
data = np.genfromtxt('./test1.csv', dtype=float, delimiter=',')
data_sys = np.genfromtxt('./test2.csv', dtype=float, delimiter=',')   
print data.shape
print data_sys.shape
if data.shape != data_sys.shape:
        print "we have an error"

这是我得到的输出:

=============New file.csv============
(603, 16)
(604, 16)
we have an error

我希望填写最后一行"数据"数组为0,以便我可以添加2个数组。 谢谢你的帮助。

6 个答案:

答案 0 :(得分:2)

您可以使用numpy中的vstack(array1, array2)来垂直堆叠数组。例如:

A = np.random.randint(2, size = (2, 16))
B = np.random.randint(2, size = (5, 16))

print A.shape
print B.shape
if A.shape[0] < B.shape[0]:
    A = np.vstack((A, np.zeros((B.shape[0] - A.shape[0], 16))))
elif A.shape[0] > B.shape[0]:
    B = np.vstack((B, np.zeros((A.shape[0] - B.shape[0], 16)))) 

print A.shape   
print A

在你的情况下:

if data.shape[0] < data_sys.shape[0]:
    data = np.vstack((data, np.zeros((data_sys.shape[0] - data.shape[0], 16))))
elif data.shape[0] > data_sys.shape[0]:
    data_sys = np.vstack((data_sys, np.zeros((data.shape[0] - data_sys.shape[0], 16)))) 

我假设您的矩阵总是具有相同的列数,否则您可以类似地使用hstack水平堆叠它们。

答案 1 :(得分:2)

如果你只有两个文件,并且它们的形状在第0维度上有所不同,那么简单的检查和复制可能是最简单的,尽管它缺乏通用性:

import numpy as np

data = np.genfromtxt('./test1.csv', dtype=float, delimiter=',')
data_sys = np.genfromtxt('./test2.csv', dtype=float, delimiter=',')   

fill_value = 0 # could be np.nan or something else instead

if data.shape[0]>data_sys.shape[0]:
    temp = data_sys
    data_sys = np.ones(data.shape)*fill_value
    data_sys[:temp.shape[0],:] = temp
elif data.shape[0]<data_sys.shape[0]:
    temp = data
    data = np.ones(data_sys.shape)*fill_value
    data[:temp.shape[0],:] = temp

print 'Using conditional:'
print data.shape
print data_sys.shape
if data.shape != data_sys.shape:
        print "we have an error"

更通用的解决方案是自定义类 - 对于两个文件来说是过度杀伤,但如果要处理大量文件则要容易得多。基本思想是静态类变量sxsy跟踪最大宽度和高度,并在调用get_data时使用,以输出标准形状数组。这预填充了您想要的填充值,相应文件中的实际数据被复制到标准形状数组的左上角:

import numpy as np

class IsomorphicArray:

    sy = 0 # static class variable
    sx = 0 # static class variable
    fill_value = 0.0

    def __init__(self,csv_filename):
        self.data = np.genfromtxt(csv_filename,dtype=float,delimiter=',')
        self.instance_sy,self.instance_sx = self.data.shape
        if self.instance_sy>IsomorphicArray.sy:
            IsomorphicArray.sy = self.instance_sy
        if self.instance_sx>IsomorphicArray.sx:
            IsomorphicArray.sx = self.instance_sx

    def get_data(self):
        out = np.ones((IsomorphicArray.sy,IsomorphicArray.sx))*self.fill_value
        out[:self.instance_sy,:self.instance_sx] = self.data
        return out

isomorphic_array_list = []

for filename in ['./test1.csv','./test2.csv']:
    isomorphic_array_list.append(IsomorphicArray(filename))

numpy_array_list = []

for isomorphic_array in isomorphic_array_list:
    numpy_array_list.append(isomorphic_array.get_data())


print 'Using custom class:'
for numpy_array in numpy_array_list:
    print numpy_array.shape

答案 2 :(得分:1)

假设两个阵列都有16列

len1=len(data)
len2=len(data_sys)
if len1<len2:
  data=np.append(data, np.zeros((len2-len1, 16)),axis=0)
elif len2<len1:
  data_sys=np.append(data_sys, np.zeros((len1-len2, 16)),axis=0)
print data.shape
print data_sys.shape
if data.shape != data_sys.shape:
  print "we have an error"
else:
  print "we r good"

答案 3 :(得分:0)

Numpy提供了一个追加函数来向数组添加值:see here for details。在多维数组中,您可以定义应如何添加值。由于您已经知道哪些数组是较小的数据,只需添加所需数量的零,然后通过numpy.zeroes创建零填充数组,然后将其附加到目标数组。

可能需要首先flatten您的数组然后再reshape

答案 4 :(得分:0)

我有类似的情况。两个大小的数组mask_in:(n1,m1)和mask_ot:(n2,m2)是通过尺寸为(N,M)的2D图像的掩模生成的,其中A2大于A1并且两者共享一个公共中心(X0) ,Y0)。我按照@AniaG使用vstack和hstack建议的方法。我只是获得了两个数组的形状,大小差异,最后考虑了两端缺失元素的数量。 这是我得到的:

[HttpPatch]
    public Task<CollectionUpdate> RenameCollection([FromBody]CollectionUpdate update)
    {
        return Task.FromResult<CollectionUpdate>(new CollectionUpdate());
    }

答案 5 :(得分:0)

如果您不关心两个阵列的完全形状,您还可以执行以下操作:

if data.size == datasys.size:
    print ('arrays have the same number of elements, and possibly shape')
else:
    print ('arrays do not have the same shape for sure')