python - Find occurrences of a value in a numpy array and assign it appropriate weights -
i have text file of close 1 million lines.it has 2 columns.column 1 has numbers 0-99 , column has 4 sizes ranging s,m,l,xl. numbers 0 99 keep repeating in 1 million lines different sizes follows:
11 s 19 s 19 m 19 m 63 l 14 s 11 l 63 xl 14 s 11 l 63 xl
my objective find final size each number.the plan of action find occurrence of each number,find size each occurrence , assign final size number maximum occurrences of size.
expected output :
11 l 14 s 19 m 63 xl
because of size of dataset,i having @ numpy,not have prior experience it. have started creating basic numpy array follows:
import numpy np data = np.loadtxt('size_data.txt')
this create numpy array.however,from whatever documentation have read till now,it not there direct way of doing want accomplish.can 1 give me pointers on how move forward ?
we can applying numpy.unique
on reversed version of first column received file. reversing required because other wise it(return_index=true
) return indices of first occurrence found of item start.
>>> arr = np.loadtxt('foo.txt', dtype=object) >>> _, indices = np.unique(arr[:, 0][::-1], return_index=true) >>> arr[::-1][indices] array([['11', 'l'], ['14', 's'], ['19', 'm'], ['63', 'xl']], dtype=object) # or >>> arr[len(arr) - indices - 1] array([['11', 'l'], ['14', 's'], ['19', 'm'], ['63', 'xl']], dtype=object)
Comments
Post a Comment