Skip to main content

pysmartcor

pysmartcor detects variable types and selects a suitable correlation method for each pair. It supports continuous, count, binary, ordinal, and categorical variables and returns the estimate, inference, selected method, and rationale.

Install

Once the package is on PyPI, a single call installs it together with all of its dependencies:

python -m pip install pysmartcor

To install the supplied archive instead (pip resolves the dependencies automatically in both cases):

python -m pip install pysmartcor.zip

Install the optional plotting dependencies when needed:

python -m pip install 'pysmartcor[viz]'

Load the example data

The package includes gss_2024_casestudy.csv. This CSV is frozen for reproducibility. The examples below read the bundled copy and do not download data.

from importlib.resources import as_file, files
import pandas as pd

csv_resource = files("pysmartcor").joinpath("data/gss_2024_casestudy.csv")
with as_file(csv_resource) as csv_path:
    gss = pd.read_csv(csv_path)

Correlate one pair

from pysmartcor import smart_cor

result = smart_cor(
    gss["coninc"],
    gss["age"],
    x_name="coninc",
    y_name="age",
    verbose=False,
)

print(result)
result.estimate
result.method
result.p_value
result.ci_lower, result.ci_upper

Set assume_latent_normal to control pairs that can use a latent-variable method:

smart_cor(gss["degree"], gss["happy"], assume_latent_normal="auto")
smart_cor(gss["degree"], gss["happy"], assume_latent_normal=True)
smart_cor(gss["degree"], gss["happy"], assume_latent_normal=False)

The default, "auto", tests the assumption for each affected pair. A binary-by-binary table is saturated, so automatic selection uses phi. Set the argument to True to request tetrachoric correlation.

Build a matrix

from pysmartcor import smart_cormat

columns = ["age", "coninc", "degree", "happy", "sex", "region"]
matrix = smart_cormat(
    gss[columns],
    assume_latent_normal=False,
    verbose=False,
)

matrix.correlations
matrix.methods
matrix.types
matrix.to_long()

Use smart_cor_df() when a script needs plain pandas DataFrames:

from pysmartcor import smart_cor_df

plain = smart_cor_df(gss[columns], assume_latent_normal=False)
plain["correlations"]
plain["methods"]

Compare methods

from pysmartcor import compare_methods

comparison = compare_methods(
    gss["degree"],
    gss["happy"],
    assume_latent_normal=False,
    bootstrap=False,
    verbose=False,
)
comparison.results

Plot a matrix

Plotting requires the optional viz dependencies.

from pysmartcor.viz import cor_heatmap, method_heatmap

cor_heatmap(matrix)
method_heatmap(matrix)

Methods

The package implements Pearson, Spearman, Kendall's tau, point-biserial, rank-biserial, phi, tetrachoric, Yule's Q, polychoric, polyserial, Cramer's V, Theil's U, Tschuprow's T, and Goodman-Kruskal's gamma.

Required dependencies are NumPy, SciPy, and pandas. Matplotlib and seaborn are optional plotting dependencies.

Paper

The accompanying paper, smartcor: Intelligent Correlation Method Selection for Mixed Variable Types by M. Harshvardhan and Pritam Ranjan, is a work in progress. The vignettes of the R package and the articles on the package website cover the same material.

Authors

M. Harshvardhan (maintainer) and Pritam Ranjan.

License

GPL (>= 3)

Download files

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

Source Distribution

pysmartcor-1.0.0.tar.gz (83.3 kB view details)

Uploaded Source

Built Distribution

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

pysmartcor-1.0.0-py3-none-any.whl (72.3 kB view details)

Uploaded Python 3

File details

Details for the file pysmartcor-1.0.0.tar.gz.

File metadata

  • Download URL: pysmartcor-1.0.0.tar.gz
  • Upload date:
  • Size: 83.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for pysmartcor-1.0.0.tar.gz
Algorithm Hash digest
SHA256 77de106ac041c117a242ef956b84abd5ee77ba6db5736367a76e49fb9765add6
MD5 529363dba282d1834c49870d3390bd18
BLAKE2b-256 6fac2186019b90086e2248472b4cc4a902d4a84f1b6f93a551ac307f68968425

See more details on using hashes here.

File details

Details for the file pysmartcor-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pysmartcor-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 72.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for pysmartcor-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e95db6addac8b68712e6c90e1fb4b1a283bb0e90c5c907262f7ad4585ee72f7
MD5 284cd59dcce85eba1c29069e7295b158
BLAKE2b-256 328637a59a5c657189a980b03a86b17760cc7e02daac938615b076340f11736a

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