|
| 1 | +import sys |
| 2 | +path = 'I://New Folder//utils' |
| 3 | +sys.path.append(path) |
| 4 | +import common_utils as utils |
| 5 | +import regression_utils as rutils |
| 6 | +from sklearn import metrics, linear_model, svm, model_selection |
| 7 | + |
| 8 | +scoring = metrics.make_scorer(rutils.rmse, greater_is_better=False) |
| 9 | + |
| 10 | +#linear pattern in 2d |
| 11 | +X, y = rutils.generate_linear_synthetic_data_regression(n_samples=100, n_features=1, |
| 12 | + n_informative=1, |
| 13 | + noise = 200) |
| 14 | +X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.1, random_state=1) |
| 15 | +rutils.plot_data_2d_regression(X_train, y_train) |
| 16 | + |
| 17 | +linear_estimator = linear_model.LinearRegression() |
| 18 | +linear_grid = {'normalize': [False]} |
| 19 | +final_linear_model = utils.grid_search_best_model(linear_estimator, linear_grid, X_train, y_train, scoring = scoring) |
| 20 | +print(final_linear_model.coef_) |
| 21 | +print(final_linear_model.intercept_) |
| 22 | +rutils.plot_model_2d_regression(final_linear_model, X_train, y_train) |
| 23 | +rutils.regression_performance(final_linear_model, X_test, y_test) |
| 24 | + |
| 25 | +lasso_estimator = linear_model.Lasso(max_iter=5000) |
| 26 | +lasso_grid = {'alpha': [0, 0.1, 0.5, 1.0, 10]} |
| 27 | +final_lasso_model = utils.grid_search_best_model(lasso_estimator, lasso_grid, X_train, y_train, scoring = scoring) |
| 28 | +print(final_lasso_model.coef_) |
| 29 | +print(final_lasso_model.intercept_) |
| 30 | +rutils.plot_model_2d_regression(final_lasso_model, X_train, y_train) |
| 31 | +rutils.regression_performance(final_lasso_model, X_test, y_test) |
| 32 | + |
| 33 | +ridge_estimator = linear_model.Ridge() |
| 34 | +ridge_grid = {'alpha': [0, 0.1, 0.5, 1.0, 10]} |
| 35 | +final_ridge_model = utils.grid_search_best_model(ridge_estimator, ridge_grid, X_train, y_train, scoring = scoring) |
| 36 | +print(final_ridge_model.coef_) |
| 37 | +print(final_ridge_model.intercept_) |
| 38 | +rutils.plot_model_2d_regression(final_ridge_model, X_train, y_train) |
| 39 | +rutils.regression_performance(final_ridge_model, X_test, y_test) |
| 40 | + |
| 41 | +svm_estimator = svm.LinearSVR() |
| 42 | +svm_grid = {'C':[0.1, 0.3, 0.5, 0.7, 1, 10] } |
| 43 | +final_svm_model = utils.grid_search_best_model(svm_estimator, svm_grid, X_train, y_train, scoring = scoring) |
| 44 | +print(final_svm_model.coef_) |
| 45 | +print(final_svm_model.intercept_) |
| 46 | +rutils.plot_model_2d_regression(final_svm_model, X_train, y_train) |
| 47 | +rutils.regression_performance(final_svm_model, X_test, y_test) |
| 48 | + |
| 49 | +#linear pattern in 3d |
| 50 | +X, y = rutils.generate_linear_synthetic_data_regression(n_samples=200, n_features=2, |
| 51 | + n_informative=2, |
| 52 | + noise = 10) |
| 53 | +X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.1, random_state=1) |
| 54 | +rutils.plot_data_3d_regression(X_train, y_train) |
| 55 | + |
| 56 | +linear_estimator = linear_model.LinearRegression() |
| 57 | +linear_grid = {'normalize': [True, False]} |
| 58 | +final_linear_model = utils.grid_search_best_model(linear_estimator, linear_grid, X_train, y_train, scoring=scoring) |
| 59 | +print(final_linear_model.coef_) |
| 60 | +print(final_linear_model.intercept_) |
| 61 | +rutils.plot_model_3d_regression(final_linear_model, X_train, y_train) |
| 62 | +rutils.regression_performance(final_linear_model, X_test, y_test) |
| 63 | + |
| 64 | +svm_estimator = svm.LinearSVR() |
| 65 | +svm_grid = {'C':[0.1, 0.3, 0.5, 0.7, 1, 10] } |
| 66 | +final_svm_model = utils.grid_search_best_model(svm_estimator, svm_grid, X_train, y_train, scoring = scoring) |
| 67 | +print(final_svm_model.coef_) |
| 68 | +print(final_svm_model.intercept_) |
| 69 | +rutils.plot_model_3d_regression(final_svm_model, X_train, y_train) |
| 70 | +rutils.regression_performance(final_svm_model, X_test, y_test) |
0 commit comments