Features Selection by using Voting Approach

davisy

Davis David

Posted on March 30, 2020

Features Selection by using Voting Approach

Feature Selection is the process where you automatically or manually select those features which contribute most to your prediction variable or output in which you are interested.

One major reason is that machine learning follows the rule of “garbage in garbage out” and that is why you need to be very concerned about the features that are being fed to the model.

Having irrelevant features in your data can both increase the computational cost of modeling and decrease the accuracy of the models and make your model learn based on irrelevant features. This means you need to select only important features to be presented during model training.

Top reasons to apply feature selection are:

  • It enables the machine learning algorithm to train faster.
  • It reduces the complexity of a model and makes it easier to interpret.
  • It improves the accuracy of a model if the right subset is chosen.
  • It reduces overfitting.

“I prepared a model by selecting all the features and I got an accuracy of around 65% which is not pretty good for a predictive model and after doing some feature selection and feature engineering without doing any logical changes in my model code my accuracy jumped to 81% which is quite impressive”- By Raheel Shaikh

Feature selection methods are intended to reduce the number of features to those that are believed to be most useful/important to a model in order to predict the target feature. There are different feature selection methods available that can help you to select important features. The most popular methods are.

  • Pearson’s correlation coefficient.
  • Spearman’s rank coefficient.
  • ANOVA correlation coefficient.
  • Chi-Squared test.
  • Machine Learning Algorithms (Random Forest and Extra Tree).
  • Mutual Information.
  • Correlation Matrix with Heatmap.

You can read more about feature selection methods from Scikit learn library

One of the challenges of these methods is to identify which method(s) should you apply in your dataset to select important features. Each method has its own way to identify the important features. For example, a certain feature can be selected as an important feature in one method and not selected as an important feature in another method.

Xverse package can help you to solve this problem.

“At the end of the day, some machine learning projects succeed and some fail.What makes the difference? Easily the most important factor is the features used.” — Pedro Domingos

What is Xverse?

Xverse stands for X Universe which is the python package for machine learning to assist Data Scientists with feature transformation and feature selection. Xverse is created by Sundar Krishnan.

How does it work?

Xverse applies a variety of techniques to select features. When an algorithm picks a feature, it gives a vote for that feature. In the end, Xverse calculates the total votes for each feature and then pick the best ones based on votes. This way, we end up picking the best variables with minimum effort in the features selection process.

Xverse uses the following methods to select important features.

  • Information Value using Weight of evidence.
  • Variable Importance using Random Forest.
  • Recursive Feature Elimination.
  • Variable Importance using Extra trees classifier.
  • Chi-Square best variables.
  • L1 based feature selection.

Installation

The package requires numpy, pandas, scikit-learn, scipy and statsmodels. In addition, the package is tested on Python version 3.5 and above.
Run the following command to install Xverse.

pip install xverse

I will use the Loan dataset to find the best features that can help to get good accuracy when predicting if a customer deserves to get a loan or not.You can download the dataset here.

Import important packages for this problem.

import pandas as pd
import numpy as np                     
from xverse.ensemble import VotingSelector
from sklearn.preprocessing import StandardScaler, MinMaxScaler  
import warnings                        # To ignore any warnings
warnings.filterwarnings("ignore")
Enter fullscreen mode Exit fullscreen mode

Load the Loan dataset.

data = pd.read_csv("data/loans_data.csv")
data.columns
Enter fullscreen mode Exit fullscreen mode

Loan_ID
Gender
Married
Dependents
Education
Self_Employed
ApplicantIncome
CoapplicantIncome
LoanAmount
Loan_Amount_Term
Credit_History
Property_Area
Loan_Status

We have 12 independent features and a target (Loan_Status). You can read the description of each feature here.

I have created a simple python function to handle missing data and feature engineering.

def preprocessing(data):

    # replace with numerical values
    data['Dependents'].replace('3+', 3,inplace=True)
    data['Loan_Status'].replace('N', 0,inplace=True)
    data['Loan_Status'].replace('Y', 1,inplace=True)

    # handle missing data 
    data['Gender'].fillna(data['Gender'].mode()[0], inplace=True)
    data['Married'].fillna(data['Married'].mode()[0], inplace=True)
    data['Dependents'].fillna(data['Dependents'].mode()[0], inplace=True)
    data['Self_Employed'].fillna(data['Self_Employed'].mode()[0], inplace=True)
    data['Credit_History'].fillna(data['Credit_History'].mode()[0], inplace=True)
    data['Loan_Amount_Term'].fillna(data['Loan_Amount_Term'].mode()[0], inplace=True)
    data['LoanAmount'].fillna(data['LoanAmount'].median(), inplace=True)

    # drop ID column
    data = data.drop('Loan_ID',axis=1)

    #scale the data
    data["ApplicantIncome"] = MinMaxScaler().fit_transform(data["ApplicantIncome"].values.reshape(-1,1))
    data["LoanAmount"] = MinMaxScaler().fit_transform(data["LoanAmount"].values.reshape(-1,1))
    data["CoapplicantIncome"] = MinMaxScaler().fit_transform(data["CoapplicantIncome"].values.reshape(-1,1))
    data["Loan_Amount_Term"] = MinMaxScaler().fit_transform(data["Loan_Amount_Term"].values.reshape(-1,1))


    return data 
Enter fullscreen mode Exit fullscreen mode

Let’s preprocess the loan dataset.

data = prepocessing(data)
Enter fullscreen mode Exit fullscreen mode

Split into independent features and target.

X = data.drop('Loan_Status',axis = 1)
y = data.Loan_Status
Enter fullscreen mode Exit fullscreen mode

Now is time to call VotingSelector from Xverse and train it to find the best features by using the voting approach.

#train to find the best features

clf = VotingSelector(minimum_votes=2)
clf.fit(X, y)
Enter fullscreen mode Exit fullscreen mode

I have set minimum_votes= 2, this means feature to be selected must have at least a total of 2 votes from the six feature selection methods presented in Xverse. Features below a total of 2 votes will be neglected.
After training, we can see the feature importance in each feature selection method used during the training.

#show important features 
clf.feature_importances_
Enter fullscreen mode Exit fullscreen mode

The output shows all the features and their values of importance in each method.

Now let’s observe the votes from these feature selection methods.

# votes 
clf.feature_votes_
Enter fullscreen mode Exit fullscreen mode

The output shows the variable name, list of feature selection methods and their votes and at last, it shows total votes for each feature.
It starts by show features with many votes to the features with low or zero votes.

You can see that Credit_History has a total of 6 votes, which means credit_History is a very important feature for this Loan problem. But both Gender and Self_employed features have 0 votes, which means we can neglect these two features because of the very low contribution to the prediction if a customer deserves to get a loan or not.

Now we can transform our data to remain with only important selected features.

# transform your data into important features 

X = clf.transform(X)
Enter fullscreen mode Exit fullscreen mode

Conclusion

Xverse is under active development. Currently, xverse package handles only binary target.

The code for this post is available on Github.

If you learned something new or enjoyed reading this article, please share it so that others can see it. Feel free to leave a comment too. Till then, see you in the next post! I can also be reached on Twitter @Davis_McDavid

💖 💪 🙅 🚩
davisy
Davis David

Posted on March 30, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related