2015-05-14 32 views
1

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

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个阵列。 感谢您的帮助。

回答

0

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

它可能需要您的阵列首先flatten然后到reshape它。

+0

我使用这下面的代码: X =数据。形状[0] \t Y = data_sys.shape [0] \t如果X zero_array = np.zeros(diff,16) TypeError:数据类型不理解“ – user3285014

+0

np.zeros(diff,16)似乎很奇怪。差异是差异,我已经理解了很多。但什么是'16'代表。该函数需要一个dtype作为第二个参数。这是一个numpy.intxx(xx = 8,32,64,...)或一个浮点型参数。 – RaJa

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他们水平叠加。

+0

我用vstack和hstack作为AniaG建议匹配两个不同大小的矩阵。这些是在二维图像上掩盖小区域的结果,并且两者共享一个共同中心。我简单地执行了vstack和hstack两次,以​​便在之后添加缺少的值。 – Hugo

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" 
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 
0

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

mask_in = np.random.randint(2, size = (2, 8)) 
mask_ot = np.random.randint(2, size = (6, 16)) 
mask_in_amp = mask_in 

dif_row = mask_ot.shape[0]-mask_in_amp.shape[0] 
dif_col = mask_ot.shape[1]-mask_in_amp.shape[1] 

complete_row = dif_row/2 
complete_col = dif_col/2 

mask_in_amp = np.vstack((mask_in_amp, np.zeros((complete_row, mask_in_amp.shape[1])))) 
mask_in_amp = np.vstack((np.zeros((complete_row, mask_in_amp.data.shape[1])), mask_in_amp)) 

mask_in_amp = np.hstack((mask_in_amp, np.zeros((mask_in_amp.shape[0],complete_col)))) 
mask_in_amp = np.hstack((np.zeros((mask_in_amp.shape[0],complete_col)), mask_in_amp))