Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DrafonFlySelectionCV with sklearn-api #33

Open
wants to merge 48 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
3f12c19
Update typo of 'quadraic' to 'quadratic'
akhil-maker Oct 7, 2022
346f9a2
fix: docs/requirement.txt to reduce vulnerabilities
snyk-bot Dec 23, 2022
4da83e1
Merge pull request #24 from jaswinder9051998/snyk-fix-0b9536ebf116747…
jaswinder9051998 Jan 12, 2023
09e2d66
Merge pull request #21 from akhil-maker/master
jaswinder9051998 Jan 12, 2023
7551361
Revert "Update typo of 'quadraic' to 'quadratic'"
jaswinder9051998 Jan 12, 2023
f2b5536
Merge pull request #26 from jaswinder9051998/revert-21-master
jaswinder9051998 Jan 12, 2023
934c463
Added method _get_bestScore to get the "right" best scores, adjusted …
Feb 8, 2023
1c6ee7d
Merge branch 'master' of github.com:RenZhen95/zoofs
Feb 8, 2023
7c502b8
Merge pull request #27 from RenZhen95/master
jaswinder9051998 Feb 9, 2023
da62f1d
Create __init__.py
jckkvs Feb 19, 2023
6be34ed
sklearn-api added
jckkvs Feb 19, 2023
a174ec3
trial_drafonfly
jckkvs Feb 19, 2023
c9c682b
Update dragon_fly.py
jckkvs Feb 25, 2023
23ec3f6
fix sonarcloud
jckkvs Feb 26, 2023
6c5e906
delete "not used function"
jckkvs Feb 26, 2023
69ce16d
delete __version__
jckkvs Feb 26, 2023
9556da1
minor change
jckkvs Feb 26, 2023
eab4e65
minor changee
jckkvs Feb 26, 2023
4007702
delete white space
jckkvs Feb 26, 2023
89c1df0
minor change
jckkvs Feb 27, 2023
82d8f82
change folder structure
jckkvs Mar 24, 2023
0c58d70
delete directory
jckkvs Mar 24, 2023
e9c1b5e
Add files via upload
jckkvs Sep 9, 2023
fdcd681
Add files via upload
jckkvs Sep 11, 2023
b5fbd3d
Delete zoofs/sklearn/dragon_fly.py
jckkvs Sep 11, 2023
c965e97
Update and rename dragonfly_.py to dragonfly.py
jckkvs Sep 11, 2023
70eb50f
Remove unused functions, add comments
jckkvs Sep 12, 2023
903cb7f
Update __init__.py
jckkvs Sep 12, 2023
58036dc
Add files via upload
jckkvs Sep 12, 2023
cc0d615
Delete zoofs/sklearn directory
jckkvs Sep 12, 2023
8676ea1
Delete zoofs/feature_seelction directory
jckkvs Sep 12, 2023
d316059
Add files via upload
jckkvs Sep 12, 2023
4ba74ac
Add "feature_selection"
jckkvs Sep 12, 2023
2d7ef49
Update dragonfly.py
jckkvs Sep 16, 2023
a31983a
Add files via upload
jckkvs Sep 16, 2023
5cab17b
Add files via upload
jckkvs Sep 16, 2023
a7f6a7d
Add print hof, hof_score
jckkvs Sep 19, 2023
2785a23
Delete trailing-whitespace
jckkvs Sep 19, 2023
f74f4dc
Delete whitespace
jckkvs Sep 19, 2023
a626971
Delete whitespace
jckkvs Sep 19, 2023
2a8bf08
Delete unused print
jckkvs Sep 19, 2023
04faa23
Delete whitespace
jckkvs Sep 19, 2023
606294b
Update dragonfly.py
jckkvs Sep 19, 2023
5975f09
Update dragonfly.py
jckkvs Sep 19, 2023
d31c168
Update dragonfly.py
jckkvs Sep 19, 2023
35db4ca
Update dragonfly.py
jckkvs Sep 19, 2023
7c95d43
Update dragonfly.py
jckkvs Sep 19, 2023
997c0dc
Update dragonfly.py
jckkvs Sep 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/requirement.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ mkdocs-meta-descriptions-plugin
jupyter_contrib_nbextensions
ipython>=7.16.3 # not directly required, pinned by Snyk to avoid a vulnerability
pygments>=2.7.4 # not directly required, pinned by Snyk to avoid a vulnerability
mistune>=2.0.1 # not directly required, pinned by Snyk to avoid a vulnerability
mistune>=2.0.1 # not directly required, pinned by Snyk to avoid a vulnerability
setuptools>=65.5.1 # not directly required, pinned by Snyk to avoid a vulnerability
wheel>=0.38.0 # not directly required, pinned by Snyk to avoid a vulnerability
105 changes: 105 additions & 0 deletions example/DragonFly.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# from dragon_fly import DragonFlyFeatureSelectionCV\n",
"from zoofs.feature_selection import DragonFlySelectionCV\n",
"from sklearn.linear_model import Lasso\n",
"from sklearn.datasets import load_diabetes, make_regression\n",
"from sklearn.pipeline import Pipeline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"selector = DragonFlySelectionCV(Lasso(),\n",
" n_jobs=-1,\n",
" min_features_to_select=10,\n",
" n_population=50,\n",
" n_iteration=100,\n",
" verbose=2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X,y = make_regression()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"selector.fit(X,y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X_transformed = selector.transform(X)\n",
"X_transformed.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pipe = Pipeline([(\"selector\", selector), (\"estimator\", Lasso())])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pipe.fit(X,y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "py310",
"language": "python",
"name": "py310"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
46 changes: 46 additions & 0 deletions tests/test_DragonFlySelectionCV.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Pytest code to test DragonFlySelectionCV class
# I will use English comments as requested.

import pytest
from sklearn.ensemble import RandomForestRegressor
from sklearn.cross_decomposition import PLSRegression
from sklearn.datasets import make_regression

from zoofs.feature_selection import DragonFlySelectionCV

# Sample dataset
X, y = make_regression(n_samples=50, n_features=20, noise=0.1, random_state=42)

# Test case parameters
params = [
(RandomForestRegressor(), False),
(PLSRegression(), True),
]

@pytest.mark.parametrize("estimator, auto_n_components", params)
def test_dragonfly_fit_transform(estimator, auto_n_components):
"""
Test if DragonFlySelectionCV can fit and transform without any errors.

Parameters
----------
estimator : object
The base estimator to use for feature selection.
auto_n_components : bool
Whether or not to automatically adjust the number of components for PLSRegression.
"""
# Initialize DragonFlySelectionCV with the given parameters
dragonfly = DragonFlySelectionCV(
estimator=estimator,
auto_n_components=auto_n_components,
n_population=15,
n_iteration=15,
n_jobs=-1
)

# Test if the 'fit' method works without any errors
dragonfly.fit(X, y)
# Test if the 'transform' method works without any errors
X_transformed = dragonfly.transform(X)
# Check if the transformed array has fewer or equal features than the original array
assert X_transformed.shape[1] <= X.shape[1]
2 changes: 2 additions & 0 deletions zoofs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
from zoofs.gravitationaloptimization import GravitationalOptimization
from zoofs.baseoptimizationalgorithm import BaseOptimizationAlgorithm
from zoofs.harrishawkoptimization import HarrisHawkOptimization

__all__ = ["feature_selection"]
6 changes: 6 additions & 0 deletions zoofs/baseoptimizationalgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ def fit(self):
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))

