【问题标题】:how to get rid of error "'float' object has no attribute 'exp'"?如何摆脱错误“'float'对象没有属性'exp'”?
【发布时间】:2017-05-01 22:41:49
【问题描述】:

我创建了一个存储一些信息的列表,如下所示:

import numpy as np

moves = [(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4)]

wins = [i for i in range(2 * len(moves))]
playout_number = [23 for i in range(2 * len(moves))]

data = [list(map(lambda i: moves[i] if divmod(i, len(moves))[0] != 1 else moves[divmod(i, len(moves))[1]],
                       [i for i in range(2 * len(moves))])),
    list(map(lambda i: 1 if i >= len(moves) else 2,
                       [i for i in range(2 * len(moves))])),
    wins,
    playout_number
    ]
table = np.asarray(data)
player1_info = table[:, np.where(table[1, :] == 1)[0].tolist()]

代码到这里都运行良好,但是当我添加下面的代码时:

b = np.divide(player1_info[2, :], player1_info[3, :]).reshape(1, player1.shape[1])
print(np.exp(b))

它给出了错误:'float' object has no attribute 'exp'。

如何重写代码来应对错误?

【问题讨论】:

  • 我不知道根本原因(可能是因为那些列表理解),但 b 的 dtype 是对象。 np.exp(b.astype('float')) 应该可以工作。
  • 您似乎为np 名称分配了一个浮点数。
  • 很确定 np.where(table[1, :] == 1)[0].tolist() 可以在您那里的上下文中简单地拼写为 table[1, :] == 1
  • 是的,table[1, :] == 1 做了同样的事情,但是错误还没有解决。

标签: python python-3.x numpy


【解决方案1】:

我这样使用np.float64

b = np.divide(player1_info[2, :], player1_info[3, :])
b = np.float64(b)
print(np.exp(b))

感谢@Eric,我将table[:, np.where(table[1, :] == 1)[0].tolist()] 更改为table[:, table[1, :] == 2]
错误没有出现,它给出了正确的答案。
我认为np.exp 不知道列表元素,它无法计算。

【讨论】:

    猜你喜欢
    • 2013-09-04
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多