python打分函数_⾃定义评分函数RandomForestRegress RandomizedSearchCV中的评分函数将只计算⽹格中指定的每个超参数组合的模型预测数据的得分,测试折叠中平均得分最⾼的超参数获胜。在
它不会以任何⽅式改变RandomForest内部算法的⾏为(当然,除了到超参数)。在
现在你有了best_estimator_(⼀个RandomForestRegressor),到最好的超参数已经设置好了,模型已经根据发送给RandomizedSearchCV的整个数据进⾏了训练(如果你使⽤refit=True,默认情况下是True)。在
所以我不知道你想把那个得分⼿传给模特做什么。best_estimator_模型可以通过使⽤predict()⽅法直接⽤于对新数据的预测。之后,您使⽤的⾃定义评分可以⽤于将预测与实际模型进⾏⽐较。没别的了。在
⼀个简单的例⼦是:from scipy.stats import randint as sp_randint
semble import RandomForestRegressor
del_selection import RandomizedSearchCV, train_test_split
from sklearn.datasets import load_boston
ics import r2_score, make_scorer
X, y = load_boston().data, load_boston().target
X_train, X_test, y_train, y_test = train_test_split(X, y)
clf = RandomForestRegressor()
# Your custom scoring strategy
def my_custom_score(y_true, y_pred):
return r2_score(y_true, y_pred)
# Wrapping it in make_scorer to able to use in RandomizedSearch
my_scorer = make_scorer(my_custom_score)
# Hyper Parameters to be tuned
param_dist = {"max_depth": [3, None],
"max_features": sp_randint(1, 11),
"min_samples_split": sp_randint(2, 11),}
random_search = RandomizedSearchCV(clf, param_distributions=param_dist,
n_iter=20, scoring=my_scorer)
random_search.fit(X_train, y_train)
# Best found parameters set and model trained on X_train, y_train
best_clf = random_search.best_estimator_
# Get predictions on your new data
y_test_pred = best_clf.predict(X_test)
# Calculate your score on the predictions with respect to actual values
random pythonprint(my_custom_score(y_test, y_test_pred))

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。