{"id":18963736,"url":"https://github.com/kaushalshetty/FeatureSelectionGA","last_synced_at":"2025-04-16T05:31:24.265Z","repository":{"id":29038002,"uuid":"110458125","full_name":"kaushalshetty/FeatureSelectionGA","owner":"kaushalshetty","description":"Feature Selection using Genetic Algorithm (DEAP Framework)","archived":false,"fork":false,"pushed_at":"2023-02-21T22:56:56.000Z","size":56,"stargazers_count":367,"open_issues_count":8,"forks_count":95,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-10T02:55:24.374Z","etag":null,"topics":["deap","feature-selection","genetic-algorithm","machine-learning","python"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kaushalshetty.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2017-11-12T18:32:44.000Z","updated_at":"2025-03-06T21:20:30.000Z","dependencies_parsed_at":"2023-01-14T14:00:45.460Z","dependency_job_id":"7ae1c4cb-0279-41c6-8e50-fa0c39d7e4c9","html_url":"https://github.com/kaushalshetty/FeatureSelectionGA","commit_stats":{"total_commits":32,"total_committers":8,"mean_commits":4.0,"dds":0.6875,"last_synced_commit":"5aa3db531d304c4262dcce16a392d60017a71658"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaushalshetty%2FFeatureSelectionGA","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaushalshetty%2FFeatureSelectionGA/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaushalshetty%2FFeatureSelectionGA/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaushalshetty%2FFeatureSelectionGA/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kaushalshetty","download_url":"https://codeload.github.com/kaushalshetty/FeatureSelectionGA/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249201117,"owners_count":21229004,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deap","feature-selection","genetic-algorithm","machine-learning","python"],"created_at":"2024-11-08T14:21:26.901Z","updated_at":"2025-04-16T05:31:24.004Z","avatar_url":"https://github.com/kaushalshetty.png","language":"Python","funding_links":[],"categories":["Uncategorized"],"sub_categories":["Uncategorized"],"readme":"# FeatureSelectionGA\n\n[![](https://img.shields.io/github/workflow/status/kaushalshetty/featureselectionga/Test.svg)](https://github.com/kaushalshetty/FeatureSelectionGA/actions)\n[![](https://img.shields.io/pypi/v/feature-selection-ga.svg)](https://pypi.python.org/pypi/feature-selection-ga/)\n[![](https://readthedocs.org/projects/featureselectionga/badge/?version=latest)](https://featureselectionga.readthedocs.io/en/latest/?badge=latest)\n[![](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\n### Feature Selection using Genetic Algorithm (DEAP Framework)\n\nData scientists find it really difficult to choose the right features to get maximum accuracy especially if you are dealing with a lot of features. There are currenlty lots of ways to select the right features. But we will have to struggle if the feature space is really big. Genetic algorithm is one solution which searches for one of the best feature set from other features in order to attain a high accuracy.\n\n#### Installation:\n\n```bash\n$ pip install feature-selection-ga\n```\n\n#### Documentation: \nhttps://featureselectionga.readthedocs.io/en/latest/\n\n#### Usage:\n\n```python\nfrom sklearn.datasets import make_classification\nfrom sklearn import linear_model\nfrom feature_selection_ga import FeatureSelectionGA, FitnessFunction\n\nX, y = make_classification(n_samples=100, n_features=15, n_classes=3,\n                           n_informative=4, n_redundant=1, n_repeated=2,\n                           random_state=1)\n\nmodel = linear_model.LogisticRegression(solver='lbfgs', multi_class='auto')\nfsga = FeatureSelectionGA(model,X,y, ff_obj = FitnessFunction())\npop = fsga.generate(100)\n\n#print(pop)\n```\n\n#### Usage (Advanced):\n\nBy default, the FeatureSelectionGA has its own fitness function class. We can also define our own\nFitnessFunction class.\n\n```python\nclass FitnessFunction:\n    def __init__(self,n_splits = 5,*args,**kwargs):\n        \"\"\"\n            Parameters\n            -----------\n            n_splits :int,\n                Number of splits for cv\n\n            verbose: 0 or 1\n        \"\"\"\n        self.n_splits = n_splits\n\n    def calculate_fitness(self,model,x,y):\n        pass\n```\n\nWith this, we can design our own fitness function by defining our calculate fitness!\nConsider the following example from [Vieira, Mendoca, Sousa, et al. (2013)](http://www.sciencedirect.com/science/article/pii/S1568494613001361)\n`$f(X) = \\alpha(1-P) + (1-\\alpha) \\left(1 - \\dfrac{N_f}{N_t}\\right)$`\n\nDefine the constructor **init** with needed parameters: alpha and N_t.\n\n```python\nclass FitnessFunction:\n    def __init__(self,n_total_features,n_splits = 5, alpha=0.01, *args,**kwargs):\n        \"\"\"\n            Parameters\n            -----------\n            n_total_features :int\n            \tTotal number of features N_t.\n            n_splits :int, default = 5\n                Number of splits for cv\n            alpha :float, default = 0.01\n                Tradeoff between the classifier performance P and size of\n                feature subset N_f with respect to the total number of features\n                N_t.\n\n            verbose: 0 or 1\n        \"\"\"\n        self.n_splits = n_splits\n        self.alpha = alpha\n        self.n_total_features = n_total_features\n\n```\n\nNext, we define the fitness function, the name has to be\ncalculate_fitness:\n\n```python\n    def calculate_fitness(self,model,x,y):\n        alpha = self.alpha\n        total_features = self.n_total_features\n\n        cv_set = np.repeat(-1.,x.shape[0])\n        skf = StratifiedKFold(n_splits = self.n_splits)\n        for train_index,test_index in skf.split(x,y):\n            x_train,x_test = x[train_index],x[test_index]\n            y_train,y_test = y[train_index],y[test_index]\n            if x_train.shape[0] != y_train.shape[0]:\n                raise Exception()\n            model.fit(x_train,y_train)\n            predicted_y = model.predict(x_test)\n            cv_set[test_index] = predicted_y\n\n        P = accuracy_score(y, cv_set)\n        fitness = (alpha*(1.0 - P) + (1.0 - alpha)*(1.0 - (x.shape[1])/total_features))\n        return fitness\n\n```\n\nExample:\nYou may also see `example2.py`\n\n```python\nX, y = make_classification(n_samples=100, n_features=15, n_classes=3,\nn_informative=4, n_redundant=1, n_repeated=2,\nrandom_state=1)\n\n# Define the model\n\nmodel = linear_model.LogisticRegression(solver='lbfgs', multi_class='auto')\n\n# Define the fitness function object\n\nff = FitnessFunction(n_total_features= X.shape[1], n_splits=3, alpha=0.05)\nfsga = FeatureSelectionGA(model,X,y, ff_obj = ff)\npop = fsga.generate(100)\n\n```\n\nExample adopted from [pyswarms](https://pyswarms.readthedocs.io/en/latest/examples/usecases/feature_subset_selection.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaushalshetty%2FFeatureSelectionGA","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaushalshetty%2FFeatureSelectionGA","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaushalshetty%2FFeatureSelectionGA/lists"}