[英]Reassigning numpy.array()
在下面的代码中,我可以轻松地将数组['a','b','a','c','b','b','c','a']
简化为二进制数组[0 1 0 1 1 1 1 0]
这样'a' -> 0
和'b','c' -> 1
。 如何在不使用for
和if-else
情况下将其转换为三元数组以便'a' -> 0
、 'b' -> 1
、 'c' -> 2
? 谢谢。
import numpy as np
x = np.array(['a', 'b', 'a', 'c', 'b', 'b', 'c', 'a'])
y = np.where(x=='a', 0, 1)
print(y)
通过做:
np.where(x == 'a', 0, (np.where(x == 'b', 1, 2)))
请注意,这会将所有既不是“a”也不是“b”的字符更改为 2。我假设您只有一个包含 a、b 和 c 的数组。
一个更具可扩展性的版本是使用转换字典:
my_dict = {'a':0, 'b':1, 'c':2}
x = np.vectorize(my_dict.get)(x)
output:
[0 1 0 2 1 1 2 0]
另一种方法是:
np.select([x==i for i in ['a','b','c']], np.arange(3))
对于小字典@ypno 的答案会更快。 对于更大的字典,请使用此答案。
时间比较:
三元字母表:
lst = ['a','b','c']
my_dict = {k: v for v, k in enumerate(lst)}
#@Ehsan's solution1
def m1(x):
return np.vectorize(my_dict.get)(x)
#@ypno's solution
def m2(x):
return np.where(x == 'a', 0, (np.where(x == 'b', 1, 2)))
#@SteBog's solution
def m3(x):
y = np.where(x=='a', 0, x)
y = np.where(x=='b', 1, y)
y = np.where(x=='c', 2, y)
return y.astype(np.integer)
#@Ehsan's solution 2 (also suggested by user3483203 in comments)
def m4(x):
return np.select([x==i for i in lst], np.arange(len(lst)))
#@juanpa.arrivillaga's solution suggested in comments
def m5(x):
return np.array([my_dict[i] for i in x.tolist()])
in_ = [np.random.choice(lst, size = n) for n in [10,100,1000,10000,100000]]
对 8 个字母的相同分析:
lst = ['a','b','c','d','e','f','g','h']
import numpy as np
x = np.array(['a', 'b', 'a', 'c', 'b', 'b', 'c', 'a'])
y = np.where(x=='a', 0, x)
y = np.where(x=='b', 1, y)
y = np.where(x=='c', 2, y)
print(y)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.