简体   繁体   English

如何将字符串的 ndarray 列表转换为浮点数

[英]How to convert a list of ndarray of strings into floats

How could one map aa list of ndarrays containing string objects into specific floats ?如何将包含字符串对象的 ndarray 列表映射到特定的浮点数? For instance, if the user decides to map orange to 1.0 and grapefruit to 2.0 ?例如,如果用户决定将orange映射到 1.0 并将grapefruit映射到 2.0 ?

myList = [np.array([['orange'], ['orange'], ['grapefruit']], dtype=object), np.array([['orange'], ['grapefruit'], ['orange']], dtype=object)] 

So one would have:所以一个人会有:

convList = [np.array([['1.0'], ['1.0'], ['2.0']], dtype=float), np.array([['1.0'], ['2.0'], ['1.0']], dtype=float)]

I tried to implement this function:我试图实现这个功能:

def map_str_to_float(iterator):
    d = {}
    for ndarr in iterator:
        for string_ in ndarr:
            d[string_] = float(input('Enter your map for {}: '.format(string_)))
    return d

test = map_str_to_float(myList)
print(test)

But I get the following error:但我收到以下错误:

d[string_] = float(input('Enter your map for {}: '.format(string_)))
TypeError: unhashable type: 'numpy.ndarray'

I believe it's because the type of string_ is a numpy array instead of a string...我相信这是因为string_的类型是一个 numpy 数组而不是字符串......

With that nested loop you will ask the user for an input 6 times (but you have 2 values grapefruit and orange ).使用该嵌套循环,您将要求用户输入 6 次(但您有 2 个值grapefruitorange )。 I would suggest you to get the unique values first and ask for just unique values:我建议您先获取唯一值,然后仅要求唯一值:

To do so:这样做:

unique_values = np.unique(np.array(myList))

Now as the user for each unique value for a number:现在作为一个数字的每个唯一值的用户:

d = {}

for unique_value in unique_values:
    d[unique_value] = float(input(f"give me a number for {unique_value} ")) 

Now you got your map in variable d .现在你在变量d得到了你的地图。

Update after a comment评论后更新

Then you can write your own unique method.然后你可以编写自己独特的方法。 Please notice the code below would get all unique values regardless of the length of it as long as it's 1D.请注意,只要是一维的,无论长度如何,下面的代码都会获得所有唯一值。

unique_values = []
for each_ndarray in myList:
    for value in each_ndarray:
        if not value[0] in unique_values:
            unique_values.append(value[0])

For the error, on debugging string_ is an array ['orange'], cant be key of dictionary对于错误,调试时 string_ 是一个数组 ['orange'],不能是字典的键

As for How to convert a list of ndarray of strings into floats We use indices, get the indices of strings, and use those indices to print required new indices in same order.至于如何将字符串的 ndarray 列表转换为浮点数我们使用索引,获取字符串的索引,并使用这些索引以相同的顺序打印所需的新索引。 Basically np.array([1, 2])[0, 1, 0, 0] will give new array of size 4 with entries in order of indices.基本上np.array([1, 2])[0, 1, 0, 0]将给出大小为 4 的新数组,其中包含按索引顺序排列的条目。 Same logic will apply which will skip dictionary mapping in python.将应用相同的逻辑,这将跳过 python 中的字典映射。 Mapping operation will happen through indices in C, so should be fast.映射操作将通过 C 中的索引发生,所以应该很快。

Comments should explain what happens评论应该解释发生了什么

import numpy as np

dataSet = np.array(['kevin', 'greg', 'george', 'kevin'], dtype='U21')

# Get all the unique strings, and their indices
# Values of indices are based on uniques ordering
uniques, indices = np.unique(dataSet, return_inverse=True)
# >>> uniques
# array(['george', 'greg', 'kevin'], dtype='<U21')
# >>> indices
# array([2, 1, 0, 2])
# Originial array
# >>> uniques[indices]
# array(['kevin', 'greg', 'george', 'kevin'], dtype='<U21')

new_indices = np.array([float(input()) for e in uniques])

# Get new indices indexed using original positions of unique strings in numpy array
print(new_indices[indices])

# You can do the same for multi dimensional arrays

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM