我想了解为什么下面的代码:
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]当我应用日志函数时,为什么数组的格式不同?
发布于 2016-07-18 19:13:27
据推测,hypothesis(x, theta_)返回一个python列表。打印列表时,将包括逗号。
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在打印输出中不包括逗号?这是你必须要问的那些不起眼的开发者。它确实减少了输出的杂乱,但如果您想要将打印的值复制并粘贴到其他代码中,则可能会带来麻烦。
如果打印"repr",则输出包含名称array,并包含逗号:
In [6]: print(repr(a))
array([1, 2, 3])https://stackoverflow.com/questions/38444245
复制相似问题