|
| 1 | +#!/usr/bin/env python |
| 2 | +# Created by "Thieu" at 15:35, 01/06/2025 ----------% |
| 3 | + |
| 4 | +# Github: https://github.com/thieu1995 % |
| 5 | +# --------------------------------------------------% |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +from mafese import get_dataset, MhaSelector, FeatureSelectionProblem |
| 9 | + |
| 10 | + |
| 11 | +# Define a custom callback objective function |
| 12 | +def my_custom_problem(**kwargs): |
| 13 | + |
| 14 | + class MyProblem(FeatureSelectionProblem): |
| 15 | + # Please check out the FeatureSelectionProblem class in mafese/utils/mealpy_util.py to know which attributes are existing in the object |
| 16 | + |
| 17 | + def obj_func(self, solution): |
| 18 | + # Decode the solution to get the selected features |
| 19 | + x = self.decode_solution(solution)["my_var"] # Please don't change this line, it is used to decode the solution |
| 20 | + cols = np.flatnonzero(x) # Get columns where features are selected |
| 21 | + |
| 22 | + # In self.data object we have train and test data, we can just use them directly |
| 23 | + |
| 24 | + # Fit the estimator on the selected features |
| 25 | + self.estimator.fit(self.data.X_train[:, cols], self.data.y_train) |
| 26 | + |
| 27 | + # Predict on the test set |
| 28 | + y_valid_pred = self.estimator.predict(self.data.X_test[:, cols]) |
| 29 | + |
| 30 | + # Evaluate using a custom metric (e.g., F1 score) |
| 31 | + evaluator = self.metric_class(self.data.y_test, y_valid_pred) |
| 32 | + obj = evaluator.get_metric_by_name("NPV", paras=self.obj_paras)["NPV"] # Change "NPV" to your desired metric name |
| 33 | + |
| 34 | + # Calculate fitness |
| 35 | + fitness = self.fit_weights[0] * obj + self.fit_weights[1] * self.fit_sign * (np.sum(x) / self.n_dims) |
| 36 | + return [fitness, obj, int(np.sum(x))] # Return fitness, objective value, and number of selected features |
| 37 | + |
| 38 | + return MyProblem(**kwargs) |
| 39 | + |
| 40 | + |
| 41 | +data = get_dataset("aniso") |
| 42 | +data.split_train_test(test_size=0.2) |
| 43 | + |
| 44 | +selector = MhaSelector(problem="classification", obj_name="F1S", |
| 45 | + estimator="svm", estimator_paras=None, |
| 46 | + optimizer="BaseGA", optimizer_paras={"epoch": 100, "pop_size": 20, "name": "GA"}, |
| 47 | + mode='single', n_workers=None, termination=None, |
| 48 | + seed=42, verbose=True) |
| 49 | +selector.fit(data.X_train, data.y_train, fs_problem=my_custom_problem) |
| 50 | + |
| 51 | +# Transform test data |
| 52 | +X_selected = selector.transform(data.X_test) |
| 53 | +print(f"Original Dataset: {data.X_train.shape}") |
| 54 | +print(f"Selected dataset: {X_selected.shape}") |
| 55 | + |
| 56 | +# Get some information |
| 57 | +print(selector.get_best_information()) |
| 58 | +print(selector.selected_feature_masks) |
| 59 | +print(selector.selected_feature_solution) |
| 60 | +print(selector.selected_feature_indexes) |
| 61 | + |
| 62 | +# Predict with new selected features |
| 63 | +res1 = selector.evaluate(estimator=None, estimator_paras=None, data=data, metrics=["AS", "PS", "RS"]) |
| 64 | +# AS: Accuracy score, PS: precision score, RS: recall score |
| 65 | +print(res1) |
| 66 | + |
| 67 | + |
0 commit comments