为什么当numpy的阵列是一个向量,设置工程和dtype
被隐式转换为float但当numpy的阵列是矩阵,设置工程,但dtype
仍然是int。这里有一个演示脚本来说明问题。2D numpy的阵列不隐式地从Int64类型转换为float64
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
输出:
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]]
的数字是整数截断,这是我原来的问题,追踪到这一点。我使用的是Python 2.7.11
和numpy 1.13.0
Python和numpy版本? –
Python的2.7.11和numpy的1.13.0 – Sticky