|
1 | 1 | import sys
|
2 | 2 | sys.path.append("E:/New Folder/utils")
|
3 | 3 |
|
| 4 | +import kernel_utils as kutils |
4 | 5 | import classification_utils as cutils
|
5 | | -from sklearn import model_selection, linear_model, svm, preprocessing, pipeline |
| 6 | +from sklearn import model_selection, linear_model, svm, preprocessing, pipeline, neural_network |
6 | 7 |
|
7 | 8 |
|
8 | 9 | #2-d classification pattern
|
|
15 | 16 | cutils.plot_data_2d_classification(X_train, y_train)
|
16 | 17 |
|
17 | 18 | #perceptron algorithm
|
18 | | -stages = [ |
19 | | - ('features', preprocessing.PolynomialFeatures()), |
20 | | - ('clf', linear_model.Perceptron(max_iter=1000)) |
21 | | - ] |
| 19 | +stages = [('features', kutils.KernelTransformer('rbf')) , |
| 20 | + ('clf', linear_model.Perceptron(max_iter=1000)) |
| 21 | + ] |
22 | 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]} |
| 23 | +perceptron_pipeline_grid = {'features__gamma':[0.1, 0.01, 0.2]} |
24 | 24 | pipeline_object = cutils.grid_search_best_model(perceptron_pipeline, perceptron_pipeline_grid, X_train, y_train)
|
25 | 25 | final_estimator = pipeline_object.named_steps['clf']
|
26 | 26 | print(final_estimator.intercept_)
|
27 | 27 | print(final_estimator.coef_)
|
28 | 28 | cutils.plot_model_2d_classification(pipeline_object, X_train, y_train)
|
29 | 29 |
|
30 | 30 | #logistic regression algorithm
|
31 | | -stages = [ |
32 | | - ('features', preprocessing.PolynomialFeatures()), |
33 | | - ('clf', linear_model.LogisticRegression()) |
34 | | - ] |
| 31 | +stages = [('features', kutils.KernelTransformer('rbf')) , |
| 32 | + ('clf', linear_model.LogisticRegression()) |
| 33 | + ] |
| 34 | + |
35 | 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]} |
| 36 | +lr_pipeline_grid = {'features__gamma':[0.1, 1, 5,10]} |
37 | 37 | pipeline_object = cutils.grid_search_best_model(lr_pipeline, lr_pipeline_grid, X_train, y_train)
|
38 | 38 | final_estimator = pipeline_object.named_steps['clf']
|
39 | 39 | print(final_estimator.intercept_)
|
40 | | -print(final_estimator.coef_) |
41 | 40 | cutils.plot_model_2d_classification(pipeline_object, X_train, y_train)
|
42 | 41 |
|
43 | 42 | #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'] |
| 43 | +kernel_svm_estimator = svm.SVC(kernel='rbf') |
| 44 | +kernel_svm_grid = {'gamma':[0.01, 0.1, 1, 2, 5, 10] } |
| 45 | +final_estimator = cutils.grid_search_best_model(kernel_svm_estimator, kernel_svm_grid, X_train, y_train) |
52 | 46 | print(final_estimator.intercept_)
|
53 | | -print(final_estimator.coef_) |
54 | | -cutils.plot_model_2d_classification(pipeline_object, X_train, y_train) |
| 47 | +cutils.plot_model_2d_classification(final_estimator, X_train, y_train) |
| 48 | + |
| 49 | +#artifical neural network |
| 50 | +ann_estimator = neural_network.MLPClassifier() |
| 51 | +ann_grid = {'hidden_layer_sizes':[(3, 4), (10, 20)] } |
| 52 | +final_estimator = cutils.grid_search_best_model(ann_estimator, ann_grid, X_train, y_train) |
| 53 | +print(final_estimator.intercepts_) |
| 54 | +print(final_estimator.coefs_) |
| 55 | +cutils.plot_model_2d_classification(final_estimator, X_train, y_train) |
0 commit comments