|
| 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, preprocessing, pipeline |
| 6 | + |
| 7 | + |
| 8 | +#2-d classification pattern |
| 9 | +X, y = cutils.generate_nonlinear_synthetic_data_classification2(n_samples=1000, noise=0.1) |
| 10 | +X, y = cutils.generate_nonlinear_synthetic_data_classification3(n_samples=1000, noise=0.1) |
| 11 | + |
| 12 | +cutils.plot_data_2d_classification(X, y) |
| 13 | + |
| 14 | +X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.2, random_state=1) |
| 15 | +cutils.plot_data_2d_classification(X_train, y_train) |
| 16 | + |
| 17 | +#perceptron algorithm |
| 18 | +stages = [ |
| 19 | + ('features', preprocessing.PolynomialFeatures()), |
| 20 | + ('clf', linear_model.Perceptron(max_iter=1000)) |
| 21 | + ] |
| 22 | +perceptron_pipeline = pipeline.Pipeline(stages) |
| 23 | +perceptron_pipeline_grid = {'clf__penalty':['l1'], 'clf__alpha':[0, 0.1, 0.3, 0.5], 'features__degree':[2,3,5,10]} |
| 24 | +pipeline_object = cutils.grid_search_best_model(perceptron_pipeline, perceptron_pipeline_grid, X_train, y_train) |
| 25 | +final_estimator = pipeline_object.named_steps['clf'] |
| 26 | +print(final_estimator.intercept_) |
| 27 | +print(final_estimator.coef_) |
| 28 | +cutils.plot_model_2d_classification(pipeline_object, X_train, y_train) |
| 29 | + |
| 30 | +#logistic regression algorithm |
| 31 | +stages = [ |
| 32 | + ('features', preprocessing.PolynomialFeatures()), |
| 33 | + ('clf', linear_model.LogisticRegression()) |
| 34 | + ] |
| 35 | +lr_pipeline = pipeline.Pipeline(stages) |
| 36 | +lr_pipeline_grid = {'clf__penalty':['l1'], 'clf__C':[0.01, 0.1, 0.3, 0.5], 'features__degree':[2,3,5,10]} |
| 37 | +pipeline_object = cutils.grid_search_best_model(lr_pipeline, lr_pipeline_grid, X_train, y_train) |
| 38 | +final_estimator = pipeline_object.named_steps['clf'] |
| 39 | +print(final_estimator.intercept_) |
| 40 | +print(final_estimator.coef_) |
| 41 | +cutils.plot_model_2d_classification(pipeline_object, X_train, y_train) |
| 42 | + |
| 43 | +#linear svm algorithm |
| 44 | +stages = [ |
| 45 | + ('features', preprocessing.PolynomialFeatures()), |
| 46 | + ('clf', svm.LinearSVC()) |
| 47 | + ] |
| 48 | +svm_pipeline = pipeline.Pipeline(stages) |
| 49 | +svm_pipeline_grid = {'clf__penalty':['l2'], 'clf__C':[0.01, 0.1, 0.3, 0.5], 'features__degree':[2,3,5,10, 50, 100]} |
| 50 | +pipeline_object = cutils.grid_search_best_model(svm_pipeline, svm_pipeline_grid, X_train, y_train) |
| 51 | +final_estimator = pipeline_object.named_steps['clf'] |
| 52 | +print(final_estimator.intercept_) |
| 53 | +print(final_estimator.coef_) |
| 54 | +cutils.plot_model_2d_classification(pipeline_object, X_train, y_train) |
0 commit comments