2017-08-30 74 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’) 
相關問題