Giter VIP home page Giter VIP logo

featureselectionga's Introduction

FeatureSelectionGA

Feature Selection using Genetic Algorithm (DEAP Framework)

Data 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.

Installation:

$ pip install feature-selection-ga

Documentation:

https://featureselectionga.readthedocs.io/en/latest/

Usage:

from sklearn.datasets import make_classification
from sklearn import linear_model
from feature_selection_ga import FeatureSelectionGA, FitnessFunction

X, y = make_classification(n_samples=100, n_features=15, n_classes=3,
                           n_informative=4, n_redundant=1, n_repeated=2,
                           random_state=1)

model = linear_model.LogisticRegression(solver='lbfgs', multi_class='auto')
fsga = FeatureSelectionGA(model,X,y, ff_obj = FitnessFunction())
pop = fsga.generate(100)

#print(pop)

Usage (Advanced):

By default, the FeatureSelectionGA has its own fitness function class. We can also define our own FitnessFunction class.

class FitnessFunction:
    def __init__(self,n_splits = 5,*args,**kwargs):
        """
            Parameters
            -----------
            n_splits :int,
                Number of splits for cv

            verbose: 0 or 1
        """
        self.n_splits = n_splits

    def calculate_fitness(self,model,x,y):
        pass

With this, we can design our own fitness function by defining our calculate fitness! Consider the following example from Vieira, Mendoca, Sousa, et al. (2013) $f(X) = \alpha(1-P) + (1-\alpha) \left(1 - \dfrac{N_f}{N_t}\right)$

Define the constructor init with needed parameters: alpha and N_t.

class FitnessFunction:
    def __init__(self,n_total_features,n_splits = 5, alpha=0.01, *args,**kwargs):
        """
            Parameters
            -----------
            n_total_features :int
            	Total number of features N_t.
            n_splits :int, default = 5
                Number of splits for cv
            alpha :float, default = 0.01
                Tradeoff between the classifier performance P and size of
                feature subset N_f with respect to the total number of features
                N_t.

            verbose: 0 or 1
        """
        self.n_splits = n_splits
        self.alpha = alpha
        self.n_total_features = n_total_features

Next, we define the fitness function, the name has to be calculate_fitness:

    def calculate_fitness(self,model,x,y):
        alpha = self.alpha
        total_features = self.n_total_features

        cv_set = np.repeat(-1.,x.shape[0])
        skf = StratifiedKFold(n_splits = self.n_splits)
        for train_index,test_index in skf.split(x,y):
            x_train,x_test = x[train_index],x[test_index]
            y_train,y_test = y[train_index],y[test_index]
            if x_train.shape[0] != y_train.shape[0]:
                raise Exception()
            model.fit(x_train,y_train)
            predicted_y = model.predict(x_test)
            cv_set[test_index] = predicted_y

        P = accuracy_score(y, cv_set)
        fitness = (alpha*(1.0 - P) + (1.0 - alpha)*(1.0 - (x.shape[1])/total_features))
        return fitness

Example: You may also see example2.py

X, y = make_classification(n_samples=100, n_features=15, n_classes=3,
n_informative=4, n_redundant=1, n_repeated=2,
random_state=1)

# Define the model

model = linear_model.LogisticRegression(solver='lbfgs', multi_class='auto')

# Define the fitness function object

ff = FitnessFunction(n_total_features= X.shape[1], n_splits=3, alpha=0.05)
fsga = FeatureSelectionGA(model,X,y, ff_obj = ff)
pop = fsga.generate(100)

Example adopted from pyswarms

featureselectionga's People

Contributors

a7medabdeldaim avatar actions-user avatar dependabot[bot] avatar kaushalshetty avatar marcjermaine-pontiveros avatar puneethapai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

featureselectionga's Issues

TypeError: unhashable type: 'slice'

Hello, when I ran your code got "TypeError: unhashable type: 'slice' ".Can you help me analyze the problem?thanks

