import tbapy import numpy as np import string from numpy.linalg import inv, det #My auth key tba = tbapy.TBA("x9bbuY4qH2k7jo0YTynCWKMWUGnMLqhI5mBXnfq3OaIB8HPiJPJrbTCSFw6CliKK") event_list = ["2018code", "2018mnmi", "2018mnmi2", "2018mndu", "2018mndu2", "2018ndgf", "2018casj", "2018wila", "2018mosl", "2018txsa", "2018txda", "2018qcmo"] X = [] Y = [] X_test = [] Y_test = [] for code in event_list: #Creates most of the data event = code teams = tba.event_teams(event,keys=True) for i in range(len(teams)): teams[i]= str(teams[i])[3:] matches = tba.event_matches(event,keys=True) #Finding just quals start_location = matches.index(event+"_qm1") end_location = matches.index(event+"_sf1m1") matches = matches[start_location:end_location] #Removes no shows all_matches = "" for i in matches: all_matches+=str(tba.match(i)) p=0 while p<len(teams): if teams[p] not in all_matches: teams.remove(teams[p]) p+=1 #initializes sparse matrix and stats sparse_matrix = [] stats = [[], [], [], [], [], [], [], [], [], [], [], []] stats_names = ["teleopPoints", "autoPoints", "autoRunPoints", "teleopScaleOwnershipSec", "foulPoints", "rp", "teleopSwitchOwnershipSec", "autoSwitchOwnershipSec", "autoScaleOwnershipSec", "vaultPoints", "endgamePoints", "score"] #Lets scrape some web! for i in matches: working_match = str(tba.match(i)) #Finds, indexes and then records score for red and blue alliance in the data def find_a_stat(name): index1 = working_match.find(name+"':") asdf = working_match[index1+1:] index2 = asdf.find(name + "':") blue_stat = working_match[index1+len(name)+3:index1+len(name)+6] red_stat = asdf[index2+len(name)+3:index2+len(name)+6] for i in range(2): if red_stat[-1] not in string.digits: red_stat = red_stat[:-1] if blue_stat[-1] not in string.digits: blue_stat = blue_stat[:-1] return red_stat, blue_stat #Puts the scores in the result scores vector for i in range(len(stats)): stats[i].append([int(find_a_stat(stats_names[i])[0])]) stats[i].append([int(find_a_stat(stats_names[i])[1])]) #creates the array out of alliances, runs through a team list, puts a 1 if they are in the alliance, 0 if they are not k = -140 row_red = [] row_blue = [] for j in teams: if j in working_match[-300:k]: row_blue.append(1) else: row_blue.append(0) if j in working_match[k:]: row_red.append(1) else: row_red.append(0) sparse_matrix.append(row_red) sparse_matrix.append(row_blue) #Makes sure when we do operations to the matrix that they work fine since I am unsure of what would happen with ints for i in range(len(stats)): for j in range(len(stats[i])): stats[i][j][0]=float(stats[i][j][0]) for i in range(len(stats)): stats[i]=np.array(stats[i]) #Makes sure everything carries out smoothly with floats for i in range(len(sparse_matrix)): for j in range(len(sparse_matrix[i])): sparse_matrix[i][j]=float(sparse_matrix[i][j]) #Makes everything fine and numpy sp = np.array(sparse_matrix) #Finds AtA, so that we can invert in and multiply the score vector by its inverse (spls = sparse matrix least squares) spls = np.dot(sp.T,sp) array_of_god = [] #Goal is x = ((AtA)^-1)(At)(scores) for i in range(len(stats)): array_of_god.append(np.dot(inv(spls),np.dot(sp.T,stats[i]))) for i in range(len(array_of_god)): for j in range(len(array_of_god[i])): array_of_god[i][j]=round(array_of_god[i][j],2) for i in range(len(teams)): teams[i]=[teams[i]] for j in range(len(stats)): teams[i].append(float(array_of_god[j][i][0])) print "Team "+"tele "+ "auto "+ "autoRun " + "teleScaleSec " + "foul " + "rp " + "teleSwitchSec " + "autoSwitchSec " + "autoScaleSec " + "vault " + "endgame " + "score" #Prints everything out in line with " " as delimeter for i in range(len(teams)): for j in range(len(teams[i])): teams[i][j]=str(teams[i][j]) print " ".join(teams[i]) #We finally get to data collection if code!="2018mnmi": for i in matches: working_match = str(tba.match(i)) winner = working_match[working_match.find("winning_alliance")+21] #Indicates if red or blue won, mapping red win as 1, blue win as 0 if winner == "r": Y.append(1) else: Y.append(0) #Now that we have Y, X is going to be a bitch to get k = -140 Xdata = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] for j in teams: if j[0] in working_match[-300:k]: for q in range(1,len(j)): Xdata[q-1]+=int(float(j[q])) if j[0] in working_match[k:]: for q in range(1,len(j)): Xdata[q+11]+=int(float(j[q])) X.append(Xdata) else: for i in matches: working_match = str(tba.match(i)) winner = working_match[working_match.find("winning_alliance")+21] #Indicates if red or blue won, mapping red win as 1, blue win as 0 if winner == "r": Y_test.append(1) else: Y_test.append(0) #Now that we have Y, X is going to be a bitch to get k = -140 Xdata = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] for j in teams: if j[0] in working_match[-300:k]: for q in range(1,len(j)): Xdata[q-1]+=int(float(j[q])) if j[0] in working_match[k:]: for q in range(1,len(j)): Xdata[q+11]+=int(float(j[q])) X_test.append(Xdata) print len(X),len(Y) print X_test print X print Y X = np.array(X) #Machine Learning aspect import tensorflow from keras.models import Sequential from keras.layers import Dense #we already have numpy as np np.random.seed(4) model = Sequential() model.add(Dense(12, input_dim=24, activation = 'relu')) model.add(Dense(15, activation = 'relu')) model.add(Dense(8, activation = 'relu')) model.add(Dense(10, activation = 'relu')) model.add(Dense(12, activation = 'relu')) model.add(Dense(1, activation = 'sigmoid')) model.compile(loss = "binary_crossentropy", optimizer = "adam", metrics = ['accuracy']) model.fit(X, Y, epochs = 1000, batch_size = 17) acc = model.evaluate(X, Y) print "\n%s: %.2f%%" % (model.metrics_names[1], acc[1]*100) for match in range(len(X_test)): print model.predict_proba(X_test[match])
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question