【问题标题】:Python RecordLinkage - Supervised Machine Learning ErrorPython RecordLinkage - 监督机器学习错误
【发布时间】:2018-02-22 07:04:12
【问题描述】:

我正在使用 python Recordlinkage 库构建一个机器学习模型,其中模型将使用预先匹配的数据进行训练。

下面是代码sn-p:

urltrain = "../Training_Set.data"
namestrain = ['TrueMatchID','System','ID','Col1','Col2']

golden_pair = ps.read_csv(urltrain, names=namestrain)

golden_pair = np.asarray(golden_pair).reshape(5000,5)

golden_pair = ps.DataFrame(golden_pair)

indexer = rl.BlockIndex(on='TrueMatchID')
golden_pair_index = indexer.index(golden_pair)

print(indexer)

# Initialize the classifier
logreg = rl.LogisticRegressionClassifier()
# Train the classifier
logreg.learn(golden_pair.all(), golden_pair_index)

我收到错误:

KeyError:“['TrueMatchID'] 不在索引中”

样本数据:

TrueMatchID   System     ID  Col1    Col2
12345       2            736     1111.1  1111
12345       1            736     1111.4  1111
54321       1            739     2222.3  2222
54321       2            740     2222    2222.4

代码中似乎有什么问题?我对 Python 比较陌生,所以不确定我是否传递了一些错误的论点。

【问题讨论】:

    标签: python record-linkage


    【解决方案1】:

    这里是你写的代码的注解——

    golden_pair = ps.read_csv(urltrain, names=namestrain) # pandas dataframe with column names intact
    
    golden_pair = np.asarray(golden_pair).reshape(5000,5) # converting it to numpy array makes you lose the metadata information of pandas like column names
    
    golden_pair = ps.DataFrame(golden_pair) # here you need to bring back the column names again as it's not present in the numpy array anymore
    

    将最后一行修改为-

    golden_pair = ps.DataFrame(golden_pair, columns=namestrain)
    

    您可以按原样继续使用其余代码:

    indexer = rl.BlockIndex(on='TrueMatchID')
    golden_pair_index = indexer.index(golden_pair)
    
    print(indexer)
    
    # Initialize the classifier
    logreg = rl.LogisticRegressionClassifier()
    # Train the classifier
    logreg.learn(golden_pair.all(), golden_pair_index)
    

    P.S 我不明白你为什么需要重新整形,然后再次将其转换回Dataframe。也许你可以避免这种情况。

    【讨论】:

    • 感谢您的帮助 Vivek。我添加了重塑,因为我收到错误:找到样本数量不一致的输入变量。现在按照您的建议进行修改,即使进行了重塑,我仍然会收到此错误。您也可以解决此错误吗?
    • @Coder 我仍然不明白你为什么首先需要重塑。尝试不使用它。读取csv然后直接去rl.BlockIndex()
    • 是的,Vivek 我刚刚这样做了,但出现错误:发现输入变量的样本数量不一致:[1, 5]。你能帮忙解决这个错误吗?
    猜你喜欢
    • 2016-05-28
    • 2017-02-21
    • 2017-08-21
    • 2014-04-20
    • 2013-03-24
    • 2018-10-17
    • 2021-10-17
    • 2018-09-25
    • 2013-03-09
    相关资源
    最近更新 更多