如何将机器学习程序的输出存储在键,值对的csv文件中

时间:2018-11-05 04:45:21

标签: python machine-learning

这是我的以下代码,classify(),最后将输入作为标签并给出相应的输出。

ERROR_THRESHOLD = 0.25


def classify(sentence):
    # generate probabilities from the model
    results = model.predict([bow(sentence, words)])[0]
    # filter out predictions below a threshold
    results = [[i,r] for i,r in enumerate(results) if r>ERROR_THRESHOLD]
    # sort by strength of probability
    results.sort(key=lambda x: x[1], reverse=True)
    return_list = []

    for r in results:
        return_list.append((classes[r[0]], r[1]))

    # return tuple of intent and probability
    return return_list


def response(sentence, userID='123', show_details=False):
    results = classify(sentence)
    # if we have a classification then find the matching intent tag
    if results:
        # loop as long as there are matches to process
        while results:                     
            for i in intents['intents']:
                # find a tag matching the first result
                if i['tag'] == results[0][0]:
                    # a random response from the intent
                    return print(random.choice(i['response']))                    
            results.pop(0)

classify('MEDTRONIK')

1 个答案:

答案 0 :(得分:0)

不确定您到底想要什么,您的代码还不足以澄清甚至完全理解。

我假设在classify()中,您试图计算一个预期类与计算类的表(这分别是键和值的意思吗?)。再次假设这些类是可哈希的(如果您想将它们视为键,按照术语“键”在python中的含义以及“键值对”所隐含的上下文),则可以直接编写{{1} }到使用csvwriter的csv文件中,该文件可以直接将此列表作为参数。如果您确实需要key:值对,则可以使用具有相同名称的内置https://docs.python.org/3/library/stdtypes.html#dict将此列表转换为return_list

但是您正在使用的ML /数字计算库应该提供一种推荐/更好的方式,例如: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html TF数据帧也具有to_csv方法。