arrays - Python: comparing columns in numpy -
i writing program creates file add_to_file function , program takes file input.
the file has 4 columns: name, start position(s), description, end position(s)
what program supposed do, read file numpy array , create plausible "motion-sequences" creating array have @ least 1 of end positions in motion same @ least 1 of start positions in motion follow
def make_sequence(size,array): count = 0 sequence = [array[0]] result = [array[0,0]] row in array[1:size]: if row[1] == sequence[count][3]: sequence.append(row) result.append(row[0]) count += 1 else: pass return result
what doing, it's taking array made file , making new array (result) such new array names of movements, movements next eachother flow because starting positions exact same ending positions of prior motion! here issue:
some movements can start , end in variety of positions, , have written can take 1 starting , 1 ending position per motion.
question: how make if string position values comma separates values ie(a,b,c,d), able iterate through them , compare them start end function return motions flow?
edit:
here example of want code able do
input:
row_1 = ["name1", "1,3,4,5", "description", "2"] row_2 = ["name2", "5,3", "description", "4"] row_3 = ["name3", "2", "description", "1"] row_4 = ["name4", "1", "description", "7,3"] row_5 = ["name5", "3", "description", "5,9,4"] row_6 = ["name6", "2", "description", "7"] row_7 = ["name7", "7", "description", "2"] row_8 = ["name8", "1", "description", "4"] row_9 = ["name9", "4", "description", "2"] data = np.array((column_index,row_1,row_2,row_3,row_4,row_5,row_6,row_7,row_8,row_9)) print make_sequence(7,array)
output:
[name1,name3,name4,name5]
you output because starts automatically row_1, has name: "name1", reads next row , if row has starting position same ending position previous row appended correct sequence, it's name appended result array.
and first 7 rows since input 7 size
well, there 2 questions here. 1 data processing: how extract required information raw data file. want names (column 0), start position (first element of column 1), , end positions (last element of column 3). might like:
import numpy np dat = np.array([["name1", "1,3,4,5", "description", "2"], ["name2", "5,3", "description", "4"], ["name3", "2", "description", "1"], ["name4", "1", "description", "7,3"], ["name5", "3", "description", "5,9,4"], ["name6", "2", "description", "7"], ["name7", "7", "description", "2"], ["name8", "1", "description", "4"], ["name9", "4", "description", "2"]]) #since want 7 initial columns dat = dat[0:7,:] #extract second , fourth columns c1 = dat[:,1] c2 = dat[:,3] names = dat[:,0] #get start position via string manipulation startpos = [] item in c1: startpos.append(item.split(',')[0]) startpos = np.array(startpos).astype(np.int32) #get end position via string manipulation endpos = [] item in c2: endpos.append(item.split(',')[-1]) endpos = np.array(endpos).astype(np.int32) print startpos print endpos
cool. need follow startpos , endpos, so:
def followpath(a1,a2): needle = 1 path = [] ii,item,newneedle in zip(np.arange(a1.size)+1,a1,a2): if item == needle: path.append(ii) needle = newneedle return np.array(path) - 1 thepath = followpath(startpos,endpos) print names[thepath]
this gives me:
['name1' 'name3' 'name4' 'name5']
Comments
Post a Comment