GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing thepythoncode.com — Feature Selection using Scikit-Learn in Python - The Python Code Get a head start on your coding projects with our Python Code Generator. Perfect for those times when you need a quick solution. Don't wait, try it today! Many steps are involved in the data science pipeline, going from raw data to building an optimized machine learning model for the given task. However, data processing is the step that requires the most effort and time, and which has a direct influence and impact on the performance of the models later on. In this article, we will focus on how to apply some feature selection to our dataset, which represents a core aspect of the data preprocessing phase. But before diving into coding and implementing the different techniques used for these tasks, let us first define what we mean by feature selection. Feature selection is the process of choosing a subset of features from the dataset that contributes the most to the performance of the model, and this without applying any type of transformation on it. The dataset we will use is the Heart Disease Prediction dataset from Kaggle and you can directly work on that using the Kaggle Kernel VM, or you can download it to your local machine. The following is the command for installing the required libraries for this tutorial:
Preprocessing We will first load our dataset into a dataframe format using pandas. It is composed of 13 features plus the label and there are 270 rows.
The above code imports the necessary libraries and reads the dataset CSV file from Below is the output:
From here, we can clearly observe that there are no null values, so we can directly start working on our data frame without performing some null values cleaning.
We will store the label column into a separate variable and drop it entirely (hence, the use of
It is important to always check how imbalanced our dataset might be since a big imbalance ratio between the minority and majority classes will negatively affect the model in a sense that it will predict only the majority class naively. For our case, however, the imbalance ratio is only
Our dataset has features that follow a categorical nature. However, while printing the data type of those columns, we observe that they are considered as integers and this might make our model to treat them as continuous values despite being discrete by nature. For this reason we will change explicitly their data type to categorical using
We will now scale our continuous features using
Feature Selection Using Chi-Square A chi-square test is used in statistics to test the independence of two events. Given the data of two variables, we can get observed count O and expected count E. Chi-Square measures how expected count E and observed count O deviates each other. In feature selection, and since chi2 tests the degree of independence between two variables, we will use it between every feature and the label and we will keep only the
Feature Selection Using Recursive Feature Elimination (RFE) From sklearn Documentation: The goal of recursive feature elimination (RFE) is to select features by recursively considering smaller and smaller sets of features. We will be using
Feature Selection Using Random Forest Tree-based machine learning algorithms like
The above histogram shows the importance of each feature. In our case, Thallium and the number of vessels fluro are the most important features, but most of them have importance, and since that's the case, it's pretty much worth feeding these features to our machine learning model. Now that you have selected the best features, you can easily use any sklearn classifier model and feed Conclusion Using such techniques for feature selection varies from one problem to another and also from a feature to another depending on their type being categorical or continuous. In addition, the number of features to select can be answered by following an iterative approach until the In this article, we have learned how to:
Related: How to Apply HOG Feature Extraction in Python. Happy Learning ♥ Finished reading? Keep the learning going with our AI-powered Code Explainer. Try it now!
Sharing is caring!
Read also
How to Make a Speech Emotion Recognizer Using Python And Scikit-learnBuilding a Speech Emotion Recognition system that detects emotion from human speech tone using Scikit-learn library in Python
How to Create Plots with Plotly In PythonLearn how to use the visualization tool Plotly to implement and create dynamic plots and figures (such as scatters, histograms, and candlesticks) in Python.
How to Use K-Means Clustering for Image Segmentation using OpenCV in PythonUsing K-Means Clustering unsupervised machine learning algorithm to segment different parts of an image using OpenCV in Python. CommentsGot a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips! |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |