Skip to main content

Highly Adaptive Principal Components

Project description

HAPC: Highly Adaptive Prinicipal Components

A fast and flexible machine learning library for nonparametric high-dimensional regression and classification with guarantees.

Installation

Prerequisites

  • Python 3.8+
  • C++ compiler (g++, clang, or MSVC)
  • CMake 3.15+
  • Eigen3

Quick Install

pip install hapc

Install from GitHub (latest development version)

pip install git+https://github.com/yourusername/hapc.git

Or with editable install for development:

git clone https://github.com/yourusername/hapc.git
cd hapc
pip install -e .

Install build dependencies

If installation fails, you may need to install build dependencies:

macOS:

brew install cmake eigen

Ubuntu/Debian:

sudo apt-get install cmake libeigen3-dev build-essential

Windows:

pip install cmake
# Install Visual Studio Build Tools or use conda
conda install -c conda-forge eigen

Quick Start

import numpy as np
from hapc.single import single_pcghal
from hapc.cv import pcghal_cv

# Generate sample data
X = np.random.randn(100, 5)
Y = X[:, 0] + 0.5 * X[:, 1] + np.random.randn(100) * 0.1

# Single fit with fixed lambda
result = single_pcghal(X, Y, maxdeg=2, npc=5, single_lambda=0.01)
print(f"Risk: {result.optimizer_output.risk:.6f}")

# Cross-validation to select lambda
lambdas = np.logspace(-4, 0, 10)
cv_result = pcghal_cv(X, Y, maxdeg=2, npc=5, lambdas=lambdas, nfolds=5)
print(f"Best lambda: {cv_result.best_lambda:.6f}")

# Make predictions
X_test = np.random.randn(20, 5)
result = single_pcghal(X, Y, maxdeg=2, npc=5, single_lambda=0.01, predict=X_test)
print(f"Predictions: {result.predictions}")

Usage

Regression

from hapc.single import single_pcghal

result = single_pcghal(
    X, Y,
    maxdeg=2,        # Maximum degree of interactions
    npc=10,          # Number of principal components
    single_lambda=0.01,
    predict=X_test   # Optional: test data for predictions
)

Classification

from hapc.single import single_pcghal

result = single_pcghal(
    X, Y_binary,
    maxdeg=2,
    npc=10,
    single_lambda=0.01,
    predict=X_test
)

Cross-Validation

from hapc.cv import pcghal_cv

cv_result = pcghal_cv(
    X, Y,
    maxdeg=2,
    npc=10,
    lambdas=np.logspace(-4, 0, 20),
    nfolds=5
)
print(cv_result.best_lambda)

API Reference

hapc.single.single_pcghal()

Fit PC-GHAL with a single lambda value.

Parameters:

  • X (ndarray, shape (n, p)): Input features
  • Y (ndarray, shape (n,)): Response variable
  • maxdeg (int): Maximum degree of interactions
  • npc (int): Number of principal components
  • single_lambda (float): Regularization parameter
  • max_iter (int, default=100): Maximum iterations
  • tol (float, default=1e-6): Convergence tolerance
  • verbose (bool, default=False): Print progress
  • predict (ndarray, optional): Test data for predictions
  • center (bool, default=True): Center the design matrix

Returns:

  • result.optimizer_output.alpha: Coefficients
  • result.optimizer_output.risk: Final risk
  • result.optimizer_output.iter: Iterations until convergence
  • result.predictions: Predictions on test data (if provided)

hapc.cv.pcghal_cv()

Cross-validation to select lambda.

Parameters:

  • lambdas (ndarray): Grid of lambda values to test
  • nfolds (int, default=5): Number of CV folds
  • ...other parameters same as single_pcghal

Returns:

  • cv_result.best_lambda: Optimal lambda
  • cv_result.mses: CV errors for each lambda
  • cv_result.best_model: Fitted model with best lambda
  • cv_result.predictions: Predictions on test data (if provided)

Contributing

Contributions welcome! The C++ core is shared between R and Python packages.

git clone https://github.com/yourusername/hapc.git
cd hapc
pip install -e .
pytest

License

MIT License - see LICENSE file

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

hapc-0.2.1.tar.gz (40.2 kB view details)

