用另一个numpy数组中的值替换numpy数组中的值 [英] Replace values of a numpy array by values from another numpy array
问题描述
我有一个1000 * 1000 numpy数组,具有一百万个值,其创建方式如下:
i have a 1000 * 1000 numpy array with 1 million values which was created as follows :
>>import numpy as np
>>data = np.loadtxt('space_data.txt')
>> print (data)
>>[[ 13. 15. 15. ..., 15. 15. 16.]
[ 14. 13. 14. ..., 13. 15. 16.]
[ 16. 13. 13. ..., 13. 15. 17.]
...,
[ 14. 15. 14. ..., 14. 14. 13.]
[ 15. 15. 16. ..., 16. 15. 14.]
[ 14. 13. 16. ..., 16. 16. 16.]]
我还有另一个numpy数组,该数组有2列,如下所示:
I have another numpy array which which has 2 columns as follows:
>> print(key)
>>[[ 10., S],
[ 11., S],
[ 12., S],
[ 13., M],
[ 14., L],
[ 15., S],
[ 16., S],
...,
[ 92., XL],
[ 93., M],
[ 94., XL],
[ 95., S]]
我基本上想要的是用这样的键数组第二列中的对应元素替换数据数组的每个元素.
What i would basically want is to replace each element of of the data array with corresponding element in the second column of the key array like this..
>> print(data)
>>[[ M S S ..., S S S]
[ L M L ..., M S S]
[ S M M ..., M S XL]
...,
[ L S L ..., L L M]
[ S S S ..., S S L]
[ L M S ..., S S S]]
推荐答案
在Python中,字典是从键到值的映射的自然选择. NumPy有 没有直接等同于字典的内容.但是它确实具有可以进行快速整数索引的数组.例如,
In Python dicts are a natural choice for mapping from keys to values. NumPy has no direct equivalent of a dict. But it does have arrays which can do fast integer indexing. For example,
In [153]: keyarray = np.array(['S','M','L','XL'])
In [158]: data = np.array([[0,2,1], [1,3,2]])
In [159]: keyarray[data]
Out[159]:
array([['S', 'L', 'M'],
['M', 'XL', 'L']],
dtype='|S2')
因此,如果我们可以将您的key
数组按摩成一个看起来像这样的数组:
So if we could massage your key
array into one that looked like this:
In [161]: keyarray
Out[161]:
array(['', '', '', '', '', '', '', '', '', '', 'S', 'S', 'S', 'M', 'L',
'S', 'S', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', 'XL', 'M', 'XL', 'S'],
dtype='|S32')
在keyarray[10]
等于S
的意义上,使10映射为'S',依此类推:
So that 10 maps to 'S' in the sense that keyarray[10]
equals S
, and so forth:
In [162]: keyarray[10]
Out[162]: 'S'
然后我们可以使用keyarray[data]
产生所需的结果.
then we could produce the desired result with keyarray[data]
.
import numpy as np
data = np.array( [[ 13., 15., 15., 15., 15., 16.],
[ 14., 13., 14., 13., 15., 16.],
[ 16., 13., 13., 13., 15., 17.],
[ 14., 15., 14., 14., 14., 13.],
[ 15., 15 , 16., 16., 15., 14.],
[ 14., 13., 16., 16., 16., 16.]])
key = np.array([[ 10., 'S'],
[ 11., 'S'],
[ 12., 'S'],
[ 13., 'M'],
[ 14., 'L'],
[ 15., 'S'],
[ 16., 'S'],
[ 17., 'XL'],
[ 92., 'XL'],
[ 93., 'M'],
[ 94., 'XL'],
[ 95., 'S']])
idx = np.array(key[:,0], dtype=float).astype(int)
n = idx.max()+1
keyarray = np.empty(n, dtype=key[:,1].dtype)
keyarray[:] = ''
keyarray[idx] = key[:,1]
data = data.astype('int')
print(keyarray[data])
收益
[['M' 'S' 'S' 'S' 'S' 'S']
['L' 'M' 'L' 'M' 'S' 'S']
['S' 'M' 'M' 'M' 'S' 'XL']
['L' 'S' 'L' 'L' 'L' 'M']
['S' 'S' 'S' 'S' 'S' 'L']
['L' 'M' 'S' 'S' 'S' 'S']]
请注意,data = data.astype('int')
假定int
s.您的数据似乎是这种情况,但对于任意浮点数却并非如此.例如,astype('int')
将1.0和1.5都映射到1.
Note that data = data.astype('int')
is assuming that the floats in data
can be uniquely mapped to int
s. That appears to be the case with your data, but it is not true for arbitrary floats. For example, astype('int')
maps both 1.0 and 1.5 map to 1.
In [167]: np.array([1.0, 1.5]).astype('int')
Out[167]: array([1, 1])
这篇关于用另一个numpy数组中的值替换numpy数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!