首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >GridSearchCV教程中使用的skLearn慢速参数

GridSearchCV教程中使用的skLearn慢速参数
EN

Data Science用户
提问于 2018-11-25 20:18:42
回答 1查看 1.2K关注 0票数 0

我不知道为什么grid.fit(X,y)是正确的,而不是grid.fit(X_2d, y_2d)

在这篇关于径向基函数支持向量机参数的教程中,我们使用GridSearchCV来寻找支持向量机的最佳超参数。

它们有以下代码:

代码语言:javascript
运行
复制
# Dataset for decision function visualization: we only keep the first 
two
# features in X and sub-sample the dataset to keep only 2 classes and
# make it a binary classification problem.

X_2d = X[:, :2]
X_2d = X_2d[y > 0]
y_2d = y[y > 0]
y_2d -= 1

..。

代码语言:javascript
运行
复制
param_grid = dict(gamma=gamma_range, C=C_range)

# GridSearchCV will search the parameter space for the best parameters to use, minimizing the score function
cv = StratifiedShuffleSplit(n_splits=5, test_size=0.2, random_state=42)

grid = GridSearchCV(SVC(), param_grid=param_grid, cv=cv)

# ==================== CODE I'M INTERESTED IN  ==================>
# ===== SWITCH `grid.fit(X,y)` with grid.fit(X_2d, y_2d) ========>
grid.fit(X, y)
# ==================== ^^^^^^^^^^^^^^^^^^^  =============>

print("The best parameters are %s with a score of %0.2f"
      % (grid.best_params_, grid.best_score_))

其中X_2dy_2dXy的子集。

只是一些说明Xy是什么的信息:

代码语言:javascript
运行
复制
print(X.shape)    #(150,4)
print(y.shape)    #(150,)
print(X_2d.shape) #(100,2)
print(y_2d.shape) #(100,)
print(type(X))    #<class 'numpy.ndarray'>
print(type(y))    #<class 'numpy.ndarray'>
print(type(X_2d)) #<class 'numpy.ndarray'>
print(type(y_2d)) #<class 'numpy.ndarray'>

为什么将上面的代码更改为grid.fit(X_2d, y_2d)不起作用?我不确定是不是花了很长时间,或者说它不正确。我的木星笔记本只是坐着,而grid.fit(X, y)只需要几秒钟。

我最初的想法是,我们希望适应我们正在运行的实际数据集,即X_2dy_2d,而不是Xy

EN

回答 1

Data Science用户

发布于 2018-11-26 03:30:47

如果您不确定代码是否有效,可以打开网格搜索输出:

代码语言:javascript
运行
复制
grid = GridSearchCV(SVC(), param_grid=param_grid, cv=cv, verbose=2)

我只是在你的链接中运行脚本-它对我来说很好。您确定在处理代码时没有更改某些内容吗?

票数 1
EN
页面原文内容由Data Science提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://datascience.stackexchange.com/questions/41686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档