n_features=1) X_train, X_test, y_train, y_test = model_selection.train_test_split( X, y, test_size=0.1, random_state=1) rutils.plot_data_2d_regression(X_train, y_train) kernel_lr = kernel_ridge.KernelRidge(kernel="rbf") kernel_lr_grid = { 'alpha': [0.0001, 0.01, 0.05, 0.2, 0.5, 1], 'gamma': [0.01, 0.1, 1, 2, 3, 4, 5, 10] } final_kernel_lr_model = utils.grid_search_best_model(kernel_lr, kernel_lr_grid, X_train, y_train, scoring=scoring) rutils.plot_model_2d_regression(final_kernel_lr_model, X_train, y_train) rutils.regression_performance(final_kernel_lr_model, X_test, y_test) kernel_svm = svm.SVR(kernel="rbf") kernel_svm_grid = { 'C': [0.2, 0.5, 10, 20, 50, 100], 'gamma': [0.01, 0.1, 1, 2, 3, 4, 5, 10] } final_kernel_svm_model = utils.grid_search_best_model(kernel_svm, kernel_svm_grid, X_train, y_train, scoring=scoring) rutils.plot_model_2d_regression(final_kernel_svm_model, X_train, y_train) rutils.regression_performance(final_kernel_svm_model, X_test, y_test)
model.add(Dense(units=20, input_shape=(1,), activation='relu')) model.add(Dense(units=10, activation='relu')) model.add(Dense(units=1, activation='linear')) return model model = getModel3() model.compile(optimizer='sgd', loss='mean_squared_error', metrics=[metrics.mean_squared_error]) path="I:/" early_stopping = EarlyStopping(monitor='val_loss', patience=2, verbose=1, mode='auto') tensorboard = TensorBoard(log_dir=os.path.join(path, "logs"), histogram_freq=1, write_images=True) history = model.fit(x=X_train, y=y_train, verbose=3, epochs=100, batch_size=16, validation_split=0.1, callbacks=[early_stopping, tensorboard] ) print(model.summary()) print(model.get_weights()) kutils.plot_loss(history) rutils.plot_data_2d_regression(X_train, y_train) #serialization to h5 model.save(os.path.join(path, "model.h5")) #deserialization from h5 loaded_model = models.load_model(os.path.join(path, "model.h5")) print(loaded_model.summary()) print(loaded_model.get_weights()) y_pred = loaded_model.predict(X_test) rutils.regression_performance(model, X_test, y_test)
y_train[::10] = 250 rutils.plot_data_2d_regression(X_train, y_train) # Fit linear model lr_estimator = linear_model.LinearRegression() lr_grid = {'normalize': [True, False]} lr_model = utils.grid_search_best_model(lr_estimator, lr_grid, X_train, y_train, scoring=scoring) rutils.plot_model_2d_regression(lr_model, X_train, y_train, title="LinearRegression") rutils.regression_performance(lr_model, X_test, y_test) # Robustly fit linear model with Huber Regressor algorithm hr_estimator = linear_model.HuberRegressor() hr_grid = {'epsilon': [1.1, 1.2, 1.3, 1.5]} hr_model = utils.grid_search_best_model(hr_estimator, hr_grid, X_train, y_train, scoring=scoring) rutils.plot_model_2d_regression(hr_model, X_train, y_train, title="HuberRegression") rutils.regression_performance(hr_model, X_test, y_test)
X, y = rutils.generate_nonlinear_synthetic_data_regression(n_samples=200, n_features=1) X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.1, random_state=1) rutils.plot_data_2d_regression(X_train, y_train) stages = [ ('features', preprocessing.PolynomialFeatures()), ('reg', linear_model.LinearRegression()) ] lr_pipeline = pipeline.Pipeline(stages) lr_pipeline_grid = {'reg__normalize':[True, False], 'features__degree':[2,3,5,10]} pipeline_object = utils.grid_search_best_model(lr_pipeline, lr_pipeline_grid, X_train, y_train, scoring = scoring) final_linear_model = pipeline_object.named_steps['reg'] print(final_linear_model.coef_) print(final_linear_model.intercept_) rutils.plot_model_2d_regression(pipeline_object, X_train, y_train) rutils.regression_performance(pipeline_object, X_test, y_test) stages = [ ('features', kutils.GaussianFeatures() ), ('reg', linear_model.LinearRegression()) ] lr_pipeline = pipeline.Pipeline(stages) lr_pipeline_grid = {'reg__normalize':[True, False], 'features__n_centres':[15, 20, 30, 36] } pipeline_object = utils.grid_search_best_model(lr_pipeline, lr_pipeline_grid, X_train, y_train, scoring = scoring) final_linear_model = pipeline_object.named_steps['reg'] print(final_linear_model.coef_) print(final_linear_model.intercept_) rutils.plot_model_2d_regression(pipeline_object, X_train, y_train) rutils.regression_performance(pipeline_object, X_test, y_test) stages = [
noise=200) X_train, X_test, y_train, y_test = model_selection.train_test_split( X, y, test_size=0.1, random_state=1) rutils.plot_data_2d_regression(X_train, y_train) linear_estimator = linear_model.LinearRegression() linear_grid = {'normalize': [False]} final_linear_model = utils.grid_search_best_model(linear_estimator, linear_grid, X_train, y_train, scoring=scoring) print(final_linear_model.coef_) print(final_linear_model.intercept_) rutils.plot_model_2d_regression(final_linear_model, X_train, y_train) rutils.regression_performance(final_linear_model, X_test, y_test) lasso_estimator = linear_model.Lasso(max_iter=5000) lasso_grid = {'alpha': [0, 0.1, 0.5, 1.0, 10]} final_lasso_model = utils.grid_search_best_model(lasso_estimator, lasso_grid, X_train, y_train, scoring=scoring) print(final_lasso_model.coef_) print(final_lasso_model.intercept_) rutils.plot_model_2d_regression(final_lasso_model, X_train, y_train) rutils.regression_performance(final_lasso_model, X_test, y_test) ridge_estimator = linear_model.Ridge() ridge_grid = {'alpha': [0, 0.1, 0.5, 1.0, 10]}
X_train, X_test, y_train, y_test = model_selection.train_test_split( X, y, test_size=0.1, random_state=1) rutils.plot_data_2d_regression(X_train, y_train) rf_estimator = ensemble.RandomForestRegressor() rf_grid = { 'n_estimators': list(range(10, 200, 20)), 'max_depth': list(range(3, 6)) } final_rf_model = utils.grid_search_best_model(rf_estimator, rf_grid, X_train, y_train, scoring=scoring) rutils.plot_model_2d_regression(final_rf_model, X_train, y_train) rutils.regression_performance(final_rf_model, X_test, y_test) et_estimator = ensemble.ExtraTreesRegressor() et_grid = { 'n_estimators': list(range(10, 200, 20)), 'max_depth': list(range(3, 6)) } final_et_model = utils.grid_search_best_model(et_estimator, et_grid, X_train, y_train, scoring=scoring) rutils.plot_model_2d_regression(final_et_model, X_train, y_train) rutils.regression_performance(final_et_model, X_test, y_test) dt_estimator = tree.DecisionTreeRegressor()