Aks*_*kar 15 python numpy pickle scikit-learn
我试图将类型从Float64转换为Float32的阈值数组(隔离森林的pickle文件从scikit学习)
for i in range(len(tree.tree_.threshold)):
tree.tree_.threshold[i] = tree.tree_.threshold[i].astype(np.float32)
Run Code Online (Sandbox Code Playgroud)
然后打印它
for value in tree.tree_.threshold[:5]:
print(type(value))
print(value)
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
<class 'numpy.float64'>
526226.0
<class 'numpy.float64'>
91.9514312744
<class 'numpy.float64'>
3.60330319405
<class 'numpy.float64'>
-2.0
<class 'numpy.float64'>
-2.0
Run Code Online (Sandbox Code Playgroud)
我没有得到正确的转换到Float32.我想将值及其类型转换为Float32,有没有人有解决方法?
Glo*_*tas 14
问题是你没有对numpy数组进行任何类型转换.您计算一个float32变量并将其作为一个条目放入float64 numpy数组.numpy然后将其正确转换回float64
尝试像这样:
a = np.zeros(4,dtype="float64")
print a.dtype
print type(a[0])
a = np.float32(a)
print a.dtype
print type(a[0])
Run Code Online (Sandbox Code Playgroud)
输出(用python 2.7测试)
float64
<type 'numpy.float64'>
float32
<type 'numpy.float32'>
Run Code Online (Sandbox Code Playgroud)
在你的情况下,a是数组tree.tree_.threshold
| 归档时间: |
|
| 查看次数: |
43307 次 |
| 最近记录: |