【问题标题】:How to do I add strings to ClassificationDataSet?如何将字符串添加到 ClassificationDataSet?
【发布时间】:2016-12-05 19:08:45
【问题描述】:

如何在 pybrain.datasets.addSample() 中使用字符串构建数据集?我收到一条错误消息,上面写着“无法将字符串转换为浮点数:gas”。

我是否遗漏了某些内容,例如索引值或输入和目标之间定义的链接?我不确定如何阅读有关此的文档。感谢您的帮助。

import pybrain
from pybrain.datasets import ClassificationDataSet

#set up input and target variables
ds = ClassificationDataSet(inp=2, target=1)

#add data to dataset
ds.addSample(('gas', 'blue'), ('car',))
ds.addSample(('desiel', 'brown'), ('truck',))

# error
ValueError: could not convert string to float: gas

【问题讨论】:

    标签: python pybrain


    【解决方案1】:

    看起来 pybrain 只使用浮点类型。因此,您可能希望为每个唯一的字符串变量创建一个唯一的浮点值。对于元组中的每个字符串,也许将 ord() 函数应用于字符串中的每个字符。最佳实践是使用列表理解语句而不是 map() 和 lambda 函数。

    >>> ord('a')
    97
    >>> ord('\u00c2')
    192
    

    或者喜欢

    >>> [ord(c) for c in 'Hello World!']
    [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]
    

    所以可能是这样的:

    >>>x = [('gas', 'blue'),]
    
    >>>for var in x:
    >>>    # for each letter of word
    >>>    for c in var:
    >>>        # list of ord() values for each letter of word
    >>>        letter = [ord(i) for i in c]
    >>>        # convert list to string
    >>>        number = [str(i) for i in letter]
    >>>        # join() to combine list into a single string
    >>>        word = ''.join(number)
    >>>        print c, word
    gas 10397115
    blue 98108117101
    

    将字符串表示为浮点类型以及使用自然语言工具包来表示单词的出现可能有助于准备数据以训练神经网络模型。

    Python3 convert Unicode String to int representation

    https://stackoverflow.com/questions/36680250/pybrain-neural-network-nominal-string-inputs

    https://datascience.stackexchange.com/questions/869/neural-network-parse-string-data

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-09
      • 2023-04-03
      • 2020-04-15
      • 2018-02-18
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2012-04-21
      相关资源
      最近更新 更多