Skip to main content

discover_feature_relationships tries to find column-to-column relationships in your dataframe using machine learning

Project description

discover_feature_relationships

Attempt to discover 1D relationships between all columns in a DataFrame using scikit-learn (RandomForests) and standard correlation tests (Pearson, Spearman and Kendall via Pandas).

The goal is to see if we can better understand the data in a DataFrame by learning which features (1 column at a time) predict each other column. This code attempts to learn a predictive relationship between the Cartesian product (all pairs) of all columns.

Rather than just learning which column(s) predict a target column, we might want to know what other relationships exist (e.g. during Exploratory Data Analysis) and whether some predictive features are driven by other less-predictive features (to help us find new & better features or data sources). We might also sense-check out data by checking that certain relationships exist.

By default it assumes every target column is a regression challenge. You can provide a list of columns to treat as classification challenges. For regression we cap negative scores at 0 (r^2 can be arbitrarily negative, we cap at 0 to make this a little easier to interpret).

Text-encoded columns are automatically LabelEncoded (this is a sensible default but may not reveal information in your case, you might need to provide your own smarter encoding). This adds to the correlation plots in YellowBrick and Pandas Profiling where the text columns are not auto-encoded.

We might want to use this tool alongside:

The project (and the examples) live on GitHub:

Titanic example

Titanic Notebook

  • Embarked (classification) is predicted well by Fare, also by Age
  • Pclass (regression) is predicted by Fare but Fare (regression) is poorly predicted by Pclass
  • Sex (classification) is predicted well by Survived
  • Survived (classification) is predicted well by Sex, Fare, Pclass, SibSpParch
    • Predicting this feature at circa 0.62 is equivalent to "no information" as 0.62 is the mean of Survived
  • SibSpParch is predicted by both SibSp and Parch (SibSpParch is the sum of both - it is an engineered additional feature) - it is also predicted by Fare
  • SibSp and Parch are also predicted by Fare (but less well so than by SibSpParch)

alt text

This is generated using:

df = pd.read_csv("titanic_train.csv")
...

import discover
df_results = discover.discover(cols, classifier_overrides, df)

df_results.pivot(index='target', columns='feature', values='score').fillna(1) \
.style.background_gradient(cmap="viridis", low=0.3, high=0.0, axis=1) \
.set_precision(2)

Boston example

Boston Notebook

  • NOX predicts AGE and DIS (but not the other way around)
  • target predicts LSTAT, LSTAT weakly predicts target, LSTAT weakly predicts RM
  • DIS predicts AGE, AGE weakly predicts DIS
  • INDUS predicts CRIM and somewhat AGE, B
  • target weakly predicts RM, RM weakly predicts target

Requirements

  • python 3.6+
  • scikit-learn (0.19+)
  • pandas
  • jupyter notebook
  • matplotlib
  • seaborn
conda install scikit-learn pandas jupyter pytest seaborn
conda install -c conda-forge watermark

Setup

Install from PyPI

pip install discover_feature_relationships

https://pypi.org/project/discover-feature-relationships/

Install from source

First check-out from GitHub, then install with python setup.py install, then cd into the examples folder and run the Notebooks.

Tests

  • Run discover.py for a simple test that the code is working
  • Run pytest to run test_discover.py for a single unit test (use pytest -s to see print outputs)

Note to Ian for Development

Environment: . ~/anaconda3/bin/activate discover_feature_relationships

Installer

To push to PyPI I need to follow https://docs.python.org/3/distributing/index.html#distributing-index - specifically python se tup.py sdist bdist_wheel and twine upload dist/*. This uses https://pypi.org/project/twine/ .

TODO

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

discover_feature_relationships-1.0.3.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file discover_feature_relationships-1.0.3.tar.gz.

File metadata

  • Download URL: discover_feature_relationships-1.0.3.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.8

File hashes

Hashes for discover_feature_relationships-1.0.3.tar.gz
Algorithm Hash digest
SHA256 faa12280b93c87cdfe69c4b1b1524ebcd701282e21f0feca5467a9cc31cdd4ea
MD5 ff7cbd216b17b62b950f021a29cfa57e
BLAKE2b-256 c9a38087f6856b837f219e6291b7188a01c0af584e18749016ad22a3872f9170

See more details on using hashes here.

File details

Details for the file discover_feature_relationships-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: discover_feature_relationships-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.8

File hashes

Hashes for discover_feature_relationships-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e50d92e27fc5cb09bb08db73667edb670e9bba6354e5e4fc5662d84cf08780a1
MD5 b05cd5b33cd7e316c1aa92d9e0d228b1
BLAKE2b-256 8bfe22019933e2950e8d6224850afdc243ad770dcdf0a92bf4b525581ef84dc1

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