def MLPRegressorPredict(playerFirstName, playerLastName, target, batter = False, solver='lbfgs'): statMap = pitchStatMap if batter: statMap = batStatMap if target not in statMap.keys(): print "bad target value" data2016, null, playerData = dataUtil.getPlayerInformation(playerFirstName + " " + playerLastName) features = statMap[target] x = [] y = [] calculate2015data = [] # print playerFirstName, playerLastName, playerData if not playerData or '2015' not in playerData: return None for i, year in enumerate(playerData): if year == '2015': continue curArray = [] if not playerData.get(str(int(year) + 1), 0): continue for feature in features: curArray.append(float(playerData[year][feature])) x.append(curArray) y.append(float(playerData[str(int(year) + 1)][target])) for feature in features: calculate2015data.append(float(playerData['2015'][feature])) if not x or not y: return None regr = MLPRegressor(hidden_layer_sizes=(1000,),solver=solver) regr.fit(x, y) return regr.predict(calculate2015data)[0]
def predict(playerFirstName, playerLastName, target, batter = False): statMap = pitchStatMap if batter: statMap = batStatMap if target not in statMap.keys(): print "bad target value" data2016, null, playerData = dataUtil.getPlayerInformation(playerFirstName + " " + playerLastName) features = statMap[target] x = [] y = [] calculate2016data = [] for year in playerData: curArray = [] for feature in features: curArray.append(float(playerData[year][feature])) x.append(curArray) y.append(float(playerData[year][target])) for feature in features: calculate2016data.append(float(data2016[feature])) regr = linear_model.LinearRegression() regr.fit(x, y) return regr.predict(calculate2016data)[0]
def SVRPredict(playerFirstName, playerLastName, target, batter = False, kernel='rbf', C=1.0, epsilon=0.1, myFeatures=None): statMap = pitchStatMap if batter: statMap = batStatMap if target not in statMap.keys(): print "bad target value" data2016, null, playerData = dataUtil.getPlayerInformation(playerFirstName + " " + playerLastName) if myFeatures == None: features = statMap[target] else: features = myFeatures x = [] y = [] calculate2015data = [] # print playerFirstName, playerLastName, playerData if not playerData or '2015' not in playerData: return None for i, year in enumerate(playerData): if year == '2015': continue curArray = [] if not playerData.get(str(int(year) + 1), 0): continue for feature in features: curArray.append(float(playerData[year][feature])) x.append(curArray) y.append(float(playerData[str(int(year) + 1)][target])) for feature in features: calculate2015data.append(float(playerData['2015'][feature])) if not x or not y: return None regr = SVR(kernel=kernel, C=C, epsilon=epsilon) regr.fit(x, y) return regr.predict(calculate2015data)[0]
def nextYearPredict(playerFirstName, playerLastName, target, batter=False): statMap = pitchStatMap if batter: statMap = batStatMap if target not in statMap.keys(): print "bad target value" data2016, null, playerData = dataUtil.getPlayerInformation( playerFirstName + " " + playerLastName) features = statMap[target] x = [] y = [] calculate2015data = [] if not playerData or '2015' not in playerData: return None for i, year in enumerate(playerData): if year == '2015': continue curArray = [] if not playerData.get(str(int(year) + 1), 0): continue for feature in features: if feature == "G": indexOfG = features.index(feature) if float(playerData[year][feature]) == 0: curArray.append(float(playerData[year][feature]) + 1) else: curArray.append(float(playerData[year][feature])) curArray = normalize(curArray, indexOfG) if curArray == None: print playerFirstName, playerLastName, target, features x.append(curArray) y.append(float(playerData[str(int(year) + 1)][target])) index = -1 for feature in features: if feature == 'G': games = float(playerData['2015'][feature]) index = features.index(feature) calculate2015data.append(float(playerData['2015'][feature])) if not x or not y: return None regr = linear_model.LinearRegression() regr.fit(x, y) prediction = regr.predict(normalize(calculate2015data, index)) val = prediction[0] if val <= 0: return 0.1 return val
def nextYearPredict(playerFirstName, playerLastName, target, batter = False): statMap = pitchStatMap if batter: statMap = batStatMap if target not in statMap.keys(): print "bad target value" data2016, null, playerData = dataUtil.getPlayerInformation(playerFirstName + " " + playerLastName) features = statMap[target] x = [] y = [] calculate2015data = [] # print playerFirstName, playerLastName, playerData if not playerData or '2015' not in playerData: return None for i, year in enumerate(playerData): if year == '2015': continue curArray = [] if not playerData.get(str(int(year) + 1), 0): continue for feature in features: curArray.append(float(playerData[year][feature])) x.append(curArray) y.append(float(playerData[str(int(year) + 1)][target])) for feature in features: calculate2015data.append(float(playerData['2015'][feature])) if not x or not y: return None regr = linear_model.LinearRegression() regr.fit(x, y) prediction = regr.predict(calculate2015data) if playerFirstName == 'Nolan' or prediction[0] < 0: print playerFirstName + " " + playerLastName, target print regr.coef_ print features print x print y print calculate2015data print prediction val = prediction[0] if val <= 0: return 0.1 return val