2017-01-22 98 views
0

我試圖爲不同深度的決策樹計算測試和訓練錯誤。不同長度的決策樹錯誤

train_error = [] 
test_error = []  
for i in range (3,21): 
    X_train, X_test, y_train, y_test = train_test_split(womendata, womeny, test_size=0.4, random_state=1) 
    decitiontree = tree.DecisionTreeClassifier(criterion='gini', splitter='best', max_depth=i, class_weight = 'balanced', min_samples_split=i) 
    clf = decitiontree.fit(X_train, y_train) 
    train_error.append(1 - clf.score(X_train, y_train) )  
    test_error.append(1 - clf.score(X_test, y_test) ) 

在Python 3我得到的錯誤:

Traceback (most recent call last): 
    File "<stdin>", line 4, in <module> 
    File "/usr/local/lib/python3.4/dist-packages/sklearn/tree/tree.py", line 154, in fit 
    X = check_array(X, dtype=DTYPE, accept_sparse="csc") 
    File "/usr/local/lib/python3.4/dist-packages/sklearn/utils/validation.py", line 398, in check_array 
    _assert_all_finite(array) 
    File "/usr/local/lib/python3.4/dist-packages/sklearn/utils/validation.py", line 54, in _assert_all_finite 
    " or a value too large for %r." % X.dtype) 

ValueError: Input contains NaN, infinity or a value too large for dtype('float32'). 

兩個womendata EN女性y是相同的長度,而且有在集中沒有丟失的數據。

回答

0

來自您提供的數據數組中包含無效值的錯誤。

ValueError: Input contains NaN, infinity or a value too large for dtype('float32').

請檢查您的數據是有效的含義:

  1. 在womendata或womeny沒有NaN值
  2. 在womendata或womeny
  3. 值的範圍內沒有天道酬勤值float32 min和float32 max

您可以使用下面的代碼:

import numpy as np 
info = np.finfo(np.float64) 

for x in [womendata, womeny]: 
    assert np.all(x <= info.max) and np.all(x >= info.min), 'not all values in range' 
    assert np.all(x != np.inf) and np.all(x != -np.inf), 'data contains infinity value' 
    assert np.all(x is not np.nan), 'data contains Nan value' 
+0

這是適合你的嗎?設法解決它? – ShmulikA