ランダムフォレスト(分類ではなく回帰分析)で学習したモデルの精度(score)を出力しようとしたらエラーになります。
エラーメッセージを見ても、どうもよく原因がよくわかりません。
お詳しい方、ご指導をお願いいたします。
#ランダムフォレストで回帰分析の精度を出力 y_pred = clf.predict(x_test_std) predicted = y_pred y_test = np.array(y_test).ravel() print(y_test) print(predicted) clf.score(y_test,predicted)
以下、実行結果(エラーです)
[0 1 1 0 1 1 1 1 1 0 1 0 1 1 1 0 1 1 1 0 1 0 1 1 1 1 1 1 1 1 0] [0.975 0.91584249 0.82605159 0.53488456 0.93512821 0.92654762 0.99237237 0.68333333 0.62357143 0.88071429 1. 0.92142857 0.92527778 0.75166667 0.95844105 0.29666667 0.97058391 1. 0.991 0.63833333 0.95844105 0.46742424 0.90916667 0.7 0.99237237 0.90989011 0.99237237 0.53333333 0.78626984 0.91584249 0.50992424] --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-28-e28a870dc070> in <module> 9 print(predicted) 10 ---> 11 clf.score(y_test,predicted) ~\Anaconda3\lib\site-packages\sklearn\base.py in score(self, X, y, sample_weight) 406 from .metrics import r2_score 407 from .metrics.regression import _check_reg_targets --> 408 y_pred = self.predict(X) 409 # XXX: Remove the check in 0.23 410 y_type, _, _, _ = _check_reg_targets(y, y_pred, None) ~\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py in predict(self, X) 691 check_is_fitted(self, 'estimators_') 692 # Check data --> 693 X = self._validate_X_predict(X) 694 695 # Assign chunk of trees to jobs ~\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py in _validate_X_predict(self, X) 357 "call `fit` before exploiting the model.") 358 --> 359 return self.estimators_[0]._validate_X_predict(X, check_input=True) 360 361 @property ~\Anaconda3\lib\site-packages\sklearn\tree\tree.py in _validate_X_predict(self, X, check_input) 389 """Validate X whenever one tries to predict, apply, predict_proba""" 390 if check_input: --> 391 X = check_array(X, dtype=DTYPE, accept_sparse="csr") 392 if issparse(X) and (X.indices.dtype != np.intc or 393 X.indptr.dtype != np.intc): ~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator) 519 "Reshape your data either using array.reshape(-1, 1) if " 520 "your data has a single feature or array.reshape(1, -1) " --> 521 "if it contains a single sample.".format(array)) 522 523 # in the future np.flexible dtypes will be handled like object dtypes ValueError: Expected 2D array, got 1D array instead: array=[0. 1. 1. 0. 1. 1. 1. 1. 1. 0. 1. 0. 1. 1. 1. 0. 1. 1. 1. 0. 1. 0. 1. 1. 1. 1. 1. 1. 1. 1. 0.]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
回答1件
あなたの回答
tips
プレビュー