Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy log with numpy arrays

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?

like image 510
Daniel Pereira Avatar asked May 30 '26 07:05

Daniel Pereira


1 Answers

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])
like image 107
Warren Weckesser Avatar answered May 31 '26 21:05

Warren Weckesser