|
| 1 | +import sys |
| 2 | +sys.path.append("E:/New Folder/utils") |
| 3 | + |
| 4 | +import kernel_utils as kutils |
| 5 | +import classification_utils as cutils |
| 6 | +from sklearn import model_selection, linear_model, svm, preprocessing, pipeline, neural_network |
| 7 | + |
| 8 | + |
| 9 | +#2-d classification pattern |
| 10 | +X, y = cutils.generate_nonlinear_synthetic_data_classification2(n_samples=1000, noise=0.1) |
| 11 | +X, y = cutils.generate_nonlinear_synthetic_data_classification3(n_samples=1000, noise=0.1) |
| 12 | + |
| 13 | +cutils.plot_data_2d_classification(X, y) |
| 14 | + |
| 15 | +X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.2, random_state=1) |
| 16 | +cutils.plot_data_2d_classification(X_train, y_train) |
| 17 | + |
| 18 | +#perceptron algorithm |
| 19 | +stages = [('features', kutils.KernelTransformer('rbf')) , |
| 20 | + ('clf', linear_model.Perceptron(max_iter=1000)) |
| 21 | + ] |
| 22 | +perceptron_pipeline = pipeline.Pipeline(stages) |
| 23 | +perceptron_pipeline_grid = {'features__gamma':[0.1, 0.01, 0.2]} |
| 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 = [('features', kutils.KernelTransformer('rbf')) , |
| 32 | + ('clf', linear_model.LogisticRegression()) |
| 33 | + ] |
| 34 | + |
| 35 | +lr_pipeline = pipeline.Pipeline(stages) |
| 36 | +lr_pipeline_grid = {'features__gamma':[0.1, 1, 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 | +cutils.plot_model_2d_classification(pipeline_object, X_train, y_train) |
| 41 | + |
| 42 | +#linear svm algorithm |
| 43 | +stages = [('features', kutils.KernelTransformer('poly')) , |
| 44 | + ('clf', svm.LinearSVC()) |
| 45 | + ] |
| 46 | +kernel_svm_pipeline = pipeline.Pipeline(stages) |
| 47 | +kernel_svm_pipeline_grid = {'features__degree':[2,3,4]} |
| 48 | +pipeline_object = cutils.grid_search_best_model(kernel_svm_pipeline, kernel_svm_pipeline_grid, X_train, y_train) |
| 49 | +final_estimator = pipeline_object.named_steps['clf'] |
| 50 | +print(final_estimator.intercept_) |
| 51 | +print(final_estimator.coef_) |
| 52 | +cutils.plot_model_2d_classification(pipeline_object, X_train, y_train) |
| 53 | + |
| 54 | +#out of box kernel based svm |
| 55 | +kernel_svm_estimator = svm.SVC(kernel='rbf') |
| 56 | +kernel_svm_grid = {'gamma':[0.01, 0.1, 1, 2, 5, 10], 'C':[0.001, 0.01, 0.1, 0.5] } |
| 57 | +final_estimator = cutils.grid_search_best_model(kernel_svm_estimator, kernel_svm_grid, X_train, y_train) |
| 58 | +print(final_estimator.intercept_) |
| 59 | +cutils.plot_model_2d_classification(final_estimator, X_train, y_train) |
| 60 | + |
| 61 | +kernel_svm_estimator = svm.SVC(kernel='poly') |
| 62 | +kernel_svm_grid = {'degree':[2, 3, 4], 'C':[0.001, 0.01, 0.1, 0.5] } |
| 63 | +final_estimator = cutils.grid_search_best_model(kernel_svm_estimator, kernel_svm_grid, X_train, y_train) |
| 64 | +print(final_estimator.intercept_) |
| 65 | +cutils.plot_model_2d_classification(final_estimator, X_train, y_train) |
| 66 | + |
| 67 | +#artifical neural network |
| 68 | +ann_estimator = neural_network.MLPClassifier() |
| 69 | +ann_grid = {'hidden_layer_sizes':[(3, 4), (10, 20)] } |
| 70 | +final_estimator = cutils.grid_search_best_model(ann_estimator, ann_grid, X_train, y_train) |
| 71 | +print(final_estimator.intercepts_) |
| 72 | +print(final_estimator.coefs_) |
| 73 | +cutils.plot_model_2d_classification(final_estimator, X_train, y_train) |
0 commit comments