2017-08-30 762 views
2

我想预定值排列从Float64转换(隔离林的泡菜文件从scikit学习)型的,以FLOAT32从Float64转换numpy的数组类型和值FLOAT32

for i in range(len(tree.tree_.threshold)): 
    tree.tree_.threshold[i] = tree.tree_.threshold[i].astype(np.float32) 

然后再打印

for value in tree.tree_.threshold[:5]: 
    print(type(value)) 
    print(value) 

我得到的输出是:

<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 

我一个没有得到正确的转换到Float32。我想将值和它们的类型转换为Float32,有没有人有任何解决方法呢?

+0

没有,也没有缺失值,而最大值为526225.98822 –

+0

可以给我们打印tree.tree_.threshold.flags – Glostas

+0

C_CONTIGUOUS:假 F_CONTIGUOUS:假 OWNDATA:假 写:真 齐:真 UPDATEIFCOPY:假 –

回答

0

Actually i tried hard but not able to do as the 'sklearn.tree._tree.Tree' objects is not writable.

It is causing a precision issue while generating a PMML file, so i raised a bug over there and they gave an updated solution for it by not converting it in to the Float64 internally.

欲了解更多信息,您可以点击此链接: Precision Issue

1

问题是你不做任何类型转换的numpy数组。你计算一个float 32变量,并把它作为一个入口到一个float64 numpy数组中。 numpy的然后将其转换得当回float64

尝试成才,如:

a = np.zeros(4,dtype="float64") 
print a.dtype 
print type(a[0]) 
a= float32(a) 
print a.dtype 
print type(a[0]) 

输出

float64 
<type 'numpy.float64'> 
float32 
<type 'numpy.float32'> 

一个是你的情况阵列tree.tree_.threshold(与Python 2.7测试)

+0

你能约np.zeroes参数解释,特别是“4” –

+0

这是一个充满创造的东西一numpy的阵列的禁食方式...这里零。 4只是零的数量。我选择4,因为我喜欢这个号码... – Glostas

+0

-------------------------------------- ------------------------------------- AttributeError Traceback(最近呼叫的最后一个) () ----> 1 tree.tree_.threshold = np.zeros(4,dtype =“float64”) 2 print(tree.tree_.threshold.dtype) 3 tree .tree_.threshold = float32(tree.tree_threshold) 4 print(a.dtype) AttributeError:'sklearn.tree._tree.Tree'对象的属性'threshold'不可写 –

0

你可以试试这个:

tree.tree_.threshold[i]=tree.tree_.threshold[i].astype('float32',casting='same_kind’)