numpy - Selection of second columns data based on match of first column with another text file in python -
i have little knowledge of numpy arrays , iterations. have 2 input files. first columns of both files represents time in milliseconds. input file 1 reference or simulated value. input file 2 obtained test value. want compare(plot second vs first ) second column of input-2 file second column of first file if , if when there match of time in first column in corresponding files. trying through iterations not find proper results yet.how find index when there match?
import numpy np my_file=np.genfromtxt('path/input1.txt') sim_val=np.genfromtxt('path/input2.txt') inp1=my_file[:,0] inp12=my_file[:,1] inpt2=sim_val[:,0] inpt21=sim_val[:,1] xarray=np.array(inp1) yarray=np.array(inp12) data=np.array([xarray,yarray]) ldata=data.t zarray=np.array(inpt2) tarray=np.array(inpt21) mdata=np.array([zarray,tarray]) kdata=mdata.t i=np.searchsorted(kdata[:,0],ldata[:,0]) print
my inputfile-2 & inputfile-1
0 5 0 5 100 6 50 6 200 10 200 15 300 12 350 12 400 15 # obtained 400 15 #simulated value 500 20 #value 500 25 600 0 650 0 700 11 700 11 800 12 850 8 900 19 900 19 1000 10 1000 3
having hard time numpy arrays , iterations. please suggest how can solve above problem. in-fact have other columns manipulation depend on match of first column(time match).
once again in advance.
did mean like
import numpy np simulated = np.array([ (0, 5), (100, 6), (200, 10), (300, 12), (400, 15), (500, 20), (600, 0), (700, 11), (800, 12), (900, 19), (1000, 10) ]) actual = np.array([ (0, 5), (50, 6), (200, 15), (350, 12), (400, 15), (500, 25), (650, 0), (700, 11), (850, 8), (900, 19), (1000, 3) ]) def indexes_where_match(a, b): """ iterator goes on indexes of wherever entries in a's first-col , b's first-col match """ return (i i, (a, b) in enumerate(zip(a, b)) if a[0] == b[0]) def main(): in indexes_where_match(simulated, actual): print(simulated[i][1], 'should compared to', actual[i][1]) if __name__ == '__main__': main()
you use column-slicing, this:
simulated_time, simulated_values = simulated[..., 0], simulated[..., 1:] actual_time, actual_values = actual[..., 0], actual[..., 1:] indexes_where_match = (i i, (a, b) in enumerate(zip(simulated_time, actual_time)) if == b) in indexes_where_match: print(simulated_values[i], 'should compared to', actual_values[i]) # outputs: # [5] should compared [5] # [10] should compared [15] # [15] should compared [15] # [20] should compared [25] # [11] should compared [11] # [19] should compared [19] # [10] should compared [3]
Comments
Post a Comment