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-2.1.0.tar.gz (59.5 kB view details)

Uploaded Source

Built Distributions

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

hapc-2.1.0-cp312-cp312-win_amd64.whl (517.1 kB view details)

Uploaded CPython 3.12Windows x86-64

hapc-2.1.0-cp312-cp312-macosx_15_0_universal2.whl (218.1 kB view details)

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

hapc-2.1.0-cp311-cp311-win_amd64.whl (513.9 kB view details)

Uploaded CPython 3.11Windows x86-64

hapc-2.1.0-cp311-cp311-macosx_15_0_universal2.whl (217.0 kB view details)

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

hapc-2.1.0-cp310-cp310-win_amd64.whl (512.2 kB view details)

Uploaded CPython 3.10Windows x86-64

hapc-2.1.0-cp310-cp310-macosx_15_0_universal2.whl (215.9 kB view details)

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

hapc-2.1.0-cp39-cp39-win_amd64.whl (518.0 kB view details)

Uploaded CPython 3.9Windows x86-64

hapc-2.1.0-cp39-cp39-macosx_15_0_universal2.whl (216.0 kB view details)

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

hapc-2.1.0-cp38-cp38-win_amd64.whl (511.8 kB view details)

Uploaded CPython 3.8Windows x86-64

hapc-2.1.0-cp38-cp38-macosx_15_0_universal2.whl (215.6 kB view details)

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

File details

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

File metadata

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

File hashes

Hashes for hapc-2.1.0.tar.gz
Algorithm Hash digest
SHA256 5054f52b9ec19ff52b285a7cba8ad1d8c276839a39d03a785149f55aa4e7f7ef
MD5 9f915a7cc97d0ee603a618ab617a829c
BLAKE2b-256 8b74053d8cdd36b939925753d74ba5624cca9075de8f7352fedf9156453648b8

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for hapc-2.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 0781c57deb24e67ad352be60f3047dcc8421c3934002145911a71a019bdbd6a4
MD5 ab10d1cb39d4a8472c4cafba55f32e1b
BLAKE2b-256 a0cc64f0108b26978c92a458e0b3f386ee2b44abc6dc9ffdd3640d7b2ec14e25

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hapc-2.1.0-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 e5821bc9c7cdca24db0a02242091193ddc956f1a96fd23d6c4a63e239e8b616c
MD5 c0e44ec086c41fb9ff793e9a644d87ac
BLAKE2b-256 edb2ca5a181010ecda254b64e12b43b16214804fa8f68f50e1c0a129c2ff3045

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for hapc-2.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e90f06b57e5523cf3e6b2846df8868bb5e75519300a89e68475d161944679eb0
MD5 59353443299a833c4623e2a1b43c0239
BLAKE2b-256 e0caf388785da6efeddc11a4809b6a48c46d6cc5254338a37d4fd6e98b469a17

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hapc-2.1.0-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 4e24b820fb26a782dd7e75450caa0595afd3821a872272311c8bb86db758c853
MD5 c8794bf09918bb4a36d4e2d9d56dcc83
BLAKE2b-256 2c71f2e89c31fee914176b6f8cc6553813108660c919465e89fe9edaf2f94abf

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for hapc-2.1.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4c29b595ab19f9ca21db2f28f516702633edd4e59216f0fb5f67549f140fc048
MD5 a9ab91fb8f2f88d042adda0d7f764d71
BLAKE2b-256 42e8f41e32b01248795738612cdd97a5778d73dc9abc397d9ed341d2a78e5d68

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hapc-2.1.0-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 57018fe9619eeed3aa50f6312287c974c9ad99e1421607258c43aea90cf9bc34
MD5 3083c4e0a72f0671d32ababf54bcc0c0
BLAKE2b-256 d0b31aa73c357de52b0b6df36bafec7ab2dceac88b7edc2057a89ba153758e9e

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for hapc-2.1.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 020981475acdff82a80dc1bb971108548b4b057239a787986d5854ad7d5a30aa
MD5 e8ca0817fc320d0b45c27a266e21d84c
BLAKE2b-256 421e1e2faf2c5d039b9df23a7d9729e54bf9b625c08acc16854caf69dd9d8c15

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.1.0-cp39-cp39-macosx_15_0_universal2.whl
  • Upload date:
  • Size: 216.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.13

File hashes

Hashes for hapc-2.1.0-cp39-cp39-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3800dddff6c8836908087b7bd29f9f941d2b302a4fbda4313658a4ed081fe696
MD5 9d88d356f7997927ed8b9812664eb164
BLAKE2b-256 3e6f68ca4b1d987c4852579f374f799ac2e7f2d68df1b0f4cc66574edcc61d9e

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for hapc-2.1.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 ad2d4aa4f95441b63d7bba84d8d7f90708ee3100f50c75cf5d818c2008399e62
MD5 e66b90d97bf3f6de64d40eaedd0ceb24
BLAKE2b-256 de3bfd8f5854a22e7765bad1fe640cd5a9863e7ee5f54d4b840ce88e72530e18

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.1.0-cp38-cp38-macosx_15_0_universal2.whl
  • Upload date:
  • Size: 215.6 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.13

File hashes

Hashes for hapc-2.1.0-cp38-cp38-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 1e07b968cfcce1204718f33ddaab1f91b23aea6be47db182262b3ada895ae739
MD5 07ce7a83fcbd096e96a4128f2895f2c0
BLAKE2b-256 398b32eae337c1f410e37fbf6ddcd1caa8fc96cb90eb71fcfa7e9307cd57dd0f

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