import numpy as np
data = np.array([\'b\',\'b\',\'b\',\'a\',\'a\',\'a\',\'a\',\'c\',\'c\',\'d\',\'d\',\'d\'])
I need to replace each group of strin
Given @DSM's noticing that my original idea doesn't work robustly, the best solution I can think of is a replacement dictionary:
data = np.array(['b','b','b','a','a','a','a','c','c','d','d','d'])
_, ind = np.unique(data, return_index=True)
unqs = data[np.sort(ind)]
data_id = dict(zip(unqs, np.arange(data.size)))
num = np.array([data_id[datum] for datum in data])
for the month data:
In [5]: f = open('test.txt','r')
In [6]: data = np.array([line.strip() for line in f.readlines()])
In [7]: _, ind, inv = np.unique(data, return_index=True)
In [8]: months = data[np.sort(ind)]
In [9]: month_id = dict(zip(months, np.arange(months.size)))
In [10]: np.array([month_id[datum] for datum in data])
Out[10]: array([ 0, 0, 0, ..., 41, 41, 41])