错误:' Numpy.str _'对象没有属性' decode'

时间:2016-08-05 13:10:11

标签: python python-3.x numpy attributes

我尝试在基于python3的Crab(开源推荐系统)上运行测试。然后发生错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/Dennis/anaconda/lib/python3.5/site-packages/scikits/crab/datasets/base.py", line 201, in load_sample_movies
    data_songs[u_ix][i_ix] = float(rating)
ValueError: could not convert string to float: "b'3.0'"

我尝试使用&#39; decode()&#39;转换字符串,但它不起作用:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/Dennis/anaconda/lib/python3.5/site-packages/scikits/crab/datasets/base.py", line 202, in load_sample_movies
    rating = rating.decode('utf-8')
AttributeError: 'numpy.str_' object has no attribute 'decode'  

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

问题是rating是字符串中的字符串,因此当您尝试将类似"b'3.0'"的字符串转换为浮点数时,它会提供valueError,因为您仍然拥有{ {1}}在前面,无法转换为浮动。

我想你需要b前面的字节编码,所以有一种方法是评估'3.0'将它从字符串转换为字节,然后再将其转换为浮点数(请注意, eval可能存在一些安全问题。)

rating