-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexam_multiclass_classification_sklearn.py
More file actions
66 lines (49 loc) · 2.12 KB
/
Copy pathexam_multiclass_classification_sklearn.py
File metadata and controls
66 lines (49 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# Created by "Thieu" at 13:53, 16/05/2025 ----------%
# Email: nguyenthieu2102@gmail.com %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score, train_test_split, GridSearchCV
from sklearn.pipeline import Pipeline
from pylwl import DataTransformer, LwClassifier
def get_cross_val_score(X, y, cv=3):
## Train and test
model = LwClassifier(kernel="gaussian", tau=1.0)
return cross_val_score(model, X, y, cv=cv)
def get_pipe_line(X, y):
## Split train and test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=2)
## Train and test
model = LwClassifier(kernel='gaussian', tau=1.0)
pipe = Pipeline([
("dt", DataTransformer(scaling_methods=("standard", "minmax"))),
("pnn", model)
])
pipe.fit(X_train, y_train)
y_pred = pipe.predict(X_test)
return model.evaluate(y_true=y_test, y_pred=y_pred, list_metrics=["F2S", "CKS", "FBS", "AS", "RS", "PS"])
def get_grid_search(X, y):
## Split train and test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=2)
para_grid = {
'kernel': ("gaussian", "tricube", "epanechnikov", "uniform", "cosine"),
'tau': np.linspace(1., 20., 50),
}
## Create a gridsearch
model = LwClassifier()
clf = GridSearchCV(model, para_grid, cv=3, scoring='accuracy', verbose=2)
clf.fit(X_train, y_train)
print("Best parameters found: ", clf.best_params_)
print("Best model: ", clf.best_estimator_)
print("Best training score: ", clf.best_score_)
print(clf)
## Predict
y_pred = clf.predict(X_test)
return model.evaluate(y_true=y_test, y_pred=y_pred, list_metrics=["F2S", "CKS", "FBS", "AS", "RS", "PS"])
## Load data object
X, y = load_iris(return_X_y=True)
print(get_cross_val_score(X, y, cv=3))
print(get_pipe_line(X, y))
print(get_grid_search(X, y))