def test_linear_regression_validation(self, get_regression_data): X, y = get_regression_data with pytest.raises(ValueError): est = LinearRegression(metric='accuracy') est.fit(X, y) with pytest.raises(ValueError): est = LinearRegression(cost='binary_cross_entropy') est.fit(X, y)
def predict_y(): X, y = datasets.load_boston(return_X_y=True) scaler = StandardScaler() X = scaler.fit_transform(X) gd = LinearRegression(epochs=5) gd.fit(X, y) y_pred = gd.predict(X) return y, y_pred
def fit_multiple_models(get_regression_data): X, y = get_regression_data X = X[:,5] X = np.reshape(X, (-1,1)) models = {} bgd = LinearRegression(epochs=200, seed=50) sgd = LinearRegression(epochs=200, seed=50, batch_size=1) mgd = LinearRegression(epochs=200, seed=50, batch_size=32) models= {'Batch Gradient Descent': bgd.fit(X,y), 'Stochastic Gradient Descent': sgd.fit(X,y), 'Mini-batch Gradient Descent': mgd.fit(X,y)} return models
# Linear Regression Scatterplot data = go.Scatter(x=X[:, 0], y=y, mode='markers', marker=dict(color='steelblue')) layout = go.Layout(title='Boston Housing Prices by Rooms', height=400, width=800, showlegend=False, xaxis_title="Average No. Rooms", yaxis_title="Median Price ($000)", margin=dict(l=10, r=10, t=40, b=10), template='plotly_white') fig = go.Figure(data=data, layout=layout) fig.show() po.plot(fig, filename="./content/figures/boston.html", auto_open=False) # ---------------------------------------------------------------------------- # # LINEAR REGRESSION # # ---------------------------------------------------------------------------- # #%% # Linear Regression lr = LinearRegression(epochs=50, learning_rate=0.05) lr.fit(X_scaled, y) plot = SingleModelSearch3D() plot.search(lr, directory=directory, filename="linear_regression_search.gif") plot = SingleModelFit2D() plot.fit(lr, directory=directory, filename="linear_regression_fit.gif") #%% # %%
def test_linear_regression_name(self, get_regression_data): X, y = get_regression_data est = LinearRegression() est.fit(X, y) assert est.name == "Linear Regression with Batch Gradient Descent", "incorrect name"
# LINEAR REGRESSION # # ---------------------------------------------------------------------------- # #%% # Linear Regression scaler = StandardScaler() scaler.fit(X_train) X_train = scaler.transform(X_train) lr = LinearRegression(epochs=1000, learning_rate=0.01, val_size=0.2, patience=40, early_stop=True, metric='r2', verbose=True, checkpoint=100) lr.fit(X_train, y_train) print(lr.intercept_) print(lr.coef_.shape) # ---------------------------------------------------------------------------- # # ANIMATIONS # # ---------------------------------------------------------------------------- # #%% # Animations plot = SingleModelSearch3D() plot.search(lr, directory=directory, filename="linear_regression_search_test.gif") plot = SingleModelFit2D() plot.fit(lr, directory=directory, filename="linear_regression_fit_test.gif") #%% # ---------------------------------------------------------------------------- #