AttributeError: ‘list‘object has no attribute‘shape‘ 的另类解决方法 和 umap.plot.points labels 图标显示为空的原因

问题背景:

在使用无监督算法后需要观察将维后的向量分布 这里使用 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 属性


  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值