|
| 1 | +import sys |
| 2 | +sys.path.append("E:/New Folder/utils") |
| 3 | + |
| 4 | +import classification_utils as cutils |
| 5 | +from sklearn import model_selection, linear_model, datasets |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +#generate data with noise(irrelevant) features |
| 9 | +X, y = datasets.make_classification(n_samples=100, n_features=20, n_informative=2, n_redundant=4, n_repeated=0, n_classes=2) |
| 10 | +X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.2, random_state=1) |
| 11 | + |
| 12 | +np.corrcoef(X_train, rowvar=False) |
| 13 | + |
| 14 | +#overfit |
| 15 | +perceptron_estimator = linear_model.Perceptron(max_iter=1000) |
| 16 | +perceptron_grid = {'alpha':[0] } |
| 17 | +final_estimator = cutils.grid_search_best_model(perceptron_estimator, perceptron_grid, X_train, y_train) |
| 18 | +print(final_estimator.intercept_) |
| 19 | +print(final_estimator.coef_) |
| 20 | + |
| 21 | +#overfit control |
| 22 | +perceptron_estimator = linear_model.Perceptron(max_iter=1000) |
| 23 | +perceptron_grid = { |
| 24 | + 'penalty':['l1', 'l2'], 'alpha':[0, 0.00001, 0.0001, 0.0005, 0.001, 0.01, 0.1, 0.2, 0.5, 1, 3] } |
| 25 | +final_estimator = cutils.grid_search_best_model(perceptron_estimator, perceptron_grid, X_train, y_train) |
| 26 | +print(final_estimator.intercept_) |
| 27 | +print(final_estimator.coef_) |
| 28 | + |
| 29 | +#overfitting |
| 30 | +lr_estimator = linear_model.LogisticRegression() |
| 31 | +lr_grid = {'C':[1e20] } |
| 32 | +final_estimator = cutils.grid_search_best_model(lr_estimator, lr_grid, X_train, y_train) |
| 33 | +print(final_estimator.intercept_) |
| 34 | +print(final_estimator.coef_) |
| 35 | + |
| 36 | +#overfit control |
| 37 | +lr_estimator = linear_model.LogisticRegression() |
| 38 | +lr_grid = {'penalty':['l1', 'l2'], 'C':[0.001, 0.1, 0.5, 1, 10, 1e2] } |
| 39 | +final_estimator = cutils.grid_search_best_model(lr_estimator, lr_grid, X_train, y_train) |
| 40 | +print(final_estimator.intercept_) |
| 41 | +print(final_estimator.coef_) |
0 commit comments