def _get_bestScore(self):
if self.minimize:
return self.best_score
else:
return -(self.best_score)

def _evaluate_fitness(
self, model, x_train, y_train, x_valid, y_valid, particle_swarm_flag=0, dragon_fly_flag=0
):
Expand Down
181 changes: 181 additions & 0 deletions zoofs/feature_selection/DragonFly.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# from dragon_fly import DragonFlyFeatureSelectionCV\n",
"from zoofs.feature_selection import DragonFlySelectionCV\n",
"from sklearn.linear_model import Lasso\n",
"from sklearn.datasets import load_diabetes"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"selector = DragonFlySelectionCV(Lasso())"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"X,y = load_diabetes(return_X_y=True)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"self.best_features_\n",
"[0 1 2 3 4 5 6 7 8 9]\n"
]
},
{
"data": {
"text/plain": [
"DragonFlyFeatureSelectionCV(estimator=Lasso())"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"selector.fit(X,y)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"bool_ = selector.transform(X)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.03807591, 0.05068012, 0.06169621, ..., -0.00259226,\n",
" 0.01990842, -0.01764613],\n",
" [-0.00188202, -0.04464164, -0.05147406, ..., -0.03949338,\n",
" -0.06832974, -0.09220405],\n",
" [ 0.08529891, 0.05068012, 0.04445121, ..., -0.00259226,\n",
" 0.00286377, -0.02593034],\n",
" ...,\n",
" [ 0.04170844, 0.05068012, -0.01590626, ..., -0.01107952,\n",
" -0.04687948, 0.01549073],\n",
" [-0.04547248, -0.04464164, 0.03906215, ..., 0.02655962,\n",
" 0.04452837, -0.02593034],\n",
" [-0.04547248, -0.04464164, -0.0730303 , ..., -0.03949338,\n",
" -0.00421986, 0.00306441]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool_"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(442, 10)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool_.shape"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.pipeline import Pipeline"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "Pipeline.__init__() takes 2 positional arguments but 3 were given",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[9], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m pipe \u001b[38;5;241m=\u001b[39m \u001b[43mPipeline\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mselector\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mselector\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mestimator\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mLasso\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[1;31mTypeError\u001b[0m: Pipeline.__init__() takes 2 positional arguments but 3 were given"
]
}
],
"source": [
"pipe = Pipeline([\"selector\", selector], [\"estimator\", Lasso()])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "py310",
"language": "python",
"name": "py310"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
5 changes: 5 additions & 0 deletions zoofs/feature_selection/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .dragonfly import DragonFlySelectionCV

__all__ = [
"DragonFlySelectionCV",
]
Loading