完成后,我看到 VSCode 在我的虚拟环境中搜索引用.venv,.venvlinux尽管我将其列在“文件:排除”和“文件:观察者排除”设置下
我的 json.settings 文件看起来像这样
{
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/Thumbs.db": true,
"**/.venv": true,
"**/.venvlinux": true
},
"files.watcherExclude": {
"**/.git/objects/**": true,
"**/.git/subtree-cache/**": true,
"**/node_modules/*/**": true,
"**/.hg/store/**": true,
"**/.venv/**": true,
"**/.venvlinux/**": true
},
}
Run Code Online (Sandbox Code Playgroud)
如何阻止 VSCode 在我的虚拟环境中搜索引用?
当我通过交叉验证训练 SVC 时,
y_pred = cross_val_predict(svc, X, y, cv=5, method='predict')
Run Code Online (Sandbox Code Playgroud)
cross_val_predict返回 X 中每个元素的一个类预测,因此y_pred.shape = (1000,)当m=1000. 这是有道理的,因为cv=5SVC 在 X 的不同部分上进行了 5 次训练和验证。在这五次验证中,每一次都对五分之一的实例进行了预测 ( m/5 = 200)。随后,将 5 个向量(每个向量包含 200 个预测)合并为y_pred.
y_pred考虑到所有这些,我使用和 y计算 SVC 的整体精度是合理的。
score = accuracy_score(y, y_pred)
Run Code Online (Sandbox Code Playgroud)
但是(!)cross_val_predict声明的文档:
cross_val_predict 的结果可能与使用 cross_val_score 获得的结果不同,因为元素以不同的方式分组。函数 cross_val_score 取交叉验证折叠的平均值,而 cross_val_predict 只是简单地返回来自几个不同模型的标签(或概率)。因此,cross_val_predict 不是泛化误差的适当度量。
有人可以换句话解释一下,为什么cross_val_predict不适合测量泛化误差,例如 via accuracy_score(y, y_pred)?
编辑:
我首先假设在cv=55 个验证中的每一个都会对 X 的所有实例进行预测。但这是错误的,每次验证仅对 X 的 1/5 实例进行预测。
以下代码cross_validate与结合以GridSearchCV对 iris 数据集上的 SVC 执行嵌套交叉验证。
(以下文档页面的修改示例:https : //scikit-learn.org/stable/auto_examples/model_selection/plot_nested_cross_validation_iris.html#sphx-glr-auto-examples-model-selection-plot-nested-cross-validation-iris -py .)
from sklearn.datasets import load_iris
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV, cross_validate, KFold
import numpy as np
np.set_printoptions(precision=2)
# Load the dataset
iris = load_iris()
X_iris = iris.data
y_iris = iris.target
# Set up possible values of parameters to optimize over
p_grid = {"C": [1, 10],
"gamma": [.01, .1]}
# We will use a Support Vector Classifier with "rbf" kernel
svm = SVC(kernel="rbf")
# …Run Code Online (Sandbox Code Playgroud) 我尝试按照扩展的自述文件中给出的说明进行操作。我使用 Windows 并打开我的笔记本,我使用存储在目录中的 jupyter-notebook.exe
..\Anaconda3\脚本
在 Anaconda3 目录中,我转到子目录
Anaconda3\Lib\site-packages\jupyter_contrib_nbextensions\nbextensions\snippets
并更改文件“snippets.json”的代码
{
"snippets" : [
{
"name" : "example",
"code" : [
"# This is an example snippet!",
"# To create your own, add a new snippet block to the",
"# snippets.json file in your jupyter data directory under nbextensions:",
"# $(jupyter --data-dir)/nbextensions/snippets/snippets.json",
"import this"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
到
{
"snippets" : [
{
"name" : "example",
"code" : [
"# This is a test if something changed",
]
] …Run Code Online (Sandbox Code Playgroud)