我一直在學習決策樹以及如何在 sklearn 中制作它們。但是當我嘗試過它時,我在所有試圖避免讀取的 vlaue 錯誤中都沒有成功
“具有多個元素的陣列的真值不明確。使用 a.any() 或 a.all()”這里是完整的錯誤:
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_15136/2104431115.py in <module>
2 dt = DecisionTreeRegressor(max_depth= 5, random_state= 1, min_samples_leaf=.1)
3 dt.fit(x_train.reshape(-1,1), y_train.reshape(-1,1))
----> 4 y_pred = dt.predict(x_test, y_test)
~\anaconda3\lib\site-packages\sklearn\tree\_classes.py in predict(self, X, check_input)
465 """
466 check_is_fitted(self)
--> 467 X = self._validate_X_predict(X, check_input)
468 proba = self.tree_.predict(X)
469 n_samples = X.shape[0]
~\anaconda3\lib\site-packages\sklearn\tree\_classes.py in _validate_X_predict(self, X, check_input)
430 def _validate_X_predict(self, X, check_input):
431 """Validate the training data on predict (probabilities)."""
--> 432 if check_input:
433 X = self._validate_data(X, dtype=DTYPE, accept_sparse="csr", reset=False)
434 if issparse(X) and (
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
到目前為止,這是該模型的所有代碼:
x = np.array(bat[["TB_x"]])
y = np.array(bat[["TB_y"]])
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size= .2, random_state= 1)
dt = DecisionTreeRegressor(max_depth= 5, random_state= 1, min_samples_leaf=.1)
dt.fit(x_train.reshape(-1,1), y_train.reshape(-1,1))
y_pred = dt.predict(x_test, y_test)
最初我收到一個錯誤,說它需要一個二維陣列,但得到一個一維陣列,我通過使用 reshape 解決了這個問題,但現在我得到了這個我不明白的值錯誤。
uj5u.com熱心網友回復:
這是對函式如何作業的輕微誤解predict。如果您從概念上考慮它,如果您嘗試預測某些東西,為什么需要傳入預期的標簽?
在DecisionTreeRegressor(以及可能在所有 sklearn 模型中)的簽名中predict,predict(X, check_input=True)您只需要傳入特征,而不是預期的標簽。
您正在做y_pred = dt.predict(x_test, y_test),但期望的第二個引數predict實際上只是一個布林值,允許您禁用一些關于x_test.
您只需要執行以下操作:
y_pred = dt.predict(x_test)
您可以參考sklearn 檔案中的 DecisionTreeRegressor了解更多資訊
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/451584.html
