字符串列表/数组到 numpy 浮点数组

Posted

技术标签:

【中文标题】字符串列表/数组到 numpy 浮点数组【英文标题】:List/Array of strings to numpy float array 【发布时间】:2017-04-29 01:26:33 【问题描述】:

我是 scikit learn 和 numpy 的新手。如何表示由字符串列表/数组组成的数据集,例如

[["aa bb","a","bbb","à"], [bb cc","c","ddd","à"], ["kkk","a","","a"]]

到一个 dtype 浮点数的 numpy 数组?

【问题讨论】:

什么???将字符串转换为浮点数?顺便说一句,它与 sklearn 无关 好吧,也许我没有使用正确的术语,但@datawrestler 理解了我的问题并给出了非常有用的建议。还是谢谢。 【参考方案1】:

我认为您正在寻找的是您的话的数字表示。您可以使用 gensim 并将每个单词映射到一个令牌 id 并从中创建您的 numpy 数组,如下所示:

import numpy as np
from gensim import corpora 

toconvert = [["aa bb","a","bbb","à"], ["bb", "cc","c","ddd","à"], ["kkk","a","","a"]]

# convert your list of lists into token id's. For example, 'aa bb' could be represented as a 2, a as a 1, etc.
tdict = corpora.Dictionary(toconvert)

# given nested structure, you can append nested numpy arrays
newlist = []
for l in toconvert:
    tmplist = []
    for word in l:
        # append to intermediate list the id for the given word under observation
        tmplist.append(tdict.token2id[word])
    # convert to numpy array and append to main list
    newlist.append(np.array(tmplist).astype(float)) # type float

print(newlist) # desired output: [array([ 2.,  0.,  1.,  0.]), array([ 5.,  3.,  4.,  6.,  0.]), array([ 7.,  0.,  8.,  0.])]

# and to see what id's represent which strings:
tdict[0] # 'a'

【讨论】:

感谢@datawrestler 您提供的答案。挺好用的。

以上是关于字符串列表/数组到 numpy 浮点数组的主要内容,如果未能解决你的问题,请参考以下文章

python中的16位浮点共享内存数组

将 CSV 文件读取到 numpy 数组,第一行为字符串,其余为浮点数

Tensorflow - ValueError:无法将 NumPy 数组转换为张量(不支持的对象类型浮点数)

格式为 00:00:00 的 iOS 数组字符串到格式为 00.00.00 的数组浮点型 Objective C

展平 3D NumPy 数组中的内部元组并作为浮点数保存到 CSV

Numpy isnan() 在浮点数组上失败(来自 pandas 数据框应用)