Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d9c206e

Browse files
author
Algorithmica
authored
Add files via upload
1 parent 585c863 commit d9c206e

File tree

3 files changed

+92
-19
lines changed

3 files changed

+92
-19
lines changed

‎2019-october/8.geometrical viz - classification algorithms/non linear models-1.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import xgboost as xgb
77

88
#2-d classification pattern
9+
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)
910
X, y = cutils.generate_nonlinear_synthetic_data_classification2(n_samples=1000, noise=0.1)
1011
cutils.plot_data_2d_classification(X, y)
1112

‎2019-october/8.geometrical viz - classification algorithms/non linear models-2.py‎

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import classification_utils as cutils
66
from sklearn import model_selection, linear_model, svm, preprocessing, pipeline, neural_network
77

8-
98
#2-d classification pattern
109
X, y = cutils.generate_nonlinear_synthetic_data_classification2(n_samples=1000, noise=0.1)
1110
X, y = cutils.generate_nonlinear_synthetic_data_classification3(n_samples=1000, noise=0.1)
@@ -16,9 +15,10 @@
1615
cutils.plot_data_2d_classification(X_train, y_train)
1716

1817
#perceptron algorithm
19-
stages = [('features', kutils.KernelTransformer('rbf')) ,
20-
('clf', linear_model.Perceptron(max_iter=1000))
21-
]
18+
stages = [
19+
('features', preprocessing.PolynomialFeatures()),
20+
('clf', linear_model.Perceptron(max_iter=1000))
21+
]
2222
perceptron_pipeline = pipeline.Pipeline(stages)
2323
perceptron_pipeline_grid = {'features__gamma':[0.1, 0.01, 0.2]}
2424
pipeline_object = cutils.grid_search_best_model(perceptron_pipeline, perceptron_pipeline_grid, X_train, y_train)
@@ -28,9 +28,10 @@
2828
cutils.plot_model_2d_classification(pipeline_object, X_train, y_train)
2929

3030
#logistic regression algorithm
31-
stages = [('features', kutils.KernelTransformer('rbf')) ,
32-
('clf', linear_model.LogisticRegression())
33-
]
31+
stages = [
32+
('features', preprocessing.PolynomialFeatures()),
33+
('clf', linear_model.LogisticRegression())
34+
]
3435

3536
lr_pipeline = pipeline.Pipeline(stages)
3637
lr_pipeline_grid = {'features__gamma':[0.1, 1, 5,10]}
@@ -40,16 +41,14 @@
4041
cutils.plot_model_2d_classification(pipeline_object, X_train, y_train)
4142

4243
#linear svm algorithm
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)
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']
4652
print(final_estimator.intercept_)
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)
53+
print(final_estimator.coef_)
54+
cutils.plot_model_2d_classification(pipeline_object, X_train, y_train)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /