Tar*_*lia 8 python scikit-learn cross-validation
我想使用一个自定义函数,cross_validate它使用特定y_test的计算精度,这y_test与实际目标不同y_test。
我尝试了几种方法,make_scorer但我不知道如何实际通过我的替代方法y_test:
scoring = {'prec1': 'precision',
'custom_prec1': make_scorer(precision_score()}
scores = cross_validate(pipeline, X, y, cv=5,scoring= scoring)
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议一种方法吗?
找到了这个方法。也许代码不是最佳的,对此感到抱歉。
好的,让我们开始:
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
from sklearn.metrics.scorer import make_scorer
xTrain = np.random.rand(100, 100)
yTrain = np.random.randint(1, 4, (100, 1))
yTrainCV = np.random.randint(1, 4, (100, 1))
model = LogisticRegression()
Run Code Online (Sandbox Code Playgroud)
yTrainCV 将在此处用作自定义记分器。
def customLoss(xArray, yArray):
indices = xArray.index.values
tempArray = [1 if value1 != value2 else 0 for value1, value2 in zip(xArray.values, yTrainCV[[indices]])]
return sum(tempArray)
scorer = {'main': 'accuracy',
'custom': make_scorer(customLoss, greater_is_better=True)}
Run Code Online (Sandbox Code Playgroud)
这里有几个技巧:
greater_is_better:True/False将返回正数或负数GridSearchCV和...
grid = GridSearchCV(model,
scoring=scorer,
cv=5,
param_grid={'C': [1e0, 1e1, 1e2, 1e3],
'class_weight': ['balanced', None]},
refit='custom')
grid.fit(xTrain, pd.DataFrame(yTrain))
print(grid.score(xTrain, pd.DataFrame(yTrain)))
Run Code Online (Sandbox Code Playgroud)
refit参数GridSearchCVDataFrame在这里传递目标数组- 这将帮助我们检测自定义损失函数中的索引| 归档时间: |
|
| 查看次数: |
2948 次 |
| 最近记录: |