【问题标题】:NumPy log function throws attribute error for intNumPy 日志函数为 int 引发属性错误
【发布时间】:2014-08-20 22:41:30
【问题描述】:

我正在尝试使用日志丢失功能并不断收到以下错误-

   AttributeError: log

引发此错误的代码行是 -

ll = sum(act*sp.log(pred) + sp.subtract(1,act)*sp.log(sp.subtract(1,pred)))

pred 在哪里-

[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0......1]

并且行动是-

[[0     1]
 [1     1]
 [2     1]
 [3     1]
 [4     1]
 [5     1]
 [6     1]
 [7     1]
 [8     1]
 [9     1]
 .
 .
 .
 [n     1]] 

有人可以帮我解决这个问题吗?完全撞墙了。

【问题讨论】:

  • 我在你的等式中没有看到 YT
  • 你的数组的数据类型是什么?大概就是这个bug:github.com/numpy/numpy/issues/1611
  • 继续@BrenBarn 的评论:pred.dtype 是什么?我怀疑pred 是一个对象数组(即pred.dtypedtype('O'))。如果您打印pred,您可能会看到类似array([...numbers...], dtype=object) 的内容。作为一种变通方法,在ll 的表达式中将pred 替换为pred.astype(int)(或pred.astype(float),如果值是浮点数)。
  • @Cyber​​ - 刚刚纠正了我的问题
  • 在我之前的评论中,当我说“如果您打印...”时,我实际上想到的是在交互式 shell(如 ipython)中显示的输出,如果您只需输入 pred by本身在输入线上。如果您使用print 语句,则需要使用print repr(pred) 来查看我希望您看到的内容。

标签: python machine-learning scipy logistic-regression


【解决方案1】:

@WarrenWeckesser 的回答帮助了我-

“作为一种变通方法,在你的 ll 表达式中将 pred 替换为 pred.astype(int)(或 pred.astype(float),如果值是浮点数)”

【讨论】:

    猜你喜欢
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多