我的问题是,当我在第一行注释掉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
,这段代码是无法工作(甚至无法运行)的。