我在以下Python列表中有地面真实数据:
ground_truth = [(A,16), (B,18), (C,36), (A,59), (C,77)]
Run Code Online (Sandbox Code Playgroud)
因此,来自以下任何值:
0-16 gets mapped to A,
17-18 maps to B,
19-36 maps to C,
37-59 maps to A
60-77 maps to C
and so on
Run Code Online (Sandbox Code Playgroud)
我正在尝试映射类似数字的时间序列输入
[9,15,29,32,49,56, 69] to its respective classes like:
[A, A, C, C, A, A, C]
Run Code Online (Sandbox Code Playgroud)
假设我的输入是一个类似Pandas的系列:
in = pd.Series([9,15,29,32,49,56, 69])
Run Code Online (Sandbox Code Playgroud)
我如何进入该系列[A, A, C, C, A, A, C]?
这是我的方法:
gt = pd.DataFrame(ground_truth)
# bins for cut
bins = [0] + list(gt[1])
# categories
cats = pd.cut(pd.Series([9,15,29,32,49,56, 69]), bins=bins, labels=False)
# labels
gt.loc[cats, 0]
Run Code Online (Sandbox Code Playgroud)
给
0 A
0 A
2 C
2 C
3 A
3 A
4 C
Name: 0, dtype: object
Run Code Online (Sandbox Code Playgroud)
或者,不创建新的数据框:
labels = np.array([x for x,_ in ground_truth])
bins = [0] + [y for _,y in ground_truth]
cats = pd.cut(pd.Series([9,15,29,32,49,56, 69]), bins=bins, labels=False)
labels[cats]
Run Code Online (Sandbox Code Playgroud)
这使:
array(['A', 'A', 'C', 'C', 'A', 'A', 'C'], dtype='<U1')
Run Code Online (Sandbox Code Playgroud)