什么是 _passthrough_scorer 以及如何在 GridsearchCV (sklearn) 中更改记分器?
What is _passthrough_scorer and How Can I Change Scorers in GridsearchCV (sklearn)?
http://scikit-learn.org/stable/modules/generated/sklearn.grid_search.GridSearchCV.html(供参考)
x = [[2], [1], [3], [1] ... ] # about 1000 data
grid = GridSearchCV(KernelDensity(), {'bandwidth': np.linspace(0.1, 1.0, 10)}, cv=10)
grid.fit(x)
当我使用 GridSearchCV 而没有像 那样指定评分函数时,grid.scorer_ 的值为 。你能解释一下 _passthrough_scorer 是什么函数吗?
除此之外,我想将评分功能更改为mean_squared_error或其他。
grid = GridSearchCV(KernelDensity(), {'bandwidth': np.linspace(0.1, 1.0, 10)}, cv=10, scoring='mean_squared_error')
但是 grid.fit(x) 行总是给我这个错误信息:
TypeError: __call__() missing 1 required positional argument: 'y_true'
我不知道如何将 y_true 赋给函数,因为我不知道真正的分布。你能告诉我如何更改评分功能吗?感谢您的帮助。
KernelDensity 的默认指标是 minkowski
,其中 p=2 是欧氏指标。如果您不分配任何其他评分方法,GridSearchCV 将使用 KernelDensity 指标进行评分。
均方误差的公式为:sum((y_true - y_estimated)^2)/n。你得到了错误,因为你需要有一个 y_true
来计算它。
这是一个将 GridSearchCV 应用于 KernelDensity 的虚构示例:
from sklearn.neighbors import KernelDensity
from sklearn.grid_search import GridSearchCV
import numpy as np
N = 20
X = np.concatenate((np.random.randint(0, 10, 50),
np.random.randint(5, 10, 50)))[:, np.newaxis]
params = {'bandwidth': np.logspace(-1.0, 1.0, 10)}
grid = GridSearchCV(KernelDensity(), params)
grid.fit(X)
print(grid.grid_scores_)
print('Best parameter: ',grid.best_params_)
print('Best score: ',grid.best_score_)
print('Best estimator: ',grid.best_estimator_)
输出为:
[mean: -96.94890, std: 100.60046, params: {'bandwidth': 0.10000000000000001},
mean: -70.44643, std: 40.44537, params: {'bandwidth': 0.16681005372000587},
mean: -71.75293, std: 18.97729, params: {'bandwidth': 0.27825594022071243},
mean: -77.83446, std: 11.24102, params: {'bandwidth': 0.46415888336127786},
mean: -78.65182, std: 8.72507, params: {'bandwidth': 0.774263682681127},
mean: -79.78828, std: 6.98582, params: {'bandwidth': 1.2915496650148841},
mean: -81.65532, std: 4.77806, params: {'bandwidth': 2.1544346900318834},
mean: -86.27481, std: 2.71635, params: {'bandwidth': 3.5938136638046259},
mean: -95.86093, std: 1.84887, params: {'bandwidth': 5.9948425031894086},
mean: -109.52306, std: 1.71232, params: {'bandwidth': 10.0}]
Best parameter: {'bandwidth': 0.16681005372000587}
Best score: -70.4464315885
Best estimator: KernelDensity(algorithm='auto', atol=0, bandwidth=0.16681005372000587,
breadth_first=True, kernel='gaussian', leaf_size=40,
metric='euclidean', metric_params=None, rtol=0)
GridSeachCV 的有效评分方法通常需要y_true。在您的情况下,您可能希望将 sklearn.KernelDensity
的指标更改为其他指标(例如 sklearn.metrics.pairwise.pairwise_kernels
、sklearn.metrics.pairwise.pairwise_distances
),因为网格搜索将使用它们进行评分。
http://scikit-learn.org/stable/modules/generated/sklearn.grid_search.GridSearchCV.html(供参考)
x = [[2], [1], [3], [1] ... ] # about 1000 data
grid = GridSearchCV(KernelDensity(), {'bandwidth': np.linspace(0.1, 1.0, 10)}, cv=10)
grid.fit(x)
当我使用 GridSearchCV 而没有像 那样指定评分函数时,grid.scorer_ 的值为 。你能解释一下 _passthrough_scorer 是什么函数吗?
除此之外,我想将评分功能更改为mean_squared_error或其他。
grid = GridSearchCV(KernelDensity(), {'bandwidth': np.linspace(0.1, 1.0, 10)}, cv=10, scoring='mean_squared_error')
但是 grid.fit(x) 行总是给我这个错误信息:
TypeError: __call__() missing 1 required positional argument: 'y_true'
我不知道如何将 y_true 赋给函数,因为我不知道真正的分布。你能告诉我如何更改评分功能吗?感谢您的帮助。
KernelDensity 的默认指标是 minkowski
,其中 p=2 是欧氏指标。如果您不分配任何其他评分方法,GridSearchCV 将使用 KernelDensity 指标进行评分。
均方误差的公式为:sum((y_true - y_estimated)^2)/n。你得到了错误,因为你需要有一个 y_true
来计算它。
这是一个将 GridSearchCV 应用于 KernelDensity 的虚构示例:
from sklearn.neighbors import KernelDensity
from sklearn.grid_search import GridSearchCV
import numpy as np
N = 20
X = np.concatenate((np.random.randint(0, 10, 50),
np.random.randint(5, 10, 50)))[:, np.newaxis]
params = {'bandwidth': np.logspace(-1.0, 1.0, 10)}
grid = GridSearchCV(KernelDensity(), params)
grid.fit(X)
print(grid.grid_scores_)
print('Best parameter: ',grid.best_params_)
print('Best score: ',grid.best_score_)
print('Best estimator: ',grid.best_estimator_)
输出为:
[mean: -96.94890, std: 100.60046, params: {'bandwidth': 0.10000000000000001},
mean: -70.44643, std: 40.44537, params: {'bandwidth': 0.16681005372000587},
mean: -71.75293, std: 18.97729, params: {'bandwidth': 0.27825594022071243},
mean: -77.83446, std: 11.24102, params: {'bandwidth': 0.46415888336127786},
mean: -78.65182, std: 8.72507, params: {'bandwidth': 0.774263682681127},
mean: -79.78828, std: 6.98582, params: {'bandwidth': 1.2915496650148841},
mean: -81.65532, std: 4.77806, params: {'bandwidth': 2.1544346900318834},
mean: -86.27481, std: 2.71635, params: {'bandwidth': 3.5938136638046259},
mean: -95.86093, std: 1.84887, params: {'bandwidth': 5.9948425031894086},
mean: -109.52306, std: 1.71232, params: {'bandwidth': 10.0}]
Best parameter: {'bandwidth': 0.16681005372000587}
Best score: -70.4464315885
Best estimator: KernelDensity(algorithm='auto', atol=0, bandwidth=0.16681005372000587,
breadth_first=True, kernel='gaussian', leaf_size=40,
metric='euclidean', metric_params=None, rtol=0)
GridSeachCV 的有效评分方法通常需要y_true。在您的情况下,您可能希望将 sklearn.KernelDensity
的指标更改为其他指标(例如 sklearn.metrics.pairwise.pairwise_kernels
、sklearn.metrics.pairwise.pairwise_distances
),因为网格搜索将使用它们进行评分。