Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting floats in a numpy array [duplicate]

If I have a numpy array like this:

[2.15295647e+01, 8.12531501e+00, 3.97113829e+00, 1.00777250e+01] 

how can I move the decimal point and format the numbers so I end up with a numpy array like this:

[21.53, 8.13, 3.97, 10.08] 

np.around(a, decimals=2) only gives me [2.15300000e+01, 8.13000000e+00, 3.97000000e+00, 1.00800000e+01] Which I don't want and I haven't found another way to do it.

like image 558
Kaly Avatar asked Jan 08 '14 23:01

Kaly


People also ask

How do I change the format of a NumPy array?

We have a method called astype(data_type) to change the data type of a numpy array. If we have a numpy array of type float64, then we can change it to int32 by giving the data type to the astype() method of numpy array. We can check the type of numpy array using the dtype class.

How do you format a float in Python?

In Python, there are various methods for formatting data types. The %f formatter is specifically used for formatting float values (numbers with decimals). We can use the %f formatter to specify the number of decimal numbers to be returned when a floating point number is rounded up.

How do you repeat an array in NumPy?

NumPy: repeat() function The repeat() function is used to repeat elements of an array. Input array. The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.


1 Answers

In order to make numpy display float arrays in an arbitrary format, you can define a custom function that takes a float value as its input and returns a formatted string:

In [1]: float_formatter = "{:.2f}".format 

The f here means fixed-point format (not 'scientific'), and the .2 means two decimal places (you can read more about string formatting here).

Let's test it out with a float value:

In [2]: float_formatter(1.234567E3) Out[2]: '1234.57' 

To make numpy print all float arrays this way, you can pass the formatter= argument to np.set_printoptions:

In [3]: np.set_printoptions(formatter={'float_kind':float_formatter}) 

Now numpy will print all float arrays this way:

In [4]: np.random.randn(5) * 10 Out[4]: array([5.25, 3.91, 0.04, -1.53, 6.68] 

Note that this only affects numpy arrays, not scalars:

In [5]: np.pi Out[5]: 3.141592653589793 

It also won't affect non-floats, complex floats etc - you will need to define separate formatters for other scalar types.

You should also be aware that this only affects how numpy displays float values - the actual values that will be used in computations will retain their original precision.

For example:

In [6]: a = np.array([1E-9])  In [7]: a Out[7]: array([0.00])  In [8]: a == 0 Out[8]: array([False], dtype=bool) 

numpy prints a as if it were equal to 0, but it is not - it still equals 1E-9.

If you actually want to round the values in your array in a way that affects how they will be used in calculations, you should use np.round, as others have already pointed out.

like image 148
ali_m Avatar answered Sep 24 '22 19:09

ali_m