質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

960閲覧

ランダムフォレスト(回帰分析)の精度を出力しようとすると沢山エラーが。。。

python_2019

総合スコア68

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/05/29 14:39

ランダムフォレスト(分類ではなく回帰分析)で学習したモデルの精度(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.

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

meg_

2020/05/29 16:24

モジュールのバージョンは何ですか?
python_2019

2020/05/29 16:41

ご連絡ありがとうございます。 モジュールのバージョンは、どこを見ればよいのでしょうか? 基本的な事をお聞きして申し訳ございません。
meg_

2020/05/30 02:34

print(モジュール名.__version__)で表示されるかと思います。
guest

回答1

0

ベストアンサー

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.

エラーメッセージの指示に従ってy_test.reshape(-1, 1)すれば良いかと思います。

投稿2020/05/30 02:41

meg_

総合スコア10580

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

python_2019

2020/05/30 07:27

ご回答ありがとうございます。 一度、ご指摘頂いた方法で試してみたいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問