使用NumPy的浮点参数和dict_values [英] Float Arguments and dict_values with NumPy

查看:154
本文介绍了使用NumPy的浮点参数和dict_values的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主要问题是Python 3告诉我dict_values被用作参数,而显然需要一个字符串或数字. (即使浮点数一个数字,或者我也这么认为.)

The main issue is that I'm being told by Python 3 that dict_values are being used as an argument while, apparently, a string or number is required. (Even though a float is a number, or so I thought.)

我已经寻找了解决该问题的各种解决方案,尽管我发现某些方面似乎在暗示可能要做的 ,但并没有立即提出来并以我可以归纳为自己的方式来说明这一点.详细信息.

I have searched for various solutions to this problem and while I see aspects that seem to suggest what might be done, nothing just comes right out and says it in a way that I can generalize to my particulars.

代码是:

def get_features(self, state, action):
    q_state = np.array(SimpleExtractor().get_features(state, action).values()).astype(dtype=float)
    return q_state

在执行代码时返回以下错误:

That returns the following error when the code is executed:

float() argument must be a string or a number, not 'dict_values'

按照建议的解决方案之一,我尝试更改np行,以便将dtype表示为:

As per one of the suggested solutions, I tried to change the np line so that the dtype was indicated as such:

def get_features(self, state, action):
    q_state = np.array(SimpleExtractor().get_features(state, action).values(), dtype=float)
    return q_state

但是返回完全相同的错误.

However that returns the exact same error.

我不确定是否重要,但是我要调用的SimpleExtractor是大量代码:

I'm not sure if it matters but that SimpleExtractor that I'm calling is a large bit of code: SimpleExtractor

我有一定的理由相信,这段编写的特定代码可以在Python 2上运行.但是我所从事的特定项目已经过重组,可以与Python 3一起使用,因此我无法再使用它来运行它. Python 2进行测试.

I have some reason to believe that this particular bit of code, as written, would work on Python 2. But the particular project I'm on has been restructured to work with Python 3 and so I can no longer run it with Python 2 to test that out.

我认为这可能与从SimpleExtractor中的get_features方法返回features变量的方式有关.但是我不清楚如何将其转换为上面我的代码可以接受的某种形式.

I think this may have to do with the fact of how the features variable is being returned from the get_features method in SimpleExtractor. But I'm not clear what I would do to get that into some form that my code above would accept.

我主要是想了解在这种情况下我应该怎么做.

I'm mainly just looking for insight into what I should do in this kind of situation.

推荐答案

首先将dict_values对象转换为列表,以便从列表的内容创建数组. NumPy无法从dict_values构建包含项的数组,而是创建类型对象的数组,并将dict_values对象放入内部:

Convert the dict_values object to list first so the array is created from the content of the list. NumPy cannot build an array of the contained items from dict_values, it instead creates an array of type object and puts the dict_values object inside:

>>> np.array(d.values())
array(dict_values([2, 4]), dtype=object)

您在做什么:

>>> import numpy as np
>>> d = {1: 2, 3: 4}
>>> np.array(d.values()).astype(float)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: float() argument must be a string or a number, not 'dict_values'


您应该做什么:


What you should do:

>>> np.array(list(d.values())).astype(float)
array([ 2.,  4.])


q_state =  np.array(list(SimpleExtractor().get_features(state, action).values()))\
                                          .astype(dtype=float)

这篇关于使用NumPy的浮点参数和dict_values的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