Skip to main content

A Python utility for analyzing and suggesting appropriate encoding methods for categorical columns in CSV datasets.

Project description

Categorical Data Analysis Tool

A Python utility for analyzing and suggesting appropriate encoding methods for categorical columns in CSV datasets. This tool helps data scientists and analysts make informed decisions about how to handle categorical variables in their machine learning pipelines.

Features

  • Automatic detection of categorical columns
  • Analysis of cardinality and unique value distributions
  • Intelligent encoding suggestions based on data characteristics
  • Detection of ordinal categorical variables
  • Identification of potential data quality issues
  • Comprehensive reporting of analysis results

Prerequisites

pandas>=1.0.0
numpy>=1.18.0

Installation

  1. Clone this repository or copy the script to your local machine
  2. Install the required dependencies:
    pip install pandas numpy
    

Usage

Run the script directly from the command line:

python categorical_analysis.py

When prompted, provide the path to your CSV file.

Function Parameters

The main analysis function analyze_categorical_columns() accepts the following parameters:

  • csv_file_path (str): Path to the input CSV file
  • max_unique_values (int, default=50): Maximum number of unique values for a column to be considered categorical
  • unique_ratio_threshold (float, default=0.1): Maximum ratio of unique values to total rows
  • cardinality_threshold (int, default=10): Threshold for suggesting one-hot encoding vs other encoding methods

Encoding Suggestions

The tool suggests one of three encoding methods based on the column characteristics:

  1. Ordinal Encoding: Suggested for categorical variables with natural ordering (detected through keywords like 'low', 'medium', 'high', etc.)
  2. One-Hot Encoding: Recommended for categorical variables with low cardinality (fewer unique values than the cardinality threshold)
  3. Frequency/Target/Hashing Encoding: Suggested for high-cardinality categorical variables

Output Format

The tool provides two types of output:

  1. Column Classification: Lists all columns in the dataset and whether they are identified as categorical
  2. Detailed Analysis: For each categorical column, provides:
    • Number of unique values
    • List of unique values (up to 10 shown)
    • Suggested encoding method
    • Additional notes (missing values, unique value counts, etc.)

Example Output

--------------------------------------------------
Column 'product_category' is categorical: True
Column 'price' is categorical: False
Column 'status' is categorical: True

Categorical Columns Analysis:

Column: product_category
Unique Values: 8
Encoding Suggestion: One-Hot Encoding

Unique Values:
  - Electronics
  - Clothing
  - Books
  ...

Additional Notes:
  - Contains missing values

Notes

  • The tool automatically detects categorical columns based on data type ('object' or 'category') and uniqueness criteria
  • Columns with numeric data types are excluded from the analysis
  • The tool provides warnings about potential data quality issues like missing values
  • Analysis results can be used to inform feature engineering decisions in machine learning pipelines

Limitations

  • Only processes CSV files
  • Assumes categorical data is stored as string or category dtype
  • May not detect numeric categorical variables
  • Limited to basic encoding suggestions without considering specific use cases

Contributing

Feel free to submit issues, fork the repository, and create pull requests for any improvements.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

categorical_finder-0.0.1.tar.gz (4.6 kB view details)

Uploaded Source

File details

Details for the file categorical_finder-0.0.1.tar.gz.

File metadata

  • Download URL: categorical_finder-0.0.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.5

File hashes

Hashes for categorical_finder-0.0.1.tar.gz
Algorithm Hash digest
SHA256 b2c23d3a0263df6ed337a089250b4fdf6995d2a8acf5ad87f1320bcbc69a5b2b
MD5 c3cd8ee4f515bae62694d0227956facd
BLAKE2b-256 01af0986bfc3bfa95ff7a3b27d040ed6c260e963b2bd8a86d71f0e42feb12b68

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page