2015-05-14 31 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))