|
| 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, svm |
| 6 | + |
| 7 | +#2-d classification pattern |
| 8 | +X, y = cutils.generate_linear_synthetic_data_classification(n_samples=1000, n_features=2, n_classes=2, weights=[0.5, 0.5], class_sep=2) |
| 9 | +X, y = cutils.generate_nonlinear_synthetic_data_classification2(n_samples=1000, noise=0.1) |
| 10 | +cutils.plot_data_2d_classification(X, y) |
| 11 | + |
| 12 | +X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.2, random_state=1) |
| 13 | +cutils.plot_data_2d_classification(X_train, y_train) |
| 14 | + |
| 15 | +#perceptron algorithm |
| 16 | +perceptron_estimator = linear_model.Perceptron() |
| 17 | +perceptron_grid = {'penalty':['l1', 'l2'], 'alpha':[0, 0.01, 0.02, 0.1, 0.3, 0.5, 0.7, 1] } |
| 18 | +final_estimator = cutils.grid_search_best_model(perceptron_estimator, perceptron_grid, X_train, y_train) |
| 19 | +print(final_estimator.intercept_) |
| 20 | +print(final_estimator.coef_) |
| 21 | +cutils.plot_model_2d_classification(final_estimator, X_train, y_train) |
| 22 | + |
| 23 | +#predict distances and classes for test data |
| 24 | +print(final_estimator.decision_function(X_test)) |
| 25 | +print(final_estimator.predict(X_test)) |
| 26 | + |
| 27 | +#logistic regression algorithm |
| 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) |
| 31 | +print(final_estimator.intercept_) |
| 32 | +print(final_estimator.coef_) |
| 33 | +cutils.plot_model_2d_classification(final_estimator, X_train, y_train) |
| 34 | + |
| 35 | +#predict distances and classes for test data |
| 36 | +print(final_estimator.decision_function(X_test)) |
| 37 | +print(final_estimator.predict(X_test)) |
| 38 | +print(final_estimator.predict_proba(X_test)) |
| 39 | + |
| 40 | +#linear svm algorithm |
| 41 | +lsvm_estimator = svm.LinearSVC() |
| 42 | +lsvm_grid = {'penalty':['l2'], 'C':[0.1, 0.3, 0.5, 0.7, 1, 10] } |
| 43 | +final_estimator = cutils.grid_search_best_model(lsvm_estimator, lsvm_grid, X_train, y_train) |
| 44 | +print(final_estimator.intercept_) |
| 45 | +print(final_estimator.coef_) |
| 46 | +cutils.plot_model_2d_classification(final_estimator, X_train, y_train) |
| 47 | + |
| 48 | +#predict distances and classes for test data |
| 49 | +print(final_estimator.decision_function(X_test)) |
| 50 | +print(final_estimator.predict(X_test)) |
0 commit comments