I am new to ML and I wanted to implement a Linear Regression to predict a golfer's scores based on certain feature supplied to my model. I would like a review to see if I'm implementing this correctly and any tips to make my model more efficient. Here is my code

import pandas as pd
import numpy as np
from sklearn.linear_model import LassoCV
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
from sklearn.preprocessing import StandardScaler

df = pd.read_csv("/Users/aus10/Desktop/PGA/History/Workday_Charity/PGA_TEST_FEAT.csv")
df_model = df.copy()
scaler = StandardScaler()

features = [['SG:ATG', 'FH', 'SG:APP', 'AVG:Putts:GIR', 'SG:Putting']]
for feature in features:
    df_model[feature] = scaler.fit_transform(df_model[feature])

SG_data = pd.read_csv("/Users/aus10/Desktop/PGA/History/Workday_Charity/SG.csv")
x = df.iloc[:,1:6]  #independent columns
y = df.iloc[:,-1]    #target column i.e price range

results = []

# fit final model
model = LassoCV(cv=5, random_state=0)
model.fit(x, y)

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=2)

model.fit(x_train, y_train)

y_pred = model.predict(x_test)

print( round(r2_score(y_test, y_pred),2) )

# define one new data instance

index = 0
count = 0

while count < len(SG_data):
    name = SG_data.loc[index].at['Name']
    strokes_gained_atg = SG_data.loc[index].at['SG:ATG']
    fairways_hit = SG_data.loc[index].at['FH']
    strokes_gained_approach = SG_data.loc[index].at['SG:APP']
    average_putts_gir = SG_data.loc[index].at['AVG:Putts:GIR']
    strokes_gained_putting = SG_data.loc[index].at['SG:Putting']

    Xnew = [[ strokes_gained_atg, fairways_hit, strokes_gained_approach, average_putts_gir, strokes_gained_putting ]]
    # make a prediction
    ynew = model.predict(Xnew)
    # show the inputs and predicted outputs
            'Name': name,
            'Projection': (round(ynew[0],2))
    index += 1
    count += 1
sorted_results = sorted(results, key=lambda k: k['Projection'], reverse=True)

df = pd.DataFrame(sorted_results, columns=[
    'Name', 'Projection'])
writer = pd.ExcelWriter('/Users/aus10/Desktop/PGA/Regressions/Linear_Regressions/Results/Workday_Projections_LR_Lasso.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
df.style.set_properties(**{'text-align': 'center'})
pd.set_option('display.max_colwidth', 100)
pd.set_option('display.width', 1000)

attached are the two csv files. https://docs.google.com/spreadsheets/d/1BH7_3Z0o2_VAI81p0ZuVZ3-0kO0n7ZrU5iBHF-WTAaw/edit?usp=sharing


  • \$\begingroup\$ So PGA_TEST_FEAT contains all the statistic related to fantasy performance. I used a feature selection method to find the features that correlated most with fantasy output. The SG_data is all the current stats for each golf prior to the tournament. @Reinderien \$\endgroup\$ – Austin Johnson Jul 13 '20 at 18:53
  • 1
    \$\begingroup\$ @Reinderien done \$\endgroup\$ – Austin Johnson Jul 13 '20 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.