The process to build plurality-with-elimination method



SEGR 2101--Code demo for plurality-with-eliminationObjective1.Illustration of python packages, namely, numpy + pandas + matplotlib2.Build plurality-with-elimination method3.Optimization for parts of codeimport pandas as pdimport numpy as npimport matplotlib.pyplot as pltdata = pd.read_csv(r"/Users/John/Downloads/Mid_term_sample_1.csv")dataThe process to build plurality-with-elimination method0. In round 0, we need to compute the bar of difference.1. In round 1, we are required to compute the lowest score for current turn.2. In round 2,a. Cross out the name of the canditate who has the lowest scoreb. Remove the canditate out from the table.c. Compute the difference. Decide if next round is required.3. Repeat rounds until ending conditions are reached.def elimination(array): def computelowest(): dic = {} for i in range(len(array[0])): if array[0][i] not in dic: dic[array[0][i]] = Number_of_voters[i] else: dic[array[0][i]] += Number_of_voters[i] print "The score for canditates are: ",dic minind,minval = "",float("inf") for ele in dic: if dic[ele]<minval: minind = ele minval = dic[ele] return minind,dic def modifyarr(array,newarray,minind): if newarray and newarray[0]: for i in range(len(newarray[0])): k=0 for j in range(len(newarray)): if array[j][i]==minind: k+=1 newarray[j][i] = array[k][i] k+=1 return newarray Number_of_voters = list(data.columns)[1:] Number_of_voters = [int(x) for x in Number_of_voters] minind,dic = computelowest() newarray = [[i for i in range(array.shape[1])] for i in range(array.shape[0]-1)] newarray = np.array(modifyarr(array,newarray,minind),dtype = object) return newarray,dicarray = data.iloc[:,1:].valueslength = data.shape[0]Number_of_voters = list(data.columns)[1:]Number_of_voters = [int(x) for x in Number_of_voters]difference_req = sum(Number_of_voters)//2for i in range(length): array,dic = elimination(array) score = sorted(dic.values(),reverse=True) if len(score)>1: difference_act = score[0]-score[1] if difference_act>difference_req: break ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download