数値データ(浮動小数点数)で基本的な機械学習アルゴリズムを実行しようとしていますが、私はpython 2.7、sklearn、pandas、そしてjupyter(ipythonノートブック)を使って作業しています。最初のパスでは、基本的なランダムフォレスト検索を使用しようとしていましたが、関数を使用するときはValueErrorを取得しています。sklearn pandasのデータフレームのデータを使用してバリューエラーを回避する方法が見つかりません
データがデータフレーム
df = pd.read_table("p12.dat", delim_whitespace=True)
df.dtypesに読み込まれたが、目的の値がすべてのfloat64であることを示しています。エラーがある
outcome_var = 'ki'
predictor_var = ['Etot','TSs','Eww-nbr']
model = RandomForestClassifier(n_estimators=100)
classification_model(model, df,predictor_var,outcome_var)
:
ValueError Traceback (most recent call last)
<ipython-input-9-7dd611cce611> in <module>()
----> 1 classification_model(model, df,predictor_var,outcome_var)
<ipython-input-8-956e572db2f4> in classification_model(model, data, predictors, outcome)
1 def classification_model(model, data, predictors, outcome):
2 #Fit the model:
----> 3 model.fit(data[predictors],data[outcome].values)
4
5 #Make predictions on training set:
/Users/jonathangough/anaconda/lib/python2.7/site-packages/sklearn/ensemble/forest.pyc in fit(self, X, y, sample_weight)
233 self.n_outputs_ = y.shape[1]
234
--> 235 y, expanded_class_weight = self._validate_y_class_weight(y)
236
237 if getattr(y, "dtype", None) != DOUBLE or not y.flags.contiguous:
/Users/jonathangough/anaconda/lib/python2.7/site-packages/sklearn/ensemble/forest.pyc in _validate_y_class_weight(self, y)
419
420 def _validate_y_class_weight(self, y):
--> 421 check_classification_targets(y)
422
423 y = np.copy(y)
/Users/jonathangough/anaconda/lib/python2.7/site-packages/sklearn/utils/multiclass.pyc in check_classification_targets(y)
171 if y_type not in ['binary', 'multiclass', 'multiclass-multioutput',
172 'multilabel-indicator', 'multilabel-sequences']:
--> 173 raise ValueError("Unknown label type: %r" % y)
174
175
ValueError: Unknown label type: array([[ 2.72000000e+00],
[ 4.60000000e+00],
[ 6.70000000e+00],
[ 2.30000000e+00],
[ 1.70000000e+00],
[ 2.20000000e+00],
[ 3.60000000e+00],
[ 9.10000000e+00],
私は検索とグーグルが、私いる
def classification_model(model, data, predictors, outcome):
model.fit(data[predictors],data[outcome])
predictions = model.predict(data[predictors])
accuracy = metrics.accuracy_score(predictions,data[outcome])
print "Accuracy : %s" % "{0:.3%}".format(accuracy)
print "Cross-Validation Score : %s" % "{0:.3%}".format(np.mean(error))
model.fit(data[predictors],data[outcome])
して、変数を設定し、実行:基本的な表現を使用して
何をすべきか分かりません。私の理解は、ランダムフォレストアルゴリズムで浮動小数点数を使うことができるということでした(私はそれがエラーだとは思わなかった)。
洞察力があれば幸いです。
sklearnモデルの入力としてパンダの基礎となるnumpy配列を試してみてください。あなたのコードでは、データ[predictors]、data [outcome]の.valuesを持っていませんが、stacktraceではデータ[outcome] .valuesがあります。すべての入力に「.values」が付いていることを確認してください。 – DataSwede