k折交叉验证测试可以独立获得结果吗?

我的问题是,当我在第一行注释掉pipe_lr时,我仍然能得到准确率得分。这让我很困惑,我认为它应该报错,为什么呢?看起来它不需要pipe_lr的定义。这是相关部分。 输入图片描述

from sklearn.ensemble import ExtraTreesClassifierfrom sklearn.feature_selection import SelectFromModelimport pandas as pdimport numpy as npfrom sklearn.cross_validation import train_test_splitfrom sklearn.preprocessing import MinMaxScalerfrom sklearn.preprocessing import LabelEncoderfrom sklearn.pipeline import Pipelinefrom sklearn.tree import DecisionTreeClassifierfrom sklearn.cross_validation import StratifiedKFoldpipe_lr=Pipeline([('normalization',mms),                  ('feature_selection',feature_selection),                  ('classification',DecisionTreeClassifier())])"""K-fold test"""kfold=StratifiedKFold(y=y_train,n_folds=10,random_state=1)scores=[]features=[]for k,(train,test) in enumerate (kfold):    pipe_lr.fit(X_train[train],y_train[train])    score=pipe_lr.score(X_train[test],y_train[test])    scores.append(score)    print('Fold: %s, Class dist.: %s,Acc: %.3f' %(k+1,            np.bincount(y_train[train]), score))

回答:

简短的回答是不,它不能。没有pipe_lr这一行,你的代码将无法运行。之所以能在没有它的情况下运行,唯一的原因是你在一个特殊的环境中运行它,比如ipython笔记本,并且之前你已经运行了包含这一行的相同代码,因此它仍然保留在你的内存中,在相同的作用域内(因此第二次运行仍然可以访问原始的pipe_lr对象)。或者你实际上在代码的其他地方定义了这个变量(例如,在定义数据、mms等的代码块中,而你从问题中删除了这些部分)。在正确运行于干净环境中时,没有pipe_lr,这段代码是无法工作(甚至无法运行)的。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注