I´d like to understand why the following code:
print((hypothesis(x, theta_)))
results in a array with this format
[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]
and when I apply the numpy.log function:
print(np.log(hypothesis(x, theta_)))
I get the following result
[-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]
Why is the format of the array different when I apply the log function?
Presumably hypothesis(x, theta_) returns a python list. When you print a list, the commas are included.
np.log(hypothesis(x, theta_)) returns a numpy array. When you print a numpy array, the commas are not included.
For example:
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]
Why doesn't numpy include the commas in the printed output? That's something you'd have to ask the numpy developers. It does make the output a bit less cluttered, but it can be a nuisance if you ever want to copy-and-paste the printed values back into some other code.
If your print the "repr", the output includes the name array, and it includes the commas:
In [6]: print(repr(a))
array([1, 2, 3])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With