- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有每个样本具有不同权重的数据。在我的申请中,重要的是在估计模型和比较备选模型时考虑这些权重。
我正在使用 sklearn
来估计模型并比较备选超参数选择。但是这个单元测试表明 GridSearchCV
没有应用 sample_weights
来估计分数。
有没有办法让 sklearn
使用 sample_weight
对模型进行评分?
单元测试:
from __future__ import division
import numpy as np
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import log_loss
from sklearn.model_selection import GridSearchCV, RepeatedKFold
def grid_cv(X_in, y_in, w_in, cv, max_features_grid, use_weighting):
out_results = dict()
for k in max_features_grid:
clf = RandomForestClassifier(n_estimators=256,
criterion="entropy",
warm_start=False,
n_jobs=-1,
random_state=RANDOM_STATE,
max_features=k)
for train_ndx, test_ndx in cv.split(X=X_in, y=y_in):
X_train = X_in[train_ndx, :]
y_train = y_in[train_ndx]
w_train = w_in[train_ndx]
y_test = y[test_ndx]
clf.fit(X=X_train, y=y_train, sample_weight=w_train)
y_hat = clf.predict_proba(X=X_in[test_ndx, :])
if use_weighting:
w_test = w_in[test_ndx]
w_i_sum = w_test.sum()
score = w_i_sum / w_in.sum() * log_loss(y_true=y_test, y_pred=y_hat, sample_weight=w_test)
else:
score = log_loss(y_true=y_test, y_pred=y_hat)
results = out_results.get(k, [])
results.append(score)
out_results.update({k: results})
for k, v in out_results.items():
if use_weighting:
mean_score = sum(v)
else:
mean_score = np.mean(v)
out_results.update({k: mean_score})
best_score = min(out_results.values())
best_param = min(out_results, key=out_results.get)
return best_score, best_param
if __name__ == "__main__":
RANDOM_STATE = 1337
X, y = load_iris(return_X_y=True)
sample_weight = np.array([1 + 100 * (i % 25) for i in range(len(X))])
# sample_weight = np.array([1 for _ in range(len(X))])
inner_cv = RepeatedKFold(n_splits=3, n_repeats=1, random_state=RANDOM_STATE)
outer_cv = RepeatedKFold(n_splits=3, n_repeats=1, random_state=RANDOM_STATE)
rfc = RandomForestClassifier(n_estimators=256,
criterion="entropy",
warm_start=False,
n_jobs=-1,
random_state=RANDOM_STATE)
search_params = {"max_features": [1, 2, 3, 4]}
fit_params = {"sample_weight": sample_weight}
my_scorer = make_scorer(log_loss,
greater_is_better=False,
needs_proba=True,
needs_threshold=False)
grid_clf = GridSearchCV(estimator=rfc,
scoring=my_scorer,
cv=inner_cv,
param_grid=search_params,
refit=True,
return_train_score=False,
iid=False) # in this usage, the results are the same for `iid=True` and `iid=False`
grid_clf.fit(X, y, **fit_params)
print("This is the best out-of-sample score using GridSearchCV: %.6f." % -grid_clf.best_score_)
msg = """This is the best out-of-sample score %s weighting using grid_cv: %.6f."""
score_with_weights, param_with_weights = grid_cv(X_in=X,
y_in=y,
w_in=sample_weight,
cv=inner_cv,
max_features_grid=search_params.get(
"max_features"),
use_weighting=True)
print(msg % ("WITH", score_with_weights))
score_without_weights, param_without_weights = grid_cv(X_in=X,
y_in=y,
w_in=sample_weight,
cv=inner_cv,
max_features_grid=search_params.get(
"max_features"),
use_weighting=False)
print(msg % ("WITHOUT", score_without_weights))
产生输出:
This is the best out-of-sample score using GridSearchCV: 0.135692.
This is the best out-of-sample score WITH weighting using grid_cv: 0.099367.
This is the best out-of-sample score WITHOUT weighting using grid_cv: 0.135692.
说明:由于手动计算不带权重的损失会产生与 GridSearchCV
相同的评分,因此我们知道未使用样本权重。
最佳答案
GridSearchCV
将一个scoring
作为输入,它可以被调用。你可以看到如何更改评分功能的详细信息,以及如何传递自己的评分功能here .为了完整起见,以下是该页面的相关代码:
编辑:fit_params 仅传递给拟合函数,而不是评分函数。如果有应该传递给 scorer
的参数,则应将它们传递给 make_scorer
。但这仍然没有解决这里的问题,因为这意味着整个 sample_weight
参数将被传递给 log_loss
,而只有对应于 的部分计算loss时的y_test
应该通过。
sklearn
不支持这样的东西,但您可以使用 padas.DataFrame
破解。好消息是,sklearn
理解 DataFrame
,并保持这种状态。这意味着您可以利用 DataFrame
的 index
,如您在此处的代码中所见:
# more code
X, y = load_iris(return_X_y=True)
index = ['r%d' % x for x in range(len(y))]
y_frame = pd.DataFrame(y, index=index)
sample_weight = np.array([1 + 100 * (i % 25) for i in range(len(X))])
sample_weight_frame = pd.DataFrame(sample_weight, index=index)
# more code
def score_f(y_true, y_pred, sample_weight):
return log_loss(y_true.values, y_pred,
sample_weight=sample_weight.loc[y_true.index.values].values.reshape(-1),
normalize=True)
score_params = {"sample_weight": sample_weight_frame}
my_scorer = make_scorer(score_f,
greater_is_better=False,
needs_proba=True,
needs_threshold=False,
**score_params)
grid_clf = GridSearchCV(estimator=rfc,
scoring=my_scorer,
cv=inner_cv,
param_grid=search_params,
refit=True,
return_train_score=False,
iid=False) # in this usage, the results are the same for `iid=True` and `iid=False`
grid_clf.fit(X, y_frame)
# more code
如您所见,score_f
使用 y_true
的 index
来查找要使用 sample_weight
的哪些部分.为了完整起见,这里是整个代码:
from __future__ import division
import numpy as np
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import log_loss
from sklearn.model_selection import GridSearchCV, RepeatedKFold
from sklearn.metrics import make_scorer
import pandas as pd
def grid_cv(X_in, y_in, w_in, cv, max_features_grid, use_weighting):
out_results = dict()
for k in max_features_grid:
clf = RandomForestClassifier(n_estimators=256,
criterion="entropy",
warm_start=False,
n_jobs=1,
random_state=RANDOM_STATE,
max_features=k)
for train_ndx, test_ndx in cv.split(X=X_in, y=y_in):
X_train = X_in[train_ndx, :]
y_train = y_in[train_ndx]
w_train = w_in[train_ndx]
y_test = y_in[test_ndx]
clf.fit(X=X_train, y=y_train, sample_weight=w_train)
y_hat = clf.predict_proba(X=X_in[test_ndx, :])
if use_weighting:
w_test = w_in[test_ndx]
w_i_sum = w_test.sum()
score = w_i_sum / w_in.sum() * log_loss(y_true=y_test, y_pred=y_hat, sample_weight=w_test)
else:
score = log_loss(y_true=y_test, y_pred=y_hat)
results = out_results.get(k, [])
results.append(score)
out_results.update({k: results})
for k, v in out_results.items():
if use_weighting:
mean_score = sum(v)
else:
mean_score = np.mean(v)
out_results.update({k: mean_score})
best_score = min(out_results.values())
best_param = min(out_results, key=out_results.get)
return best_score, best_param
#if __name__ == "__main__":
if True:
RANDOM_STATE = 1337
X, y = load_iris(return_X_y=True)
index = ['r%d' % x for x in range(len(y))]
y_frame = pd.DataFrame(y, index=index)
sample_weight = np.array([1 + 100 * (i % 25) for i in range(len(X))])
sample_weight_frame = pd.DataFrame(sample_weight, index=index)
# sample_weight = np.array([1 for _ in range(len(X))])
inner_cv = RepeatedKFold(n_splits=3, n_repeats=1, random_state=RANDOM_STATE)
outer_cv = RepeatedKFold(n_splits=3, n_repeats=1, random_state=RANDOM_STATE)
rfc = RandomForestClassifier(n_estimators=256,
criterion="entropy",
warm_start=False,
n_jobs=1,
random_state=RANDOM_STATE)
search_params = {"max_features": [1, 2, 3, 4]}
def score_f(y_true, y_pred, sample_weight):
return log_loss(y_true.values, y_pred,
sample_weight=sample_weight.loc[y_true.index.values].values.reshape(-1),
normalize=True)
score_params = {"sample_weight": sample_weight_frame}
my_scorer = make_scorer(score_f,
greater_is_better=False,
needs_proba=True,
needs_threshold=False,
**score_params)
grid_clf = GridSearchCV(estimator=rfc,
scoring=my_scorer,
cv=inner_cv,
param_grid=search_params,
refit=True,
return_train_score=False,
iid=False) # in this usage, the results are the same for `iid=True` and `iid=False`
grid_clf.fit(X, y_frame)
print("This is the best out-of-sample score using GridSearchCV: %.6f." % -grid_clf.best_score_)
msg = """This is the best out-of-sample score %s weighting using grid_cv: %.6f."""
score_with_weights, param_with_weights = grid_cv(X_in=X,
y_in=y,
w_in=sample_weight,
cv=inner_cv,
max_features_grid=search_params.get(
"max_features"),
use_weighting=True)
print(msg % ("WITH", score_with_weights))
score_without_weights, param_without_weights = grid_cv(X_in=X,
y_in=y,
w_in=sample_weight,
cv=inner_cv,
max_features_grid=search_params.get(
"max_features"),
use_weighting=False)
print(msg % ("WITHOUT", score_without_weights))
代码的输出是:
This is the best out-of-sample score using GridSearchCV: 0.095439.
This is the best out-of-sample score WITH weighting using grid_cv: 0.099367.
This is the best out-of-sample score WITHOUT weighting using grid_cv: 0.135692.
编辑 2:正如下面的评论所说:
the difference in my score and the sklearn score using this solution originates in the way that I was computing a weighted average of scores. If you omit the weighted average portion of the code, the two outputs match to machine precision.
关于python - sklearn GridSearchCV 不在评分函数中使用 sample_weight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49581104/
我知道有几个类似的问题被问到,但我的问题仍然没有得到解答。 问题来了。我使用命令 python3 -m pip3 install -U scikit-learn 来安装 sklearn、numpy 和
_train_weather.values : [[ 0.61818182 0.81645199 0.6679803 ..., 0. 0. 1.
如果我有一个数据集X及其标签Y,那么我将其分为训练集和测试集,scle为0.2,并使用随机种子进行洗牌: 11 >>>X.shape (10000, 50,50) train_data, test_d
首先我查看了所有相关问题。给出了非常相似的问题。 所以我遵循了链接中的建议,但没有一个对我有用。 Data Conversion Error while applying a function to
这里有两种标准化方法: 1:这个在数据预处理中使用:sklearn.preprocessing.normalize(X,norm='l2') 2:分类方法中使用另一种方法:sklearn.svm.Li
所以刚看了一个教程,作者不需要import sklearn使用时 predict anaconda 环境中pickled 模型的功能(安装了sklearn)。 我试图在 Google Colab 中重
我想评估我的机器学习模型。我使用 roc_auc_score() 计算了 ROC 曲线下的面积,并使用 sklearn 的 plot_roc_curve() 函数绘制了 ROC 曲线。在第二个函数中,
我一直在寻找此信息,但在任何地方都找不到,所以这是我的镜头。 我是Python 2.7的初学者,我学习了一个模型,感谢cPickle我保存了它,但现在我想知道是否可以从另一个设备(没有sklearn库
>>> import sklearn.model_selection.train_test_split Traceback (most recent call last): File "", li
在阅读有关使用 python 的 LinearDiscriminantAnalysis 的过程中,我有两种不同的方法来实现它,可在此处获得, http://scikit-learn.org/stabl
我正在使用 sklearn,我注意到 sklearn.metrics.plot_confusion_matrix 的参数和 sklearn.metrics.confusion_matrix不一致。 p
我正在构建一个多标签文本分类程序,我正在尝试使用 OneVsRestClassifier+XGBClassifier 对文本进行分类。最初,我使用 Sklearn 的 Tf-Idf 矢量化来矢量化文本
我想看看模型是否收敛于我的交叉验证。我如何增加或减少 sklearn.svm.SVC 中的时代? 目前: SVM_Model = SVC(gamma='auto') SVM_Model.fit(X_t
有人可以帮助我吗?我很难知道它们之间的区别 from sklearn.model_selection import train_test_split from sklearn.cross_valida
我需要提取在 sklearn.ensemble.BaggingClassifier 中训练的每个模型的概率。这样做的原因是为了估计 XGBoostClassifier 模型的不确定性。 为此,我创建了
无法使用 scikit-learn 0.19.1 导入 sklearn.qda 和 sklearn.lda 我得到: 导入错误:没有名为“sklearn.qda”的模块 导入错误:没有名为“sklea
我正在尝试在 google cloud ai 平台上创建一个版本,但找不到 impute 模块 No module named 'sklearn.impute._base; 'sklearn.impu
我在 PyQt5 中编写了一个 GUI,其中包括以下行 from sklearn.ensemble import RandomForestClassifier 。 遵循this answer中的建议,
我正在做一个 Kaggle 比赛,需要输入一些缺失的数据。我安装了最新的Anaconda(4.5.4)具有所有相关依赖项(即 scikit-learn (0.19.1) )。 当我尝试导入模块时,出现
在安装了所需的模块后,我正在尝试将imblearn导入到我的Python笔记本中。但是,我收到以下错误:。。附加信息:我使用的是一个用Visual Studio代码编写的虚拟环境。。我已经确定venv
我是一名优秀的程序员,十分优秀!