python - Numpy compare 2 array shape, if different, append 0 to match shape -


i comparing 2 numpy arrays, , want add them together. but, before doing so, need make sure same size. if size not same, take smaller sized 1 , fill last rows 0 match shape. both array have 16 columns , n rows. assuming should pretty straight forward, can't head around it. far able compare 2 array shape.

import csv import numpy 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 error" 

this output got:

=============new file.csv============ (603, 16) (604, 16) have error 

i want fill last row of "data" array 0 can add 2 arrays. help.

you can use vstack(array1, array2) numpy stacks arrays vertically. example:

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]:     = 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 

in case:

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))))  

i assume matrices have same number of columns, if not can use hstack stack them horizontally.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -