Skip to main content

A column classifier using spaCy for entity recognition.

Project description

Here is the entire README in a single markdown block for easy copying:

spacy-column-classifier

A Python package that classifies DataFrame columns into Named Entity (NER) or Literal types using spaCy's powerful natural language processing models. This library is optimized for batch processing, making it efficient for working with large datasets.

Features

  • Classification of Columns: Classifies each column of a DataFrame as Named Entity (NER) or Literal (LIT) types, including LOCATION, ORGANIZATION, PERSON, NUMBER, DATE, and more.
  • Batch Processing: Uses spaCy’s nlp.pipe() to efficiently process multiple columns across multiple tables in parallel, improving performance for large datasets.
  • Customizable: Supports both transformer-based models (for high accuracy) and smaller models (for speed).
  • Handles Multiple DataFrames: Allows you to classify columns across multiple DataFrames in one go.
  • Conflict Resolution: Handles cases where multiple class types are detected for a single column and resolves conflicts based on customizable thresholds.

Installation

You can install the package via pip:

pip install column-classifier

Make sure you have installed one of the compatible spaCy models:

For accuracy (slower but more precise):

python -m spacy download en_core_web_trf

For speed (faster but less accurate):

python -m spacy download en_core_web_sm

Quick Start

Here’s how you can use spacy-column-classifier in your project with hardcoded example data:

import pandas as pd
from column_classifier import ColumnClassifier

# Hardcoded sample data
data1 = {
    'title': ['Inception', 'The Matrix', 'Interstellar'],
    'director': ['Christopher Nolan', 'The Wachowskis', 'Christopher Nolan'],
    'release year': [2010, 1999, 2014],
    'domestic distributor': ['Warner Bros.', 'Warner Bros.', 'Paramount'],
    'length in min': [148, 136, 169],
    'worldwide gross': [829895144, 466364845, 677471339]
}

data2 = {
    'company': ['Google', 'Microsoft', 'Apple'],
    'location': ['California', 'Washington', 'California'],
    'founded': [1998, 1975, 1976],
    'CEO': ['Sundar Pichai', 'Satya Nadella', 'Tim Cook'],
    'employees': [139995, 163000, 147000],
    'revenue': [182527, 168088, 274515]
}

# Create DataFrames
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

# List of DataFrames to classify
dataframes = [df1, df2]

# Create an instance of ColumnClassifier
classifier = ColumnClassifier(model_type='accurate')  # 'accurate' for transformer model

# Classify multiple DataFrames
results = classifier.classify_multiple_tables(dataframes)

# Display the results
for table_result in results:
    for table_name, classification in table_result.items():
        print(f"Results for {table_name}:")
        for col, types in classification.items():
            print(f"  Column '{col}': Classified as {types['classification']}")
        print()

API Reference

ColumnClassifier

The main class used to classify DataFrame columns.

Parameters:

•	model_type: Choose between ‘accurate’ (transformer-based) or ‘fast’ (small model).
•	sample_size: Number of samples to analyze per column.
•	classification_threshold: Minimum threshold for confident classification.
•	close_prob_threshold: Threshold for resolving conflicts between close probabilities.
•	word_threshold: If the average word count in a column exceeds this, the column is classified as a DESCRIPTION.

Methods:

•	classify_multiple_tables(tables: list) -> list: Classifies all columns across multiple DataFrames. Returns a list of dictionaries containing the classification results.
•	classify_column(column_data: pd.Series) -> dict: Classifies a single column and returns a dictionary of classifications and probabilities.

Example Output

After classifying your DataFrames, the output will be structured like this:

[
  {
    "table_1": {
      "title": {
        "classification": "OTHER",
        "probabilities": {
          "OTHER": 1.0
        }
      },
      "director": {
        "classification": "PERSON",
        "probabilities": {
          "PERSON": 1.0
        }
      },
      "release year": {
        "classification": "NUMBER",
        "probabilities": {
          "NUMBER": 1.0,
          "DATE": 1.0
        }
      },
      "domestic distributor": {
        "classification": "ORGANIZATION",
        "probabilities": {
          "ORGANIZATION": 1.0
        }
      },
      "length in min": {
        "classification": "NUMBER",
        "probabilities": {
          "NUMBER": 1.0
        }
      },
      "worldwide gross": {
        "classification": "NUMBER",
        "probabilities": {
          "NUMBER": 1.0
        }
      }
    }
  },
  {
    "table_2": {
      "company": {
        "classification": "ORGANIZATION",
        "probabilities": {
          "ORGANIZATION": 1.0
        }
      },
      "location": {
        "classification": "LOCATION",
        "probabilities": {
          "LOCATION": 1.0
        }
      },
      "founded": {
        "classification": "NUMBER",
        "probabilities": {
          "NUMBER": 1.0,
          "DATE": 1.0
        }
      },
      "CEO": {
        "classification": "PERSON",
        "probabilities": {
          "PERSON": 1.0
        }
      },
      "employees": {
        "classification": "NUMBER",
        "probabilities": {
          "NUMBER": 1.0
        }
      },
      "revenue": {
        "classification": "NUMBER",
        "probabilities": {
          "NUMBER": 1.0
        }
      }
    }
  }
]

Each column is classified with a winning classification, and the probabilities show the likelihood of different class types detected in the column.

License

This project is licensed under the Apache License.

This version should be easier to copy and paste correctly without errors.

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

column_classifier-0.1.2.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

column_classifier-0.1.2-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file column_classifier-0.1.2.tar.gz.

File metadata

  • Download URL: column_classifier-0.1.2.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.6

File hashes

Hashes for column_classifier-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b2cf92dc401c3bcd4da222ba5d402f89626959d6e6298313dba772195304d603
MD5 3bded5b0a12e6ae8e50ff68a97b8b969
BLAKE2b-256 1e1ed7000129473123257906a99a63d5746fa442aef312d2daae84faed2da4ca

See more details on using hashes here.

File details

Details for the file column_classifier-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for column_classifier-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 39866c5eb9acd6dc67c889eb2cc8df89e7b3f3d3657b5d79ce7acb66b3149664
MD5 54049e9494ebffad5df97a836e0b2fa4
BLAKE2b-256 f6e8b04f5591b6b1ebb0c8339dffcb9bc1e47a5acaf28ed7f071d05e8a61cd81

See more details on using hashes here.

Supported by

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