Thursday, 16 May 2024

How to Transform Your Machine Learning Data in Weka

 Often your raw data for machine learning is not in an ideal form for modeling.

You need to prepare or reshape it to meet the expectations of different machine learning algorithms.

In this post you will discover two techniques that you can use to transform your machine learning data ready for modeling.

After reading this post you will know:

  • How to convert a real valued attribute into a discrete distribution called discretization.
  • How to convert a discrete attribute into multiple real values called dummy variables.
  • When to discretize or create dummy variables from your data.'

Let’s get started.

Discretize Numerical Attributes

Some machine learning algorithms prefer or find it easier to work with discrete attributes.

For example, decision tree algorithms can choose split points in real valued attributes, but are much cleaner when split points are chosen between bins or predefined groups in the real-valued attributes.

Discrete attributes are those that describe a category, called nominal attributes. Those attributes that describe a category that where there is a meaning in the order for the categories are called ordinal attributes. The process of converting a real-valued attribute into an ordinal attribute or bins is called discretization.

You can discretize your real valued attributes in Weka using the Discretize filter.

The tutorial below demonstrates how to use the Discretize filter. The Pima Indians onset of diabetes dataset is used to demonstrate this filter because of the input values are real-valued and grouping them into bins may make sense.

You can learn more about the dataset here:

You can also access the dataset directory in your installation of Weka under the data/ directory by loading the file diabetes.arff.

1. Open the Weka Explorer.

2. Load the Pima Indians onset of diabetes dataset.

Weka Explorer Loaded Diabetes Dataset

Weka Explorer Loaded Diabetes Dataset

3. Click the “Choose” button for the Filter and select Discretize, it is under unsupervised.attribute.Discretize.

Weka Select Discretize Data Filter

Weka Select Discretize Data Filter

4. Click on the filter to configure it. You can select the indices of the attributes to discretize, the default is to discretize all attributes, which is what we will do in this case. Click the “OK” button.

5. Click the “Apply” button to apply the filter.

You can click on each attribute and review the details in the “Selected attribute” window to confirm that the filter was applied successfully.

Weka Discretized Attribute

Weka Discretized Attribute

Discretizing your real valued attributes is most useful when working with decision tree type algorithms. It is perhaps more useful when you believe that there are natural groupings within the values of given attributes.

Need more help with Weka for Machine Learning?

Take my free 14-day email course and discover how to use the platform step-by-step.

Click to sign-up and also get a free PDF Ebook version of the course.

Convert Nominal Attributes to Dummy Variables

Some machine learning algorithms prefer to use real valued inputs and do not support nominal or ordinal attributes.

Nominal attributes can be converted to real values. This is done by creating one new binary attribute for each category. For a given instance that has a category for that value, the binary attribute is set to 1 and the binary attributes for the other categories is set to 0. This process is called creating dummy variables.

You can create dummy binary variables from nominal attributes in Weka using the NominalToBinary filter.

The recipe below demonstrates how to use the NominalToBinary filter. The Contact Lenses dataset is used to demonstrate this filter because the attributes are all nominal and provide plenty of opportunity for creating dummy variables.

You can download the Contact Lenses dataset from the UCI Machine learning repository. You can also access the dataset directory in your installation of Weka under the data/ directory by loading the file contact-lenses.arff.

1. Open the Weka Explorer.

2. Load the Contact Lenses dataset.

Weka Explorer Loaded Contact Lenses Dataset

Weka Explorer Loaded Contact Lenses Dataset

3. Click the “Choose” button for the Filter and select NominalToBinary, it is under unsupervised.attribute.NominalToBinary.

Weka Select NominalToBinary Data Filter

Weka Select NominalToBinary Data Filter

4. Click on the filter to configure it. You can select the indices of the attributes to convert to binary values, the default is to convert all attributes. Change it to only the first attribute. Click the “OK” button.

Weka NominalToBinary Data Filter Configuration

Weka NominalToBinary Data Filter Configuration

5. Click the “Apply” button to apply the filter.

Reviewing the list of attributes will show that the age attribute has been removed and replaced with three new binary attributes: age=young, age=pre-presbyopic and age=presbyopic.

Weka Nominal Attribute Converted to Dummary Variables

Weka Nominal Attribute Converted to Dummary Variables

Creating dummy variables is useful for techniques that do not support nominal input variables like linear regression and logistic regression. It can also prove useful in techniques like k-nearest neighbors and artificial neural networks.

Summary

In this post you discovered how to transform your machine learning data to meet the expectations of different machine learning algorithms.

Specifically, you learned:

  • How to convert real valued input attributes to nominal attributes called discretization.
  • How to convert a categorical input variable to multiple binary input attributes called dummy variables.
  • When to use discretization and dummy variables when modeling data.

Do you have any questions about data transforms or about this post? Ask your questions in the comments and I will do my best to answer them.

No comments:

Post a Comment

Connect broadband