1 回答
TA贡献2041条经验 获得超4个赞
当您想为数组的每个唯一值分配一个随机整数值时,您可以将唯一值映射到字典。
更新
我们需要检查随机数是否用作映射器的值。
from random import randint
import numpy as np
# input
X = np.array([['a', 'p', 'b'],
['a', 'p', 'd'],
['c', 'p', 'd'],
['c', 'p', 'e'],
['c', 'p', 'f']])
mapper = {}
output = []
for ar in X:
temp = []
for i in range(len(ar)):
if ar[i] not in mapper:
random_number = None
while True:
random_number = randint(1,120)
if random_number not in mapper.values():
break
mapper[ar[i]] = random_number
temp.append(mapper[ar[i]])
output.append(temp)
output = np.array(output)
print(output)
输出:
[[ 7 86 120]
[ 7 86 14]
[ 51 86 14]
[ 51 86 113]
[ 51 86 54]]
添加回答
举报