Python 属性错误:';DecisionTreeRegressor';对象没有属性';损失';
我正在调整Python 属性错误:';DecisionTreeRegressor';对象没有属性';损失';,python,scikit-learn,Python,Scikit Learn,我正在调整DecisionTreeRegressor的超参数,如下所示 from sklearn.tree import DecisionTreeRegressor from sklearn.model_selection import train_test_split from sklearn.model_selection import GridSearchCV import numpy as np import tensorflow as tf from sklearn.datasets
DecisionTreeRegressor
的超参数,如下所示
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
import numpy as np
import tensorflow as tf
from sklearn.datasets import fetch_california_housing
from sklearn.metrics import f1_score, make_scorer
housing = fetch_california_housing()
seed1 = 7
seed2 = 5
seed3 = 42
X_train_full, X_test, y_train_full, y_test = train_test_split(housing.data, housing.target, random_state=seed1)
X_train, X_valid, y_train, y_valid = train_test_split(X_train_full, y_train_full, random_state=seed2)
def build_model(max_depth=3, min_samples_leaf=3, max_leaf_nodes=4):
clf = DecisionTreeRegressor(max_depth,
min_samples_leaf,
max_leaf_nodes, random_state=seed3)
return clf
keras_clf = tf.keras.wrappers.scikit_learn.KerasRegressor(build_model)
param_spec = {
"max_depth": np.arange(2,10).tolist(),
"min_samples_leaf": np.arange(1,100).tolist(),
"max_leaf_nodes": np.arange(1,5).tolist()
}
f1_scorer = make_scorer(f1_score)
rnd_search_cv = GridSearchCV(keras_clf, param_spec, scoring=f1_scorer, cv=3, n_jobs=-1, verbose=1)
rnd_search_cv.fit(X_train, y_train, epochs=50,
validation_data=(X_valid, y_valid))
print(rnd_search_cv.best_params_)
print(rnd_search_cv.best_score_)
但会引发以下错误:
if (losses.is_categorical_crossentropy(self.model.loss) and
AttributeError: 'DecisionTreeRegressor' object has no attribute 'loss'
我没有在我的模型中指定任何损失,而且,好吧,我不必在这个场景中设置一个,在我的
KerasRegressor
中设置评分。那么,如何解决这个问题呢?您似乎使用Keras的scikit learn包装器来包装决策树。这是没有必要的(实际上是错误的)。。。只需将build\u model
返回的决策树直接传递到GridSearchCV
。