Uploaded Source

Built Distributions

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

hapc-0.2.1-cp312-cp312-win_amd64.whl (196.4 kB view details)

Uploaded CPython 3.12Windows x86-64

hapc-0.2.1-cp312-cp312-macosx_15_0_universal2.whl (159.7 kB view details)

Uploaded CPython 3.12macOS 15.0+ universal2 (ARM64, x86-64)

hapc-0.2.1-cp311-cp311-win_amd64.whl (195.3 kB view details)

Uploaded CPython 3.11Windows x86-64

hapc-0.2.1-cp311-cp311-macosx_15_0_universal2.whl (159.2 kB view details)

Uploaded CPython 3.11macOS 15.0+ universal2 (ARM64, x86-64)

hapc-0.2.1-cp310-cp310-win_amd64.whl (194.5 kB view details)

Uploaded CPython 3.10Windows x86-64

hapc-0.2.1-cp310-cp310-macosx_15_0_universal2.whl (157.9 kB view details)

Uploaded CPython 3.10macOS 15.0+ universal2 (ARM64, x86-64)

hapc-0.2.1-cp39-cp39-win_amd64.whl (196.7 kB view details)

Uploaded CPython 3.9Windows x86-64

hapc-0.2.1-cp39-cp39-macosx_15_0_universal2.whl (158.0 kB view details)

Uploaded CPython 3.9macOS 15.0+ universal2 (ARM64, x86-64)

hapc-0.2.1-cp38-cp38-win_amd64.whl (194.2 kB view details)

Uploaded CPython 3.8Windows x86-64

hapc-0.2.1-cp38-cp38-macosx_15_0_universal2.whl (157.7 kB view details)

Uploaded CPython 3.8macOS 15.0+ universal2 (ARM64, x86-64)

File details

Details for the file hapc-0.2.1.tar.gz.

File metadata

  • Download URL: hapc-0.2.1.tar.gz
  • Upload date:
  • Size: 40.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for hapc-0.2.1.tar.gz
Algorithm Hash digest
SHA256 eabd4538765a014004c68236532221c07e544528509c0feab21320a8255f7a44
MD5 20e981bfeb4a68bd96ff7ef71efb1fdf
BLAKE2b-256 59f41503a8a131505d278a100a0aa74c2986e43b4c9aacc47e6ececc76e3489c

See more details on using hashes here.

File details

Details for the file hapc-0.2.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: hapc-0.2.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 196.4 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for hapc-0.2.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 eee01b5fec5189318b817f9839833bff61188fa83d25dc570e1f164f015aa0cc
MD5 c6b9326f14b244a7e612665109780096
BLAKE2b-256 9324d5fde20136b0b230cf89371bb5191b769a08d54029afb9281bd9aab634e8

See more details on using hashes here.

File details

Details for the file hapc-0.2.1-cp312-cp312-macosx_15_0_universal2.whl.

File metadata

  • Download URL: hapc-0.2.1-cp312-cp312-macosx_15_0_universal2.whl
  • Upload date:
  • Size: 159.7 kB
  • Tags: CPython 3.12, macOS 15.0+ universal2 (ARM64, x86-64)
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for hapc-0.2.1-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b2929c47ed84323ad04e75a82a2b413b7c347c93cd48657111b19ce3f46dd028
MD5 ad21324af2e36b45242038c13062fd42
BLAKE2b-256 dbf9527f81e459c14cfb9e0e0a378c955042665c13ff81aa8a5fb0a3ef16c771

See more details on using hashes here.

File details

Details for the file hapc-0.2.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: hapc-0.2.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 195.3 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for hapc-0.2.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 dde27caddd8de1bb340ce73a6e6c43588bedc7eb5cc4b827d6f211c9890628ef
MD5 0c40ffc18410f1871e71a98408a48c57
BLAKE2b-256 844a1d575b31c873531a7f4286c9cc39978cd85afa31f3f5ce44d08df73519c5

See more details on using hashes here.

File details

Details for the file hapc-0.2.1-cp311-cp311-macosx_15_0_universal2.whl.

