gpt4 book ai didi

python - 带有 numpy 数组的 numpy 日志

转载 作者:行者123 更新时间:2023-11-28 16:23:29 24 4
gpt4 key购买 nike

我想了解为什么会出现以下代码:

print((hypothesis(x, theta_)))

以这种格式生成一个数组

[0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]

当我应用 numpy.log 函数时:

print(np.log(hypothesis(x, theta_)))

我得到以下结果

[-0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718
-0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718
-0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718
-0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718
-0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718
-0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718
-0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718
-0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718
-0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718
-0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718
-0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718
-0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718
-0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718
-0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718
-0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718
-0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718 -0.69314718
-0.69314718 -0.69314718 -0.69314718 -0.69314718]

为什么我应用log函数时,数组的格式不一样?

最佳答案

大概 hypothesis(x, theta_) 返回一个 python list。打印列表时,会包含逗号。

np.log(hypothesis(x, theta_)) 返回一个 numpy 数组。打印 numpy 数组时,不包括逗号。

例如:

In [1]: x = [1, 2, 3]  # `x` is a python list.

In [2]: print(x)
[1, 2, 3]

In [3]: a = np.array(x) # `a` is a numpy array.

In [4]: print(a)
[1 2 3]

为什么 numpy 不在打印输出中包含逗号?这是你必须问 numpy 开发人员的事情。它确实使输出不那么困惑,但如果您想将打印的值复制并粘贴回其他代码,这可能会很麻烦。

如果您打印“repr”,则输出包含名称 array,并且包含逗号:

In [6]: print(repr(a))
array([1, 2, 3])

关于python - 带有 numpy 数组的 numpy 日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38444245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com