Klausos Klausos
Klausos Klausos

Reputation: 16050

How to round all values in one-dimensional numpy.ndarray

I have one-dimensional numpy.ndarray called y that contains natural logarithm values. I want to convert all these values into a linear scale and round, using just a single line of code. The following code works, but it provides incorrect results. For example, the first value in result is 0, instead of 15.

result = [round(np.expm1(x)) for x in range(len(y))]

Upvotes: 0

Views: 996

Answers (1)

Charlie Haley
Charlie Haley

Reputation: 4310

Use

result = [round(np.expm1(x)) for x in y] 

or

result = [round(np.expm1(y[x])) for x in range(len(y))]

The way you have it now, you are putting the array index into the function.

Upvotes: 2

Related Questions