import pandas as pd from sklearn.linear_model import LogisticRegression from feature_selection_ga import FeatureSelectionGA data = pd.read_excel("D:\\Project_CAD\\ๅฎž้ชŒ6\\data\\train_data_1\\train_1.xlsx") x, y = data.iloc[:, :53], data.iloc[:, 56] model = LogisticRegression() fsga = FeatureSelectionGA(model, x, y) pop = fsga.generate(100)

C:\Python27\lib\pkgutil.py:183: ImportWarning: Not importing directory

plz help to time run
error
C:\Python27\lib\site-packages\deap\tools_hypervolume\pyhv.py:33: ImportWarning: Falling back to the python version of hypervolume module. Expect this to be very slow.
"module. Expect this to be very slow.", ImportWarning)
C:\Python27\lib\pkgutil.py:183: ImportWarning: Not importing directory 'C:\Users\taha\AppData\Roaming\Python\Python27\site-packages\mpl_toolkits': missing init.py
file, filename, etc = imp.find_module(subname, path)
C:\Python27\lib\pkgutil.py:183: ImportWarning: Not importing directory 'c:\users\taha\appdata\roaming\python\python27\site-packages\mpl_toolkits': missing init.py
file, filename, etc = imp.find_module(subname, path)
C:\Python27\lib\pkgutil.py:183: ImportWarning: Not importing directory 'C:\Python27\lib\site-packages\google': missing init.py
file, filename, etc = imp.find_module(subname, path)
C:\Python27\lib\pkgutil.py:183: ImportWarning: Not importing directory 'c:\python27\lib\site-packages\google': missing init.py
file, filename, etc = imp.find_module(subname, path)
C:\Python27\lib\pkgutil.py:183: ImportWarning: Not importing directory 'C:\Python27\lib\site-packages\google\cloud': missing init.py
file, filename, etc = imp.find_module(subname, path)
C:\Python27\lib\pkgutil.py:183: ImportWarning: Not importing directory 'C:\Python27\lib\site-packages\google\cloud\proto': missing init.py
file, filename, etc = imp.find_module(subname, path)
C:\Python27\lib\pkgutil.py:183: ImportWarning: Not importing directory 'C:\Python27\lib\site-packages\google\cloud\proto\pubsub': missing init.py
file, filename, etc = imp.find_module(subname, path)
C:\Python27\lib\pkgutil.py:183: ImportWarning: Not importing directory 'C:\Python27\lib\site-packages\google\cloud\proto\datastore': missing init.py
file, filename, etc = imp.find_module(subname, path)
C:\Python27\lib\pkgutil.py:183: ImportWarning: Not importing directory 'C:\Python27\lib\site-packages\google\pubsub': missing init.py
file, filename, etc = imp.find_module(subname, path)
C:\Python27\lib\pkgutil.py:183: ImportWarning: Not importing directory 'C:\Python27\lib\site-packages\google\logging': missing init.py
file, filename, etc = imp.find_module(subname, path)
C:\Python27\lib\pkgutil.py:183: ImportWarning: Not importing directory 'C:\Python27\lib\site-packages\google\iam': missing init.py
file, filename, etc = imp.find_module(subname, path)
C:\Python27\lib\pkgutil.py:183: ImportWarning: Not importing directory 'C:\Python27\lib\site-packages\google\cloud\logging': missing init.py
file, filename, etc = imp.find_module(subname, path)
C:\Python27\lib\pkgutil.py:183: ImportWarning: Not importing directory 'C:\Python27\lib\site-packages\google\cloud\gapic': missing init.py
file, filename, etc = imp.find_module(subname, path)
C:\Python27\lib\pkgutil.py:183: ImportWarning: Not importing directory 'C:\Python27\lib\site-packages\google\cloud\gapic\pubsub': missing init.py
file, filename, etc = imp.find_module(subname, path)

plz help tanks
plz exampl for run program send for me . tanks

example

hi,
can you provide us with an example of using the featureselectionGA

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.