问题背景:
在使用无监督算法后需要观察将维后的向量分布 这里使用 Umap,在使用 umap.plot.points 的时候使用umap.plot.points 出现 报错
问题描述:
umap.plot.points(reducer, labels= self.str_label, theme='fire', color_key_cmap='Paired', background='black') 提示
AttributeError: 'list' object has no attribute 'shape'
解决过程及问题成因:
由于这里的 self.str_label 是 list:
['_u_turn','_turn_left',......等等]
在plot 的时候需要校验 labels.shape[0] == data.shape[0]
data 是numpy array 的形式 因此没有问题
而list 确实没有shape 属性
解决方案:
方案1:将 list 转为 pandas Series 类:
from pandas import Series self.label = Series(self.label)
此为简易方案
方案2:直接为 list 类创建子类 加入 shape 属性,根据builtins.py 的list 类,我们可一些写出新的子类如下:
class slist(list): def __init__(self,seq=()): super().__init__(seq) self.shape= (len(self),)
然后
umap.plot.points(reducer, labels= slist(self.str_label), theme='fire', color_key_cmap='Paired', background='black')
但是依旧发现label 的显示框在右上角什么都没有:
结果发现 字符串不能以下划线 _ 起始,不然不会显示,删除开头下划线即可正常显示
结论及重点:
本文旨在 解决 以字符串 为 label 绘制 umap.plot.points 时出现的list 属性问题,并以 子类继承的方式为 新list 类增加 shape 属性