2D numpy array doesn't implicitly convert from int64 to float64

Sticky

How come when the numpy array is a vector, the setting works and the dtype is implicitly converted to float but when the numpy array is a matrix, the setting works but the dtype is still int. Here's a demo script to illustrate the problem.

import numpy as np

# successfully sets / converts
x = np.array([100, 101])
c = -np.max(x)
x += c
print 'before', x.dtype
x = np.exp(x)
print 'after', x.dtype

print x

# doesn't successfully set / convert
matrix = np.array([(100, 101), (102, 103)])
for i in range(len(matrix)):
    c = -np.max(matrix[i])
    matrix[i] += c
    print 'before', matrix[i].dtype
    matrix[i] = np.exp(matrix[i])
    print 'after', matrix[i].dtype

print matrix

output:

before int64
after float64 <-- from vector
[ 0.36787944  1.        ]
before int64
after int64 <-- from row 1 of matrix
before int64
after int64 <-- from row 2 of matrix
[[0 1]
 [0 1]]

The numbers are integer truncated, which was my original problem, traced down to this.

I'm using Python 2.7.11 and numpy 1.13.0

Nils Werner

Whenever you write a value into an existing array, the value is cast to match the array dtype. In your case, the resulting float64 value is cast to int64:

b = numpy.arange(4).reshape(2, 2)
b.dtype  # dtype('int64')

taking numpy.exp() of any of these values will return a float64:

numpy.exp(b[0, :]).dtype  # dtype('float64')

but if you now take this float64 and write it back into the original int64 array, it needs to be cast first:

b[0, :] = numpy.exp(b[0, :])
b.dtype  # dtype('int64')

Note that using

b = numpy.exp(b)

creates a new array with its own dtype. If instead you did

b[:] = numpy.exp(b[:])

you would be implicitly casting to int64 again.

Also note that there is no need to write a loop like you did. Instead you can vectorize the operation:

np.exp(matrix - numpy.max(matrix, axis=1, keepdims=True))
# array([[ 0.36787944,  1.        ],
#        [ 0.36787944,  1.        ]])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to convert a numpy array from 'float64' to 'float'

From Dev

NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?

From Dev

Convert numpy array type and values from Float64 to Float32

From Dev

Convert float64 numpy array to floats not in scientific notation

From Dev

Convert float64 to int64 using "safe"

From Dev

Convert float64 column to int64 in Pandas

From Dev

How to Convert from double to Int64

From Dev

Cannot convert from '_int64' to 'Data *'

From Dev

How to Convert from double to Int64

From Dev

How to convert Array{Float64, 1} to float in julia?

From Dev

Cast from int64 to byte array

From Dev

How to convert a Tuple{Array{Float64,1},Array{Float64,1}} to Array{Tuple{Float64,Float64},1} in julia

From Dev

How to convert a Tuple{Array{Float64,1},Array{Float64,1}} to Array{Tuple{Float64,Float64},1} in julia

From Dev

Convert 64 bits array into Int64 or ulong C#

From Dev

Tensorflow MaxPool doesn't accept float64

From Dev

Convert dtype from int64 to int32

From Dev

Convert dtype from int64 to int32

From Dev

Why cannot numpy arrays convert from datetime to np.datetime64 implicitly?

From Dev

Numpy.dot TypeError: Cannot cast array data from dtype('float64') to dtype('S32') according to the rule 'safe'

From Dev

How to safely round-and-clamp from float64 to int64?

From Dev

How to safely round-and-clamp from float64 to int64?

From Dev

How can I convert an int64 into a byte array in go?

From Dev

Convert list into array dtype=int64 in Python

From Dev

Convert byte array to signed int64 in javascript

From Dev

Numpy float64 vs Python float

From Dev

Convert Int64 to UUID

From Dev

Is there a way to prevent dtype from changing from Int64 to float64 when reindexing/upsampling a time-series?

From Dev

How to convert Array{Array{Float64, 1}, 1} to Matrix in julia?

From Dev

convert 2d numpy array to string

Related Related

  1. 1

    How to convert a numpy array from 'float64' to 'float'

  2. 2

    NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?

  3. 3

    Convert numpy array type and values from Float64 to Float32

  4. 4

    Convert float64 numpy array to floats not in scientific notation

  5. 5

    Convert float64 to int64 using "safe"

  6. 6

    Convert float64 column to int64 in Pandas

  7. 7

    How to Convert from double to Int64

  8. 8

    Cannot convert from '_int64' to 'Data *'

  9. 9

    How to Convert from double to Int64

  10. 10

    How to convert Array{Float64, 1} to float in julia?

  11. 11

    Cast from int64 to byte array

  12. 12

    How to convert a Tuple{Array{Float64,1},Array{Float64,1}} to Array{Tuple{Float64,Float64},1} in julia

  13. 13

    How to convert a Tuple{Array{Float64,1},Array{Float64,1}} to Array{Tuple{Float64,Float64},1} in julia

  14. 14

    Convert 64 bits array into Int64 or ulong C#

  15. 15

    Tensorflow MaxPool doesn't accept float64

  16. 16

    Convert dtype from int64 to int32

  17. 17

    Convert dtype from int64 to int32

  18. 18

    Why cannot numpy arrays convert from datetime to np.datetime64 implicitly?

  19. 19

    Numpy.dot TypeError: Cannot cast array data from dtype('float64') to dtype('S32') according to the rule 'safe'

  20. 20

    How to safely round-and-clamp from float64 to int64?

  21. 21

    How to safely round-and-clamp from float64 to int64?

  22. 22

    How can I convert an int64 into a byte array in go?

  23. 23

    Convert list into array dtype=int64 in Python

  24. 24

    Convert byte array to signed int64 in javascript

  25. 25

    Numpy float64 vs Python float

  26. 26

    Convert Int64 to UUID

  27. 27

    Is there a way to prevent dtype from changing from Int64 to float64 when reindexing/upsampling a time-series?

  28. 28

    How to convert Array{Array{Float64, 1}, 1} to Matrix in julia?

  29. 29

    convert 2d numpy array to string

HotTag

Archive