Scikit-learn Linear Regression

Code credit:
Scikit-learn
https://www.udemy.com/machinelearning/

from sklearn.metrics import mean_squared_error, r2_score

# Split
# No Scale

# Fit
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)

# test set prediction results
yhat = regressor.predict(X_test)
print(mean_squared_error(y_true=y_test, y_pred=yhat))
print(r2_score(y_test, yhat))


Stats Model Ordinary Least Squares

Code credit:
https://www.statsmodels.org/stable/index.html
http://www.statsmodels.org/dev/endog_exog.html

import numpy as np
import statsmodels.api as sm

# add the bias
np.append(arr = np.ones((len(x), 1)).astype(int), values = x, axis = 1)

# feature array
features = x[:, [0, 1, 2, 3]]

# import statsmodels.formula.api as sm
regressor = sm.OLS(endog = y, exog = features).fit()
regressor.summary()