Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Filter by Categories
About Article
Analyze Data
Archive
Best Practices
Better Outputs
Blog
Code Optimization
Code Quality
Command Line
Course
Daily tips
Dashboard
Data Analysis & Manipulation
Data Engineer
Data Visualization
DataFrame
Delta Lake
DevOps
DuckDB
Environment Management
Feature Engineer
Git
Jupyter Notebook
LLM
LLM Tools
Machine Learning
Machine Learning & AI
Machine Learning Tools
Manage Data
MLOps
Natural Language Processing
Newsletter Archive
NumPy
Pandas
Polars
PySpark
Python Helpers
Python Tips
Python Utilities
Scrape Data
SQL
Testing
Time Series
Tools
Visualization
Visualization & Reporting
Workflow & Automation
Workflow Automation

Feature Engineer

Encode Rare Labels with Feature-engine

When dealing with features with high cardinality, you might want to mark the rare categories as “Other”. Feature-engine’s RareLabelEncoder makes it easy for you to do so.

In the code above, we use RareLabelEncoder to replace categories with the frequency below 0.05 in the column “education” with “Other”.

Link to Feature-engine.

My previous tips on feature engineering.

Encode Rare Labels with Feature-engine Read More »

Similarity Encoding for Dirty Categories Using dirty_cat

When encoding categorical variables, you might want to capture the similarities among these categories such as ‘Master Police Officer’ and ‘Police Officer III’. If so, use dirty-cat.

In the code above, I use dirty-cat’s SimilarityEncoder to encode the titles while capturing their similarities.

The correlation matrix shows how similar two labels are using the encoded values. We can see that the similarity between ‘Master Police Officer’ and ‘Police Officer III’ is 0.86.

Link to dirty-cat.

Link to my full article about dirty-cat.

Similarity Encoding for Dirty Categories Using dirty_cat Read More »

Datacommons: Get Statistics about a Location in One Line of Code

If you want to get some interesting statistics about a location in one line of code, try Datacommons.Datacommons is a publicly available data from open sources (census.gov, cdc.gov, data.gov, etc.). In the example above, I used Datacommons to get the median income in California over time.

Find other interesting statistics using Datacommons here.

Link to Datacommons.

Datacommons: Get Statistics about a Location in One Line of Code Read More »

fastai’s cont_cat_split: Get a DataFrame’s Continuous and Categorical Variables Based on Their Cardinality

To get a DataFrame’s continuous and categorical variables based on their cardinality, use fastai’s cont_cat_split method.
If a column consists of integers, but its cardinality is smaller than the max_card parameter, it is considered as a category variable.
Find an example of this method above.
Link to the source code.
Link to the documentation.

fastai’s cont_cat_split: Get a DataFrame’s Continuous and Categorical Variables Based on Their Cardinality Read More »

fastai’s df_shrink: Shrink DataFrame’s Memory Usage in One Line of Code

Changing data types of DataFrame columns to smaller data types can significantly reduce the memory usage of the DataFrame.
Instead of manually choosing smaller data types, is there a way that you can automatically change data types in one line of code?
That is when the df_shrink method of fastai comes in handy. In the code above, the memory usage of the DataFrame decreases from 200 bytes to 146 bytes,
Learn more about df_shrink here.
Link to the source code.

fastai’s df_shrink: Shrink DataFrame’s Memory Usage in One Line of Code Read More »

Scroll to Top

Work with Khuyen Tran

Work with Khuyen Tran