|
| 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 |
| 6 | + |
| 7 | +X, y = cutils.generate_linear_synthetic_data_classification(n_samples=1000, n_features=2, n_classes=2, weights=[0.8,0.2], class_sep=1.0) |
| 8 | + |
| 9 | +X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.2, random_state=1) |
| 10 | +cutils.plot_data_2d_classification(X_train, y_train) |
| 11 | + |
| 12 | +lr_estimator = linear_model.LogisticRegression() |
| 13 | +lr_grid = {'penalty':['l1', 'l2'], 'C':[0.01, 0.001, 0.1, 0.3, 0.5, 0.7, 1] } |
| 14 | +final_estimator = cutils.grid_search_best_model(lr_estimator, lr_grid, X_train, y_train, scoring='roc_auc') |
| 15 | +print(final_estimator.intercept_) |
| 16 | +print(final_estimator.coef_) |
| 17 | +cutils.plot_model_2d_classification(final_estimator, X_train, y_train) |
| 18 | + |
| 19 | +final_estimator.predict_proba(X_test) |
| 20 | +cutils.performance_metrics_soft_binary_classification(final_estimator, X_test, y_test) |
| 21 | + |
| 22 | +#imbalanced binary classification |
| 23 | +X, y = cutils.generate_linear_synthetic_data_classification(n_samples=1000, n_features=2, n_classes=2, weights=[0.05,0.95], class_sep=0.9) |
| 24 | + |
| 25 | +X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.2, random_state=1) |
| 26 | +cutils.plot_data_2d_classification(X_train, y_train) |
| 27 | + |
| 28 | +lr_estimator = linear_model.LogisticRegression() |
| 29 | +lr_grid = {'penalty':['l1', 'l2'], 'C':[0.01, 0.001, 0.1, 0.3, 0.5, 0.7, 1] } |
| 30 | +final_estimator = cutils.grid_search_best_model(lr_estimator, lr_grid, X_train, y_train, scoring='roc_auc') |
| 31 | +print(final_estimator.intercept_) |
| 32 | +print(final_estimator.coef_) |
| 33 | +cutils.plot_model_2d_classification(final_estimator, X_train, y_train) |
| 34 | + |
| 35 | +cutils.performance_metrics_soft_binary_classification(final_estimator, X_test, y_test) |
| 36 | + |
| 37 | +#imbalanced multi-class classification |
| 38 | +X, y = cutils.generate_linear_synthetic_data_classification(n_samples=1000, n_features=2, n_classes=3, weights=[0.6,0.3,0.1], class_sep=1.5) |
| 39 | + |
| 40 | +X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.2, random_state=1) |
| 41 | +cutils.plot_data_2d_classification(X_train, y_train) |
| 42 | + |
| 43 | +lr_estimator = linear_model.LogisticRegression() |
| 44 | +lr_grid = {'penalty':['l1', 'l2'], 'C':[0.01, 0.001, 0.1, 0.3, 0.5, 0.7, 1] } |
| 45 | +final_estimator = cutils.grid_search_best_model(lr_estimator, lr_grid, X_train, y_train, scoring='roc_auc') |
| 46 | +print(final_estimator.intercept_) |
| 47 | +print(final_estimator.coef_) |
| 48 | +cutils.plot_model_2d_classification(final_estimator, X_train, y_train) |
| 49 | + |
| 50 | +cutils.performance_metrics_soft_multiclass_classification(final_estimator, X_test, y_test) |
0 commit comments