File metadata

  • Download URL: hapc-0.2.1-cp311-cp311-macosx_15_0_universal2.whl
  • Upload date:
  • Size: 159.2 kB
  • Tags: CPython 3.11, macOS 15.0+ universal2 (ARM64, x86-64)
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for hapc-0.2.1-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 759b6d1363ce068b58927fc5793be1bed3d6e4f82f5fe2b8ec5e7b0c9d838a69
MD5 43546c70d0da014caa4293968542284d
BLAKE2b-256 6608457dab0c0e2ff57ae6e5d6b268a6f9181318a529e101457e8994ae4f5526

See more details on using hashes here.

File details

Details for the file hapc-0.2.1-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: hapc-0.2.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 194.5 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for hapc-0.2.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 21e1ec4123ff530903e89ff0280d22643e10ac5700d5b99e484b6e036210a940
MD5 8d17ee0673e7d9f09496d11ca2dc09ae
BLAKE2b-256 545668dca8816da075d28c1004fdce3fd13e518a05a0b9c4b4d7c9566b8cd142

See more details on using hashes here.

File details

Details for the file hapc-0.2.1-cp310-cp310-macosx_15_0_universal2.whl.

File metadata

  • Download URL: hapc-0.2.1-cp310-cp310-macosx_15_0_universal2.whl
  • Upload date:
  • Size: 157.9 kB
  • Tags: CPython 3.10, macOS 15.0+ universal2 (ARM64, x86-64)
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for hapc-0.2.1-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 09b03e3196e54a206047ba3179c979d084cc36416ac83f8a28719eef846697bf
MD5 7aa5c4f2227499df9ef0d144c22f25cb
BLAKE2b-256 40f20ea3f2a091f9efbc7791f040ada93a2fa2fe2497c1d0a0ae76c2befe2bd9

See more details on using hashes here.

File details

Details for the file hapc-0.2.1-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: hapc-0.2.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 196.7 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for hapc-0.2.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 369e4cd204426cda599d18f8a8628ebb7479e49e42dfb64a80a7fa21f5597d20
MD5 dd8c23a7cbc07d4c30cff3316e45875e
BLAKE2b-256 73bffc2159b9a6bcd1848d3b7526d69086bf52c2f98a09a29a0857a758bfa9f1

See more details on using hashes here.

File details

Details for the file hapc-0.2.1-cp39-cp39-macosx_15_0_universal2.whl.

File metadata

  • Download URL: hapc-0.2.1-cp39-cp39-macosx_15_0_universal2.whl
  • Upload date:
  • Size: 158.0 kB
  • Tags: CPython 3.9, macOS 15.0+ universal2 (ARM64, x86-64)
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for hapc-0.2.1-cp39-cp39-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 daee86ac6631311661c17c41eb20c7b165bb0a871a87f3a239b5c1c55791855f
MD5 1dc87cea2ceffd67c50494180b463ae5
BLAKE2b-256 b6f80b7b65c2ca271c38899fa7ef10dd0120fe286c0f7f84787161797e064991

See more details on using hashes here.

File details

Details for the file hapc-0.2.1-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: hapc-0.2.1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 194.2 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for hapc-0.2.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 bc8b126aabc0fd70b67316596faec06200409c7f69c65e14ed3a990ec2c777d8
MD5 3c229cb339fa89086a49716b576b82ec
BLAKE2b-256 a6590d5e2c6d8970c89b26679339531697381a1fb9c925d3d89435a957919cf0

See more details on using hashes here.

File details

Details for the file hapc-0.2.1-cp38-cp38-macosx_15_0_universal2.whl.

File metadata

  • Download URL: hapc-0.2.1-cp38-cp38-macosx_15_0_universal2.whl
  • Upload date:
  • Size: 157.7 kB
  • Tags: CPython 3.8, macOS 15.0+ universal2 (ARM64, x86-64)
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for hapc-0.2.1-cp38-cp38-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 02d839a7884b0d92c87c3db0bc480a3458002558b59cff5740af70bf071166d6
MD5 a8fa3e8a50e2afd4cd03f14804a8b98f
BLAKE2b-256 fdfde5d5dd703b99cf501ac4c6e0ea83b0b56a8730c9d7f5444badb8ee4d1f86

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