【发布时间】: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