【问题标题】:create a matrix with links创建一个带有链接的矩阵
【发布时间】:2018-10-15 12:09:47
【问题描述】:

我正在创建一个模型来查看元组的元素如何迭代到列表中的其他元组。

举个例子

employerEmployeeEdges= [(12,a), (12,c), (12,d), (14,e), (14,a), (13, a), (13,b), (13,d), (13,c), (16,b),(16,b) ]

这里的目标是匹配例如元组 1 中的 12 到元组 2 中的 12 以及它们是否匹配计数。匹配被视为“链接”。我需要将这些链接的数量放入一个矩阵中。

例如:

   a  b  c  d  e 
a  0     1  2  2
b    0
c  1    0   0
d    0     0
e  1           0

我有以下代码

from collections import defaultdict

将熊猫导入为 pd 将 numpy 导入为 np 从 itertools 导入组合 从集合导入计数器 将 numpy 导入为 np 将 scipy.sparse 导入为 ss np.seterr(divide='ignore', invalid='ignore')

测试数据

year= [2001, 2002, 2002, 2005, 2002, 2004, 2001, 2001, 2002, 2003, 2003, 2002, 2004, 2005, 2003, 2004, 2005, 2004, 2004, 2002, 2001, 2001]
indviduals= [12, 23, 12, 24, 28,30, 15, 17, 18, 18, 19, 12, 15, 12, 12, 12, 15, 15, 15, 12, 12, 15, 200, 200]
employers= ['a', 'b', 'b','c', 'd', 'e', 'a', 'a', 'b', 'b', 'c', 'b', 'a', 'c', 'e', 'a', 'a', 'a', 'a', 'b', 'a', 'a', 'b']

employerEmployeeEdges=[]
for j in np.unique(year):
    """generates the count of employees per employer per year"""
    #print("year",j)
    d = dict.fromkeys(employers, ())
    cond_year = j
    for i,e,y in zip(indviduals, employers, year):
        if y == cond_year:
            d[e] = d[e] + (i,)
            
    #print(d, [len(v) for k, v in d.items()]) # if I want to print all the employers and employee per year 
    
    for k, v in d.items():
        if len(v)>1:
            """I am gonna have to ignore if there are no values for that specific employer. 
            Zero employees means nothing for that year"""
            #print(j,k)
            for item in v:
                #print(item, "item")
                #print(j, item, k)
                edges = (item, k)
                edges=edges
                #print(edges, type(edges))
                employerEmployeeEdges.append(edges) # create a list of employees employer edge for all years
                
                
print("employees employer edges", [i for i in employerEmployeeEdges]) # list of possible links between employee and employer 
employersNew=[i[1] for i in employerEmployeeEdges]
# print("dfd",employersNew)
n = len([i[1] for i in employerEmployeeEdges])
Q = np.zeros((n, n), dtype=int)

for firstLink in  employerEmployeeEdges:
    for secondLink in employerEmployeeEdges[1:]: #potential second link where the combination is possible. 
        if firstLink[0]==secondLink[0]:
            print(firstLink[1], secondLink[1])
# #             print(firstLink, secondLink)
# #             break
#         from_node, to_node=firstLink[1],secondLink[1] #check where did the employee go? 
            
#         indx, jdx= employersNew.index(from_node), employersNew[1:].index(to_node)
            
#         Q[indx, jdx]=0
#         print(Q)
# #print(len(employerEmployeeEdges))
# #print(Q)

这个打印不会给我想要的输出。我如何将链接数放在矩阵上?

此外,我想使用矩阵 Q 来计算概率,如下所示:

# P=np.empty((n,n))
# #print(P)
# for i in range(n):
#     #print(i)
#     P[i, :] = Q[i, :] / Q[i, :].sum()
    
# #print(P)

【问题讨论】:

  • 您的代码有问题吗?请描述您需要什么样的帮助。
  • 是的,因为当我打印 Q 时,我得到全零。如何在该矩阵上添加计数?
  • 您的代码没有运行。我在employersNew = [i[1] for i in employerEmployeeEdges] 收到一个错误,这很糟糕,因为它是第一行代码。请纠正。我也很难理解你对你想要什么的解释。它与您提供的示例不匹配。还是我错过了什么?
  • 嗨 figbeam ,对不起,我将复制的部分代码分块。让我发布更新的代码,以便您可以看到我在说什么

标签: python numpy matrix


【解决方案1】:

你可以这样做:

employerEmployeeEdges= np.array([(12,'a'), (12,'c'), (12,'d'), (14,'e'), (14,'a'),
(13, 'a'), (13,'b'), (13,'d'), (13,'c'), (16,'b'),(16,'b') ])

unique_employee = np.unique(employerEmployeeEdges[:,1])
n_unique = len(unique_employee)

Q = np.zeros([n_unique,n_unique])

for n, employer_employee in enumerate(employerEmployeeEdges):
   #copy the array for the original o be intact
   eee = np.copy(employerEmployeeEdges)
   #sustitue the current tuple with a empty one to avoid self comparing
   eee[n] = (None,None)

   #get the index for the current employee, the one on the y axis
   employee_index = np.where(employer_employee[1] == unique_employee)

   #get the indexes where the the employees letter match
   eq_index = np.where(eee[:,0] == employer_employee[0])[0]
   eq_employee = eee[eq_index,1]

   #add at the final array Q by index
   for emp in eq_employee:

      emp_index = np.where(unique_employee == emp)
      Q[employee_index,emp_index]+= 1

print(Q)

此代码呈现以下答案:

[[0. 1. 2. 2. 1.]
 [1. 2. 1. 1. 0.]
 [2. 1. 0. 2. 0.]
 [2. 1. 2. 0. 0.]
 [1. 0. 0. 0. 0.]]

请记住,Q[0,0] 是 'a:a' 而 Q[-1,-1] 是 'e:e'

【讨论】:

  • 谢谢。我会检查这个并回复你
猜你喜欢
  • 1970-01-01
  • 2012-03-16
  • 2020-05-25
  • 2017-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-21
  • 1970-01-01
相关资源
最近更新 更多