import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from LinearRegression import LinReg
#%run -i LinearRegression.py
np.random.seed(1)
n = 500
x1 = np.random.uniform(0, 10, n)
x2 = np.random.uniform(0, 50, n)
x3 = np.random.uniform(100, 200, n)
y = 3 + 4.2 * x1 + 0.7 * x2 + 0.2 * x3 + np.random.normal(0, 6, n)
df = pd.DataFrame({'x1':x1, 'x2':x2, 'x3':x3, 'y':y})
df.head()
X = np.hstack([x1.reshape(n,1), x2.reshape(n,1), x3.reshape(n,1)])
print(X.shape)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
mod = LinReg(X_train, y_train)
print('Model coefficients:', mod.coefficients)
print('Training r-Squared:', mod.r_squared)
print('Testing r-Squared: ', mod.score(X_test, y_test))
mod.summary()
sk_mod = LinearRegression()
sk_mod.fit(X_train, y_train)
print('Model intercept: ', sk_mod.intercept_)
print('Model coefficients:', sk_mod.coef_)
print('Training r-Squared:', sk_mod.score(X_train,y_train))
print('Testing r-Squared: ', sk_mod.score(X_test,y_test))