Turbocharge Your Machine Learning Workflows by Ditching CSV for Feather
As data scientists and machine learning engineers, we‘re always looking for ways to optimize our workflows and tame ever-increasing data sizes. One of the biggest bottlenecks is often data loading – when you‘re dealing with gigabytes or terabytes of training data, slow I/O can grind your productivity to a halt.
Luckily, there‘s a powerful tool that can speed up your machine learning workflows by an order of magnitude: the Feather file format. Developed by Wes McKinney, the creator of pandas, Feather is a language-agnostic binary format designed for lightning-fast read and write performance on data frames.
In this post, we‘ll dive into the problems with the ubiquitous CSV format, explore how Feather solves them, and see how you can easily adopt Feather in your Python machine learning projects. After reading, you‘ll be equipped to leverage Feather for maximum efficiency in your development cycle. Let‘s get started!
The Trouble with CSV
On the surface, CSV seems like a great format. It‘s simple, human-readable, and universally supported. However, CSV starts to break down once you‘re working with real-world datasets that can easily span millions of rows across hundreds of columns.
The main issues with CSV are:
-
Slow read/write performance – CSV parsing is expensive, especially when dealing with millions of rows. Every time you load a CSV, the parser needs to convert each value from raw text to the appropriate data type in memory. This overhead adds up to significant costs on large files.
-
Poor scalability – CSV read times scale linearly (or worse) with data size. Parsing a 2GB CSV takes at least twice as long as a 1GB file. When you‘re constantly iterating on a dataset, this becomes a major productivity sink.
-
Error-prone type inference – CSV doesn‘t store any type metadata, so the parser has to infer the type of each value. This is slow and can silently produce incorrect results when a column contains mixed types.
-
Lack of cross-language support – Although CSV is widely supported, the lack of a single standard means that different languages handle edge cases like quoting and escape characters differently. This introduces friction when sharing data between tools.
To highlight CSV‘s performance problems, let‘s generate a large DataFrame with 10 million rows and 10 columns and measure the read times:
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randn(10**7, 10))
%time df.to_csv(‘data.csv‘, index=False)
# Wall time: 30.5 s
%time pd.read_csv(‘data.csv‘)
# Wall time: 23.2 s
As you can see, just loading this relatively modestly-sized 1.5 GB CSV takes over 20 seconds! When you‘re iterating on a machine learning model, this quickly becomes unbearable.
Enter Feather
Feather is a binary file format that solves all of CSV‘s pain points while still being easy to use. Let‘s break down the key benefits:
- Blazing-fast I/O – Feather consistently achieves an order-of-magnitude speedup over CSV on both reads and writes. As an example, let‘s compare the same 10M row DataFrame from before in Feather format:
df.to_feather(‘data.feather‘)
# Wall time: 1.4 s
pd.read_feather(‘data.feather‘)
# Wall time: 0.9 s
Feather loads the data in less than a second, a 25x speedup over CSV! For even larger datasets, the gains are even more dramatic.
- Optimized storage – Under the hood, Feather uses the Apache Arrow columnar memory format to efficiently pack data into a binary representation. A quick comparison of the file sizes reveals massive space savings:
!ls -lh *.csv
# -rw-r--r-- 1 user wheel 1.5G data.csv
!ls -lh *.feather
# -rw-r--r-- 1 user wheel 626M data.feather
Feather reduces the file size by nearly 60% without sacrificing any data fidelity. For machine learning workloads that depend on huge datasets, this translates into big cost savings on storage.
-
Automatic type handling – Since Feather stores data in a structured binary format, it can automatically serialize and deserialize columns to the appropriate data type without any manual parsing. This eliminates a major source of bugs and slowdowns in CSV workflows.
-
Cross-language support – Feather is designed as a language-agnostic format with first-class support in Python, R, and Julia and growing support in other ecosystems. Saving data frames in Feather makes it effortless to share data between languages and opens up opportunities for polyglot machine learning workflows.
Adopting Feather in Python
Now that you know why Feather is so powerful, let‘s walk through how to easily adopt it in your Python machine learning projects. Feather has deep integration with the PyArrow library, which provides a fast implementation of the Apache Arrow standard in Python.
To get started with Feather, first install PyArrow using conda or pip:
conda install -c conda-forge pyarrow
# or
pip install pyarrow
Now you‘re ready to start using Feather! The pandas API provides a familiar interface for reading and writing Feather files using the to_feather() and read_feather() methods:
import pandas as pd
# Write DataFrame df to Feather file
df.to_feather(‘data.feather‘)
# Read a Feather file into a DataFrame
df = pd.read_feather(‘data.feather‘)
You can also take advantage of Feather in machine learning specific libraries like scikit-learn:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# Load the iris dataset and convert to DataFrame
iris = load_iris()
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df[‘target‘] = iris.target
# Write full dataset to Feather
df.to_feather(‘iris.feather‘)
# Load train and test sets from Feather
train_df = pd.read_feather(‘iris_train.feather‘)
test_df = pd.read_feather(‘iris_test.feather‘)
With just a few lines of code, we‘re able to save our training data in an optimized format that will significantly speed up our modeling iterations. The space savings also add up when you‘re dealing with larger datasets.
The Power of Apache Arrow
To fully understand the magic behind Feather‘s performance, it helps to take a closer look at the Arrow memory format that powers it. Arrow is a cross-language development platform for in-memory analytics designed to improve the performance of big data processing.
The key innovation of Arrow is its columnar memory format. By organizing data in a column-oriented layout, Arrow enables significant speedups on typical analytics and machine learning workloads. Columnar storage has a few key advantages:
-
Improved memory usage – Storing data in columns allows for more efficient compression and encoding schemes. Arrow uses techniques like bit-packing and dictionary encoding to minimize the memory footprint.
-
Vectorized processing – Modern CPUs are designed to operate efficiently on batches of data. Column-oriented storage maximizes CPU throughput by allowing the processor to access and operate on contiguous memory regions.
-
Reduced IO bottlenecks – With columnar storage, queries can access only the subset of columns they need, minimizing the amount of data that needs to be read from disk.
On top of the columnar format, Arrow also provides a standardized in-memory representation for data types that can be shared across languages. This eliminates the need for costly serialization and deserialization when moving data between tools.
Feather inherits all of these benefits by virtue of being built on top of Arrow. When you save a DataFrame to Feather, it‘s automatically converted into Arrow‘s efficient columnar representation before being written to disk. Upon reading a Feather file, the data can be memory-mapped and operated on directly, bypassing any overhead.
The result is drastically faster read and write speeds that scale seamlessly to big data. For machine learning workflows that depend on rapidly iterating over large datasets, Feather is a game-changer.
Conclusion
In summary, Feather is a powerful tool for optimizing machine learning and data science workflows in Python. By leveraging the Apache Arrow columnar memory format, Feather enables:
- Speedups of up to 25x or more on read and write performance compared to CSV
- Effortless cross-language interoperability with standardized data types
- Reduced storage costs and memory usage through efficient compression
- Faster model training and iteration cycles on big data
Although CSV will always have its place as the most portable data exchange format, it‘s becoming increasingly unsuited for the demanding performance requirements of modern machine learning. Feather solves CSV‘s biggest pain points while providing a seamless upgrade path.
With PyArrow, adopting Feather in your Python workflows takes just a few lines of code. The resulting speedups in loading and processing data accumulate into massive productivity gains across all your projects and supercharge your iteration cycles.
If you‘re not using Feather yet, I highly recommend giving it a try the next time you‘re working with a large tabular dataset. Load it with Feather and marvel at the blazing fast speeds! Once you experience the difference yourself, you‘ll wonder how you ever put up with the sluggishness of CSVs.
To learn more about Feather and Apache Arrow, check out the following resources:
- Feather File Format Documentation
- Apache Arrow Website
- Wes McKinney – Apache Arrow: A Cross-Language Development Platform for In-Memory Data
Happy data crunching!