1
votes

I am creating a model to see how an element of tuple iterates over to other tuples in the list.

For an instance

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

The goal here is to match say for example 12 in tuple 1 to 12 in tuple 2 and if they match count. The match is considered as 'Link'. I need to put count of those links into a matrix.

For example:

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

I have following code

from collections import defaultdict

import pandas as pd import numpy as np from itertools import combinations from collections import Counter import numpy as np import scipy.sparse as ss np.seterr(divide='ignore', invalid='ignore')

test data

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)

This print wont give me desired output. How would I put the count of links on the matrix?

Further, I want to use the Matrix Q to compute the probability like as follows:

# P=np.empty((n,n))
# #print(P)
# for i in range(n):
#     #print(i)
#     P[i, :] = Q[i, :] / Q[i, :].sum()
    
# #print(P)
1
Do you have a problem with your code? Please describe what kind of help you want. - Moberg
Yes, because when I print Q, I get all zeros. How do add count on that matrix? - lpt
Your code does not run. I get an error at employersNew = [i[1] for i in employerEmployeeEdges] which is bad as it's the first line of code. Please correct. Also I'm having a hard time understanding your explanation of what it is you want. It does not match the example you present. Or am I missing something? - figbeam
hi figbeam , sorry I chunked the copied the partial code. Let me post the updated code so you may see what I am talking - lpt

1 Answers

0
votes

You could do something like this:

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)

This code renders the following answer:

[[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.]]

Keep in mind that Q[0,0] is the 'a:a' and Q[-1,-1] is the 'e:e'