2016-01-19 23 views
1

我正在處理分類問題。我想的標籤預測:Scikit學習:RandomForest:clf.predict適用於float,但不適用clf.score

df3['relevance'].unique() 
array([ 3. , 2.5 , 2.33, 2.67, 2. , 1. , 1.67, 1.33, 1.25, 
     2.75, 1.75, 1.5 , 2.25]) 

當我打電話用我所做的預測功能,它的工作原理確定:

clf = RandomForestClassifier() 
clf.fit(df3[features], df['relevance']) 
pd.crosstab(clf.predict(df3[features]), df3['relevance']) 

但是,當我打電話clf.score:

clf.score(df3['features'], df3['relevance']) 

我得到 ValueError:連續不支持

我應該是C lassifying相關標籤我試圖預測爲另一種數據類型?謝謝你的幫助。

回答

1

您面臨的問題可能是因爲您的relevance列由連續數字組成。

如果您試圖預測連續數字,我建議切換到RandomForestRegressor()。否則,根據某個閾值將您的變量轉換爲1和0。

+0

tthanks @ericmjl - 的迴歸做了工作。現在閱讀有關它。 –

1

簡單地將標籤編碼爲整數,一切都會正常工作。花車表明迴歸。

特別是你可以使用LabelEncoder http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html

>>> from sklearn.ensemble import RandomForestClassifier as RF 
>>> import numpy as np 
>>> X = np.array([[0], [1], [1.2]]) 
>>> y = [0.5, 1.2, -0.1] 
>>> clf = RF() 
>>> clf.fit(X, y) 
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini', 
      max_depth=None, max_features='auto', max_leaf_nodes=None, 
      min_samples_leaf=1, min_samples_split=2, 
      min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1, 
      oob_score=False, random_state=None, verbose=0, 
      warm_start=False) 
>>> print clf.score(y, X) 
Traceback (most recent call last): 
[.....] 
ValueError: continuous is not supported 
>>> y = [0, 1, 2] 
>>> clf.fit(X, y) 
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini', 
      max_depth=None, max_features='auto', max_leaf_nodes=None, 
      min_samples_leaf=1, min_samples_split=2, 
      min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1, 
      oob_score=False, random_state=None, verbose=0, 
      warm_start=False) 
>>> print clf.score(X, y) 
1.0 

或計算.score自己是這是極其平凡函數

print np.mean(clf.predict(X) == y) 
+0

感謝@lejlot - 有趣的使用numpy的意思。 –