NBA = pd.read_csv("NBA_train.csv")

"""
Explanation: Video 1
End of explanation
"""


NBA

"""
Explanation: Simply printing out the DataFrame will give us similar information to R's str():
End of explanation
"""


NBA.describe()

"""
Explanation: And for the summary we'll use an equivalent method, DataFrame.describe():
End of explanation
"""


NBA[['Playoffs', 'W']].groupby('W').aggregate(sum)

"""
Explanation: Video 2
Now we want to create a table with Playoffs and Wins. In R, the command is table(NBA$W, NBA$Playoffs).
End of explanation
"""


NBA['PTSdiff'] = NBA['PTS'] - NBA['oppPTS']

NBA

"""
Explanation: Create a new column in NBA, which contains the difference between points scored and lost. (R: NBA$PTSdiff = NBA$PTS - NBA$oppPTS)
End of explanation
"""


NBA.plot(x='PTSdiff', y='W', kind='scatter')

"""
Explanation: Now we plot Wins as a function of points difference to get an idea whether there is a corellation.
End of explanation
"""


from sklearn.linear_model import LinearRegression

"""
Explanation: To build a linear regression model, I followed this blog post. We need to import LinearRegression from scikit-learn:
End of explanation
"""


lr = LinearRegression()
lr.fit(NBA[['PTSdiff']], NBA[['W']])

"""
Explanation: And build the model:
End of explanation
"""


print(lr.score(NBA[['PTSdiff']], NBA[['W']]))    # R^2
print(lr.intercept_)
print(lr.coef_)

"""
Explanation: I didn't find a method that would print a nice R-like summary of the model in python, so we need to print them manually:
End of explanation
"""


from pandas.stats.api import ols
model = ols(x=NBA['PTSdiff'], y=NBA['W'])

model

"""
Explanation: I've just found a method in pandas that prints out a summary!
End of explanation
"""


NBA['X2PA'] = NBA['2PA']
NBA['X3PA'] = NBA['3PA']

import statsmodels.formula.api as sm
model = sm.ols(formula="PTS ~ X2PA + X3PA + FTA + AST + ORB + DRB + TOV + STL + BLK", data=NBA).fit()

model.summary()

"""
Explanation: Multiple linear regression (Video 3)
There are several ways of creating a multidimensional model, but I like this one: link.
First, we need to convert column names like in R --- otherwise the methods will throw an error
End of explanation
"""


model.resid

SSE = sum(model.resid**2)
SSE

RMSE = np.sqrt(SSE/len(NBA))
RMSE

np.mean(NBA['PTS'])

model1 = sm.ols(formula="PTS ~ X2PA + X3PA + FTA + AST + ORB + DRB + STL + BLK", data=NBA).fit()
model1.summary()

model2 = sm.ols(formula="PTS ~ X2PA + X3PA + FTA + AST + ORB + STL + BLK", data=NBA).fit()
model2.summary()

model3 = sm.ols(formula="PTS ~ X2PA + X3PA + FTA + AST + ORB + STL", data=NBA).fit()
model3.summary()

SSE3 = sum(model3.resid**2)
SSE3

RMSE3 = np.sqrt(SSE3/len(NBA))
RMSE3

"""
Explanation: Let's print the residuals:
End of explanation
"""


NBA_test = pd.read_csv("NBA_test.csv")
NBA_test['X2PA'] = NBA_test['2PA']
NBA_test['X3PA'] = NBA_test['3PA']

prediction = model3.predict(NBA_test)

SSE_pred = sum((prediction-NBA_test['PTS'])**2)

SST_pred = sum((np.mean(NBA['PTS']) - NBA_test['PTS'])**2)

R2 = 1-SSE_pred/SST_pred
R2

RMSE_pred = np.sqrt(SSE_pred/len(NBA_test))
RMSE_pred

"""
Explanation: Video 4: Making predictions
End of explanation
"""


