numpy - 比较矩阵的两个元素并在不同位置填入相对值
我有一个12 x 12的矩阵,只有第一行和第一列有数据。下面是这个矩阵:
[['-' 'T' 'S' 'V' 'K' 'Y' 'A' 'F' 'H' 'L' 'P' 'Q']
['T' '' '' '' '' '' '' '' '' '' '' '']
['S' '' '' '' '' '' '' '' '' '' '' '']
['V' '' '' '' '' '' '' '' '' '' '' '']
['K' '' '' '' '' '' '' '' '' '' '' '']
['Y' '' '' '' '' '' '' '' '' '' '' '']
['A' '' '' '' '' '' '' '' '' '' '' '']
['F' '' '' '' '' '' '' '' '' '' '' '']
['H' '' '' '' '' '' '' '' '' '' '' '']
['L' '' '' '' '' '' '' '' '' '' '' '']
['P' '' '' '' '' '' '' '' '' '' '' '']
['Q' '' '' '' '' '' '' '' '' '' '' '']]
接下来,我需要比较第一行的每个元素和第一列的每个元素,看看它们是相同还是不同(比如,TT、SS或者SV、KS),而且要忽略字母的顺序(也就是说,SV和VS被认为是一样的)。我正在尝试使用下面的for循环:
for x in np.nditer(matrix,op_flags=['readwrite'],op_dtypes=['str']):
不过,我不太确定怎么用for循环访问每个元素,并将它们与不同位置的元素进行比较。而且,我也不太清楚怎么把这个比较的结果写到矩阵的其他位置。我对numpy不太熟悉,虽然我看过一些相关的问题,但似乎没有一个能帮到我。请问在numpy中可以做到这一点吗?
提前谢谢你们!
3 个回答
0
我按照G M建议的做法,写了类似的代码,代码如下:
def freq_pair(output_pa):
# Creating matrix using the list with 12 rows and 12 columns
aa = ['T','S','V','K','Y','A','F','H','L','P','Q']
nrows = len(aa)
ncols = len(aa)
#print nrows,ncols
matrix = np.zeros((nrows,ncols))
print(matrix)
#Looping through the matrix to change the value to frequency of occurence
for item in aa:
for itemB in aa:
if aa.index(item) != aa.index(itemB):
eab = float((output_pa[item][1])) * float((output_pa[itemB][1]))*2
else:
eab = float((output_pa[item][1])) * float((output_pa[itemB][1]))
#print(eab)
matrix[aa.index(item), aa.index(itemB)] = eab
return matrix
output_pa是一个字典,字典里用字母作为键,值则是这些字母出现的次数和它们出现次数占总次数的比例。这样做让我得到想要的结果,速度比其他方法快多了!
0
好的,如果我理解得没错的话:
>>> import numpy as np
>>>
>>> a = np.array([[1,2,3,4],[4,6,7,8],[3,10,11,12],[1,13,14,15]])
>>> a
array([[ 1, 2, 3, 4],
[ 4, 6, 7, 8],
[ 3, 10, 11, 12],
[ 1, 13, 14, 15]])
>>> b = a[0]
>>> b
array([1, 2, 3, 4])
>>> a.transpose()[0]
array([1, 4, 3, 1])
>>> c = a.transpose()[0]
>>> c
array([1, 4, 3, 1])
>>> for x in b:
... for y in c:
... if x == y:
... print "Do this if y=%d is equal to x=%d"%(y,x)
... else:
... print "Do that if y=%d is not equal to x=%d"%(y,x)
...
Do this if y=1 is equal to x=1
Do that if y=4 is not equal to x=1
Do that if y=3 is not equal to x=1
Do this if y=1 is equal to x=1
Do that if y=1 is not equal to x=2
Do that if y=4 is not equal to x=2
Do that if y=3 is not equal to x=2
Do that if y=1 is not equal to x=2
Do that if y=1 is not equal to x=3
Do that if y=4 is not equal to x=3
Do this if y=3 is equal to x=3
Do that if y=1 is not equal to x=3
Do that if y=1 is not equal to x=4
Do this if y=4 is equal to x=4
Do that if y=3 is not equal to x=4
Do that if y=1 is not equal to x=4
我做的事情是先从给定的矩阵中取出第一行,方法是用 b = a[0]
。然后我把原始矩阵进行转置,再从结果中取出第一行,代码是 c = a.transpose()[0]
,这实际上就是拿到了原始矩阵的第一列。这样你就得到了一个数组 b
,里面是第一行的元素,还有 c
,里面是第一列的元素。接下来你可以随意处理这两个数组。我做的事情是:对 b
中的每个元素,在 c
中查找,如果找到就打印出来
1
我觉得你可以直接用Python来完成这个工作,而不需要用到numpy。你可以简单地通过 a[subarray][index] 这样的方式来访问和写入数组,就像解决方案中展示的那样:
a=[['-','T','S','V','K','Y','A','F','H','L','P','Q'],
['T','','','','','','','','','','',''],
['S','','','','','','','','','','',''],
['V','','','','','','','','','','',''],
['K','','','','','','','','','','',''],
['Y','','','','','','','','','','',''],
['A','','','','','','','','','','',''],
['F','','','','','','','','','','',''],
['H','','','','','','','','','','',''],
['L','','','','','','','','','','',''],
['P','','','','','','','','','','',''],
['Q','','','','','','','','','','',''],]
def comparematrix(a):
for i in a[0][1:]:#loop over the first row (note skip first element!)
for j in range(1,len(a)):#loop over number of rows
print a[j][0],i
if a[j][0]==i:#find if they are equal
a[j][a[0].index(i)]='couple!'
else:
a[j][a[0].index(i)]='%s,%s'%(a[j][0],i)
return a
当然,如果你想的话,最后也可以创建一个numpy数组,但我觉得这个例子并不是用numpy数组的常规方式,因为它并不是真正的计算。
[['-', 'T', 'S', 'V', 'K', 'Y', 'A', 'F', 'H', 'L', 'P', 'Q'],
['T', 'couple!', 'T,S', 'T,V', 'T,K', 'T,Y', 'T,A', 'T,F', 'T,H', 'T,L', 'T,P', 'T,Q'],
['S', 'S,T', 'couple!', 'S,V', 'S,K', 'S,Y', 'S,A', 'S,F', 'S,H', 'S,L', 'S,P', 'S,Q'],
['V', 'V,T', 'V,S', 'couple!', 'V,K', 'V,Y', 'V,A', 'V,F', 'V,H', 'V,L', 'V,P', 'V,Q'],
['K', 'K,T', 'K,S', 'K,V', 'couple!', 'K,Y', 'K,A', 'K,F', 'K,H', 'K,L', 'K,P', 'K,Q'],
['Y', 'Y,T', 'Y,S', 'Y,V', 'Y,K', 'couple!', 'Y,A', 'Y,F', 'Y,H', 'Y,L', 'Y,P', 'Y,Q'],
['A', 'A,T', 'A,S', 'A,V', 'A,K', 'A,Y', 'couple!', 'A,F', 'A,H', 'A,L', 'A,P', 'A,Q'],
['F', 'F,T', 'F,S', 'F,V', 'F,K', 'F,Y', 'F,A', 'couple!', 'F,H', 'F,L', 'F,P', 'F,Q'],
['H', 'H,T', 'H,S', 'H,V', 'H,K', 'H,Y', 'H,A', 'H,F', 'couple!', 'H,L', 'H,P', 'H,Q'],
['L', 'L,T', 'L,S', 'L,V', 'L,K', 'L,Y', 'L,A', 'L,F', 'L,H', 'couple!', 'L,P', 'L,Q'],
['P', 'P,T', 'P,S', 'P,V', 'P,K', 'P,Y', 'P,A', 'P,F', 'P,H', 'P,L', 'couple!', 'P,Q'],
['Q', 'Q,T', 'Q,S', 'Q,V', 'Q,K', 'Q,Y', 'Q,A', 'Q,F', 'Q,H', 'Q,L', 'Q,P', 'couple!']]