def genlist(mini, maxi, step): r = [] i = mini while i < maxi: r.append(i) i += step r.append(maxi) return r
def accuracy_by_filter_score( data, title, sc=False ): # Convert Groups of arrays into lists of scores by csore. first, last, random = [], [], [] hmm, best, ind = [], [], [] sample_sizes = [] for j in xrange(0,995): #trial = np.loadtxt( '../Data/Results/' + trial_name, delimiter = ',' ) #accuracies = means[::2] #acc_std = stds[::2] #softcalls = means[1::2] #sc_std = stds[1::2] trial = data[j] accuracies = trial[::2] softcalls = trial[1::2] if sc: f,l,r,h,b,i = softcalls else: f,l,r,h,b,i = accuracies first.append( f ); last.append( l ); random.append( r ) hmm.append( h ) ; best.append( b ); ind.append( i ) #sample_sizes.append( trial_name.split('_')[3] ) x = cscores #plt.plot( x, first, label='First', ls='--', c='k', lw=1) #plt.plot( x, last, label='Last', ls='--', c='c', lw=1) #plt.plot( x, random, label='Random', ls='--', c='m', lw=1 ) hmm, hmm_std = rolling_average(hmm) best, best_std = rolling_average(best) ind, ind_std = rolling_average(ind) plt.plot( x, hmm, label='HMM')#, c='y', lw=1 ) plt.plot( x, best, label='Best')#, lw=1, c='r' ) plt.plot( x, ind, label='Ind')#, lw=1, c='b') # plt.title( trial_name.split('_')[0] + ' - ' + trial_name.split('_')[1] \ # + ' - ' + trial_name.split('_')[2], fontsize=14 ) plt.title( title, fontsize=18 ) plt.xlabel( 'Read Cutoff', fontsize=14 ) plt.ylabel( 'Accuracy', fontsize=14 ) plt.xlim([0.0, .95]) plt.ylim( [0.5,0.85] ) plt.legend(loc=8, bbox_to_anchor=(0.5, 0.0), ncol=2, fancybox=True, shadow=True) plt.gca().invert_xaxis() print 'plt.show' plt.show() plt.savefig( '/Users/Jvivian/Desktop/MR.svg', dpi=300)
def getListOfFirstWords(notes): dd = {} for nn in notes: firstWord = nltk.pos_tag([nltk.word_tokenize(nn.contents)[0]])[0] if firstWord[0] in dd: dd[firstWord[0]][1] += 1 else: dd[firstWord[0]] = [firstWord[1], 1] rr = [] for key, pair in dd.items(): rr.append((key, pair[1])) rr.sort(lambda x,y:cmp(x[1],y[1])) return rr
def accuracy_by_filter_score( data, title, sc=False ): # Convert Groups of arrays into lists of scores by csore. first, last, random = [], [], [] hmm, best, ind = [], [], [] sample_sizes = [] for j in xrange(0,10): #trial = np.loadtxt( '../Data/Results/' + trial_name, delimiter = ',' ) #accuracies = means[::2] #acc_std = stds[::2] #softcalls = means[1::2] #sc_std = stds[1::2] trial = data[j] accuracies = trial[::2] softcalls = trial[1::2] if sc: f,l,r,h,b,i = softcalls else: f,l,r,h,b,i = accuracies first.append( f ); last.append( l ); random.append( r ) hmm.append( h ) ; best.append( b ); ind.append( i ) #sample_sizes.append( trial_name.split('_')[3] ) x = [ 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0 ] plt.plot( x, first, label='first', ls='--', c='k') plt.plot( x, last, label='last', ls='--', c='c') plt.plot( x, random, label='random', ls='--', c='m' ) plt.plot( x, hmm, label='hmm', c='y' ) plt.plot( x, best, label='best', lw=2, c='r' ) plt.plot( x, ind, label='ind', lw=2, c='b') # plt.title( trial_name.split('_')[0] + ' - ' + trial_name.split('_')[1] \ # + ' - ' + trial_name.split('_')[2], fontsize=14 ) plt.title( title ) plt.xlabel( 'Chunk Score Cutoff', fontsize=14 ) plt.ylabel( 'Accuracy', fontsize=14 ) plt.ylim( [0.5,1.0] ) plt.legend(loc='lower left', bbox_to_anchor=(1.02, 0), borderaxespad=0, fontsize=14) plt.gca().invert_xaxis() plt.show()
def rules(self, color): win = [] block = [] open_four = [] block_open_four = [] random = [] for move in self.board.get_empty_points(): best = 5 for two_directions in [["N", "S"], ["NE", "SW"], ["E", "W"], ["SE", "NW"]]: stats = self.line_rule(color, move, two_directions) if stats["win"]: best = 1 break elif stats["block_win"]: if best > 2: best = 2 elif stats["open_four"]: if best > 3: best = 3 elif stats["block_open_four"]: if best > 4: best = 4 if best == 1: win.append(move) elif best == 2: block.append(move) elif best == 3: open_four.append(move) elif best == 4: block_open_four.append(move) else: random.append(move) if len(win) > 0: return {"Win": win} if len(block) > 0: return {"BlockWin": block} if len(open_four) > 0: return {"OpenFour": open_four} if len(block_open_four): return {"BlockOpenFour": block_open_four} if len(random) > 0: return {"Random": random} return "pass"
def createPopulation(): random = [] a = 0 while a < popSize: # ini buat check banyak rule tiap chromosome. min 1 rule , max 3 rule checker = numpy.random.randint(1, 5) # bikin array data random, dengan isi 0/1 data = numpy.random.randint(2, size=popLength * checker) arrcheck = numpy.array_split(data, checker) valid = True j = 0 # nge check kalo di dalam array random apakah salah 1 persyaratannya adalah kosong, ex: suhu = [0,0,0] # kalo suhu = [0,0,0], maka chromosome nya salah dan akan di random lagi while j < checker and valid: # array suhu bit ke 1-3 suhu = arrcheck[j][0:3] # array waktu bit ke 4-7 waktu = arrcheck[j][3:7] #array cuaca bit ke 8-11 cuaca = arrcheck[j][7:11] #array kelembapan bit ke 12-14 kelembapan = arrcheck[j][11:14] # nge check kalo suhu nya [0,0,0] if (not any(suhu)): valid = False # nge check kalo waktu nya [0,0,0,0] elif (not any(waktu)): valid = False # nge check kalo cuaca nya [0,0,0,0] elif (not any(cuaca)): valid = False # nge check kalo kelembapan nya [0,0,0] elif (not any(kelembapan)): valid = False j = j + 1 # kalo data nya valid, maka di masukan ke dalam array, otherwise random ulang sampai data valid if valid: random.append(data) a = a + 1 return random
def run(black, white, rep): runs = np.zeros(black + white) for i in range(rep): if i % 1000 == 0: print i b = bag(black, white) r = [] for i in range(black + white): r.append(b.pull()) runs += np.array(r) print np.mean(runs / rep) print np.var(runs / rep) plt.plot(np.zeros(len(runs)) + np.mean(runs / rep)) plt.plot(runs / rep) plt.title("%d experiments with draws from a bag of %d white and %d black" % (rep, white, black)) plt.legend(["mean number of whites", "whites pr. run"]) plt.xlabel("N") plt.ylabel("P") plt.show()
def showPlot3(): """ Produces a plot comparing the two robot strategies. """ xvals = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] standard = [] for i in range(len(xvals)): time = runSimulation(1, 1.0, 20, 20, xvals[i], 100, StandardRobot) standard.append(time) random = [] for i in range(len(xvals)): time = runSimulation(1, 1.0, 20, 20, xvals[i], 100, RandomWalkRobot) random.append(time) #print standard, random pylab.plot(xvals, standard, 'r-', label="StandardRobot") pylab.plot(xvals, random, 'b-', label="RandomWalkRobot") pylab.xlabel('Fraction of Room to be Cleaned') pylab.ylabel('Mean time') pylab.legend(loc='upper left') pylab.title( 'Comparison of StandardRobot and RandomWalkRobot in a 20x20 room') pylab.show()
def ComponentQuery2(subj, pred, obj): prefix = """ PREFIX rdf:<http://www.w3.org/2000/01/rdf-schema#> PREFIX dbpd:<http://dbpedia.org/ontology/> """ r = [] if pred!=None and obj!=None: qSelect_S = prefix + """ SELECT ?sub WHERE { ?sub """ + pred + """ """ + obj + """. }""" #r1 = m_graph.query(qSelect_S) sparql.setQuery(qSelect_S) r1 = sparql.query().convert() if r1 != None: # may need one more variable to record source r.append(r1) if subj!=None and obj!=None: qSelect_P = prefix + """ SELECT ?pred WHERE { """ + subj + """ ?pred """ + obj + """. }""" #r2 = m_graph.query(qSelect_P) sparql.setQuery(qSelect_P) r2 = sparql.query().convert() if r2 != None: # may need one more variable to record source r.append(r2) if subj!=None and pred!=None: qSelect_O = prefix + """ SELECT ?obj WHERE { """ + subj + """ """ + pred + """ ?obj. }""" #r3 = m_graph.query(qSelect_O) sparql.setQuery(qSelect_O) r3 = sparql.query().convert() if r3 != None: # may need one more variable to record source r.append(r3) #if r!=[]: # print(r) #PrintQueryResult(r) return r
with open('parted_idx.json','r') as f: partitions = json.load(f) with open('traj_viewpoint.json','r') as f: traj_viewpoint = json.load(f) #%% part_2 = partitions['random'] + partitions['phrase'] phrase = [] random = [] for t_i in part_2: ls = re.split('\, +|\. ',data[t_i[0]]['instructions'][t_i[1]]) ls = [elm for elm in ls if elm != ''] if len(ls) > 1 and len(phrase) < 7392: phrase.append(t_i) else: random.append(t_i) partitions['random'] = random partitions['phrase'] = phrase with open('parted_idx.json','w') as f: json.dump(partitions,f) #%% start constructing hardNeg = [] gt = {} #%% entity and random (which is reversal actually) ## load responses with open('responses_train_new.json') as f: resp_1 = json.load(f)
def heuristique_arrondi(H,data): m = gurobipy.Model("MyModel") V = len(H) x = [] z = [] for i in range(V+1): x2 = [] for j in range(V+1): x2.append(m.addVar(vtype = gurobipy.GRB.CONTINUOUS, name = "x%d%d"%(i,j))) x.append(x2) for i in range(V+1): z2 = [] for j in range(V+1): z2.append(m.addVar(vtype = gurobipy.GRB.CONTINUOUS, name = "z%d%x"%(i,j))) z.append(z2) m.update() obj = gurobipy.LinExpr(); obj = 0 #Fonction objectif for i in range(V): for j in range(V): obj += evalCouple2(H[i],H[j],data) * x[i][j] m.setObjective(obj,gurobipy.GRB.MAXIMIZE) #1 arete sortante par sommet for j in range(V+1): m.addConstr(gurobipy.quicksum(x[i][j] for i in range(V+1))==1) #1 arete entrante par sommet for i in range(V+1): m.addConstr(gurobipy.quicksum(x[i][j] for j in range(V+1))==1) #pas d'arete sur un meme sommet for i in range(V+1): m.addConstr(x[i][i]==0) #pas d'arete de a vers b et de b vers a simultanes for i in range(V+1): for j in range(V+1): m.addConstr(x[i][j]+x[j][i]<=1) #Contraintes de flots #1 m.addConstr(gurobipy.quicksum(z[0][j] for j in range(1,V+1)) == V) #2 for i in range(1,V+1): m.addConstr( (gurobipy.quicksum(z[i][j] for j in range(1,V+1) if j != i)+1) == (gurobipy.quicksum(z[j][i] for j in range(V+1) if j!=i)) ) #3 for i in range(V+1): list_j = [j for j in range(1,V+1) if j != i] for j in list_j: m.addConstr((z[i][j]+z[j][i]) <= (V)*(x[i][j]+x[j][i])) #4 for i in range(V+1): list_j = [j for j in range(1,V+1) if j != i] for j in list_j: m.addConstr(z[i][j]>=0) #Resolution m.optimize() result = [] v = [ [x[i][j].x,i,j] for j in range(len(x[i])-1) for i in range(len(x)-1)] v.sort(key=lambda y:y[0], reverse = True) alreadyI = [0] * V alreadyJ = [0] * V compteur=0 while(compteur < V-1): if(alreadyI[v[0][1]] == 0 and alreadyJ[v[0][2]]==0): r = c.deepcopy(result) r.append([[v[0][1]],[v[0][2]]]) r = deepTransfoPl(r) b=True for i in r : if(i[0] == i[len(i)-1]): b= False if(b): compteur = compteur+1 result = r alreadyI[v[0][1]]=1 alreadyJ[v[0][2]]=1 del(v[0]) result = result[0] result.insert(0,len(result)) return result
def createRandomChildren(n, num_variables): random = [] for i in range(0, n): random.append(np.array(np.random.choice(a=[-1, 1], size=(1, num_variables)))[0]) return random