numpy replace groups of elements with integers incrementally

前端 未结 2 1076
花落未央
花落未央 2021-01-13 02:53
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

2条回答
  •  时光取名叫无心
    2021-01-13 03:16

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

提交回复
热议问题