DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Machine Learning: Getting Data Into the Right Shape

Machine Learning: Getting Data Into the Right Shape

Getting your data into the right shape is important in ensuring accurate results when using Machine Learning. Let's investigate how to help make sure you have that shape.

Andrejus Baranovskis user avatar by
Andrejus Baranovskis
·
Nov. 09, 18 · Tutorial
Like (1)
Save
Tweet
Share
7.03K Views

Join the DZone community and get the full member experience.

Join For Free

When you build a Machine Learning model, first start with the data — make sure input data is prepared well and it represents the true state of what you want the Machine Learning model to learn. Data preparation task takes time, but don't hurry — quality data is key for Machine Learning success. In this post, I will go through the essential steps required to bring data into the right shape to feed it into a Machine Learning algorithm.

Sample dataset and Python notebook for this post can be downloaded from my GitHub repo.

Each row from the dataset represents invoice, which was sent to the customer. The original dataset extracted from the ERP system comes with five columns:

customer — customer ID
invoice_date — date when invoice was created
payment_due_date — expected invoice payment date
payment_date — actual invoice payment date
grand_total — invoice total

invoice_risk_decision — 0/1 value column that describes the current invoice risk. The goal of the Machine Learning module will be to identify the risks for future invoices based on risks estimated for historical invoice data.

There are two types of features — categorical and continuous:

categorical — often text than number, something that represents distinct groups/types
continuous — numbers

Machine Learning typically works with numbers. This means we need to transform all categorical features into continuous. For example, grand_total is a continuous feature, but dates and customer ID are not.

The date can be converted to continuous feature by breaking it into multiple columns. Here is an example of breaking invoice_date into multiple continuous features (year, quarter, month, week, day of year, day of month, day of week):

Using this approach, all date columns can be transformed into continuous features. Customer ID column can be converted into matrix of 0/1. Each unique text value is moved into a separate column and assigned with 1, all other columns in that row are assigned with 0. This transformation can be done with a Python library called Pandas, we will see it later.

You may or may not have decision values for your data, this depends on how data was collected and what process was implemented in ERP app to collect this data. Decision column (invoice_risk_decision) value represents business rule we want to calculate with machine learning. See 0/1 assigned to this column:

Rule description:

0 — invoice was payed on time, payment_date less or equal payment_due_date
0 — invoice wasn't payed on time, but total is less than all invoices total average and payment delay is less or equal 10% for current customer average
1 — all other cases, indicates high invoice payment risk

I would recommend to save data in CSV format. Once data is prepared, we can load it in Python notebook:

I'm using Pandas library (imported through pd variable) to load data from file into data frame. Function head() prints first five rows from data frame (dataset size 5x24):

We can show the number of rows with 0/1, this helps to understand how the data set is constructed — we see that more than half rows represent invoices without payment risk:

Customer ID column is not a number, we need to convert it. Will be using Pandas get_dummies function for this task. It will turn every unique value into a column and place 0 or 1 depending on whether the row contains the value or not (this will increase dataset width):

Original customer column is gone, now we have multiple columns for each customer. If a customer with ID = 4 is located it given row, 1 is set:

Finally, we can check the correlation between decision column — invoice_risk_decision and other columns from the dataset. Correlation shows which columns will be used by a Machine Learning algorithm to predict a value based on the values in other columns in the dataset. Here is the correlation for our dataset (all columns with more than 10% correlation):

As you can see, all date columns have a high correlation as well as grand_total. Our rule tells that invoice payment risk is low if invoice amount is less than all total average — that's why correlation on grand_total value exist.

The customer with ID = 11 is the one with the largest number of invoices, and the correlation for this customer is higher than for others, as expected.

Machine learning Data (computing) Pandas

Published at DZone with permission of Andrejus Baranovskis, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Understanding gRPC Concepts, Use Cases, and Best Practices
  • What Should You Know About Graph Database’s Scalability?
  • Kotlin Is More Fun Than Java And This Is a Big Deal
  • Iptables Basic Commands for Novice

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: