Skip to main content

Automated feature engineering using Large Language Models (LLMs) for tabular data

Project description

llm-feat

Python Version License: MIT

llm-feat is a Python library that automatically generates feature engineering code for pandas datasets using Large Language Models. Generate context-aware, target-specific features that can be automatically added to your DataFrame or customized before execution.

🌟 Key Features

  • 🤖 LLM-Powered: Uses OpenAI models to generate context aware feature engineering code.
  • 📝 Dual Modes: Get code suggestions or directly add features
  • 🔧 Jupyter Support: Automatically injects code into next cell in Jupyter notebooks
  • 🎯 Metadata-Driven: Uses column descriptions to generate contextually relevant features
  • 🎯 Target-Aware: Generates features specifically relevant to your prediction task

📦 Installation

To install the latest release of llm-feat from PyPI:

pip install llm-feat

🚀 Quick Start

1. Setup

import pandas as pd
import llm_feat

# Set your OpenAI API key
llm_feat.set_api_key("your-openai-api-key-here")
# Or use environment variable: export OPENAI_API_KEY="your-key"

2. Prepare Data and Metadata

Consider the problem of healthy and unhealthy labelling:

# Your dataset with target column
df = pd.DataFrame({
    'height': [170, 175, 180, 165, 185, 172, 178, 168, 182, 174],
    'weight': [70, 75, 80, 65, 85, 72, 78, 68, 83, 74],
    'bmi': [24.2, 24.5, 24.7, 23.9, 24.8, 24.3, 24.6, 24.1, 25.0, 24.4],
    'health_score': [1, 1, 0, 1, 0, 1, 1, 1, 0, 1]  # Target: 1=healthy, 0=unhealthy
})

# Metadata with column descriptions and target definition
metadata_df = pd.DataFrame({
    'column_name': ['height', 'weight', 'bmi', 'health_score'],
    'description': [
        'Height in centimeters',
        'Weight in kilograms',
        'Body Mass Index',
        'Health classification score'
    ],
    'data_type': ['numeric', 'numeric', 'numeric', 'numeric'],
    'label_definition': [None, None, None, '1 if healthy, 0 if unhealthy']
})

3. Generate Features

Mode 1: Get Code (Recommended for Jupyter)

We will now generate features for the above dataset based on feature descriptions in the metadata and prior knowledge of the LLM.

code = llm_feat.generate_features(df, metadata_df, mode='code', model='gpt-4o-mini')
print(code)

Example Output:

import numpy as np

df['height_weight_ratio'] = df['height'] / df['weight'].replace(0, np.nan)
df['bmi_squared'] = df['bmi'] ** 2
df['weight_bmi_interaction'] = df['weight'] * df['bmi']
df['health_score_bmi_diff'] = df['health_score'] - df['bmi'].apply(lambda x: 1 if x < 24.9 else 0)
df['height_bmi_category'] = pd.cut(df['bmi'], bins=[0, 18.5, 24.9, 29.9, np.inf], 
                                    labels=['Underweight', 'Normal', 'Overweight', 'Obese'])

Note: In Jupyter notebooks, the code is automatically injected into the next cell. The LLM generates target-aware features that are relevant to predicting health_score.

Mode 2: Direct Feature Addition

df_with_features = llm_feat.generate_features(df, metadata_df, mode='direct', model='gpt-4o-mini')
print(df_with_features.head())

Example Output:

   height  weight   bmi  health_score  height_weight_ratio  bmi_squared  \
0     170      70  24.2             1             2.428571      585.64   
1     175      75  24.5             1             2.333333      600.25   
2     180      80  24.7             0             2.250000      610.09   

   weight_bmi_interaction  health_score_bmi_diff height_bmi_category  
0                1694.0                      0              Normal  
1                1837.5                      0              Normal  
2                1976.0                      0              Normal  

📖 Usage Examples

Jupyter Notebook Example

See example_llm_feat.ipynb for complete usage examples in Jupyter notebook format.

Python Script Example

You can also run the example as a standalone Python script:

poetry run python example_llm_feat.py

Note: The notebook and script examples use df as the DataFrame variable name, which is required for the generated code to work directly.

Development Installation

Prerequisites

  • Python 3.10.19 or higher (tested with Python 3.10.19)
  • Poetry (for development)
  • Conda (optional, for environment management)
  1. Clone the repository:
    git clone https://github.com/codeastra2/llm-feat.git
    cd llm-feat
    
  2. Create conda environment:
    conda create -n llm_feat_310 python=3.10.19 -y;
    conda activate llm_feat_310
    
  3. Install Poetry (if not already installed):
    curl -sSL https://install.python-poetry.org | python3 -
    
  4. Install dependencies:
    poetry install
    
  5. Add env to jupyter kernels(to be used in ipython notebooks):
    poetry run python -m ipykernel install --user --name llm_feat_310 --display-name "Python (llm_feat_310)"
    

Running Tests

poetry run pytest

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🤝 Contributing

Contributions are welcome! Please check our GitHub repository for guidelines.

👤 Author

Srinivas Kumar - srinivas1996kumar@gmail.com

📚 Documentation

  • CHANGELOG.md - Version history and changes For any questions or issues, please open an issue on our GitHub repository.

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

llm_feat-0.1.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_feat-0.1.0-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file llm_feat-0.1.0.tar.gz.

File metadata

  • Download URL: llm_feat-0.1.0.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for llm_feat-0.1.0.tar.gz
Algorithm Hash digest
SHA256 53cc55aa47809ac408c5425274a85e1cb9021d75b39f2ff048fe4c5f00010da5
MD5 75d1b9237e63f56a03fe165bc7af376c
BLAKE2b-256 ab0342de6b04632fe978ea0fcf81447e34b32f0a2371188e07f4dfe285aa9dbf

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_feat-0.1.0.tar.gz:

Publisher: publish.yml on codeastra2/llm-feat

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llm_feat-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: llm_feat-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for llm_feat-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b2988e4c6be8a8a2b089438225d2cfc6992e6b9edf41d8774347d7a274f7539f
MD5 883b2ea89a8137ab28f1e500d573041a
BLAKE2b-256 4c5236d831acf4316a4266424b089561e9a12126b375423dc5854bf64ff869c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_feat-0.1.0-py3-none-any.whl:

Publisher: publish.yml on codeastra2/llm-feat

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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