管道在sklearn python中给出不同的答案

时间:2016-12-18 15:47:40

标签: python machine-learning scikit-learn artificial-intelligence logistic-regression

我写了两个应该遵循相同逻辑的程序。但他们俩都给出了不同的答案。

首先 -

train_data = train_features[:1710][:]
train_label = label_features[:1710][:].ravel()
test_data = train_features[1710:][:]
test_label = label_features[1710:][:].ravel()

def getAccuracy(ans):
    d = 0
    for i in range(np.size(ans,0)):
        if(ans[i] == test_label[i]):
            d+=1
    return (d*100)/float(np.size(ans,0))

estimators = [('pps', pps.RobustScaler()), ('clf', LogisticRegression())]
pipe = Pipeline(estimators)
pipe = pipe.fit(train_data,train_label)

ans = pipe.predict(test_data)
getAccuracy(ans)

二 -

train_data = train_features[:1710][:]
train_label = label_features[:1710][:].ravel()
test_data = train_features[1710:][:]
test_label = label_features[1710:][:].ravel()

def getAccuracy(ans):
    d = 0
    for i in range(np.size(ans,0)):
        if(ans[i] == test_label[i]):
            d+=1
    return (d*100)/float(np.size(ans,0))

def preprocess(features):
    return pps.RobustScaler().fit_transform(features)

train_data = preprocess(train_data)
clf = LogisticRegression().fit(train_data,train_label)

test_data = preprocess(test_data)
ans = clf.predict(test_data)
getAccuracy(ans)

第一个给出80.81,第二个给出84.92。为什么两者都不同?

1 个答案:

答案 0 :(得分:2)

您的第二个代码无效,因为您的“预处理”适合缩放器到测试集,这不应该发生。另一方面,Pipeline只能将RobustScaler与您的列车数据相匹配,然后在测试数据上调用“转换”。