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.0.tar.gz (40.0 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.0-cp312-cp312-win_amd64.whl (196.4 kB view details)

Uploaded CPython 3.12Windows x86-64

hapc-0.2.0-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.0-cp311-cp311-win_amd64.whl (195.3 kB view details)

Uploaded CPython 3.11Windows x86-64

hapc-0.2.0-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.0-cp310-cp310-win_amd64.whl (194.5 kB view details)

Uploaded CPython 3.10Windows x86-64

hapc-0.2.0-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.0-cp39-cp39-win_amd64.whl (196.7 kB view details)

Uploaded CPython 3.9Windows x86-64

hapc-0.2.0-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.0-cp38-cp38-win_amd64.whl (194.2 kB view details)

Uploaded CPython 3.8Windows x86-64

hapc-0.2.0-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.0.tar.gz.

File metadata

  • Download URL: hapc-0.2.0.tar.gz
  • Upload date:
  • Size: 40.0 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.0.tar.gz
Algorithm Hash digest
SHA256 d071cc904de8e8d58b4035512d80a792fee3035e6f044043718b1da7e1332ab0
MD5 81577d75c3cfca666e4e0f24b41f05e9
BLAKE2b-256 0d07e9be21a057a293e16729d7e13937e76ba39a2e90a0adee9911eeff120171

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-0.2.0-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.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8891bde84a2f70c08b61f18d5b93e1e5a8f4b89dd71482b4817cf62bf2a506b7
MD5 dfae36b50ee5dddd01c6a999a27a2f51
BLAKE2b-256 3bfe2df07de46d2287a6725f98d94580588bd9bcf057b9518c68515f574815b0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-0.2.0-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.0-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 115e01dfe1d1056f0bcf1ac2d733883edabc0b8fc5b44885b25cd9383084b2ba
MD5 3346a80a6c5f9ccacdaa3277df5e938d
BLAKE2b-256 65953e23c41c192e9b78fa59e306e93e4ee6dd20a1a865b727ae15dadd60e664

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-0.2.0-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.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 146a6bc333328e182f551e0b1519a11f9572a516067bc19885eff2b3df204213
MD5 8a159003e8fd1b9ad0e333fb8c328117
BLAKE2b-256 70558a0f0d6a1f10fcabee0ff84fd05e62df2133c534893a74f40d6bef5000d9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-0.2.0-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.0-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b0796a86b2704ec3b81fa4e10ad58e279f63b3dd92cf3035f2758c2cedff91f3
MD5 927039eac9db381377fc39b90dda696f
BLAKE2b-256 e4760acbf03d091b99d567a38ee00cc00716e3922def168e175d7a017df0aab3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-0.2.0-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.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 19cba997ecbf874a0942ebe4fee4675191516d80917d7cbe3f3998a96da9c597
MD5 c16d04e4bd69939eead0444a4673c98b
BLAKE2b-256 82d56957df003f0e317aacd552c350cd8f2e784d730015500cf542c208c42a70

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-0.2.0-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.0-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 58c3d22ad07d64c79aca5bea6acfcfa6df79f16eaf710f47ea434c3eb79b4215
MD5 55e71d8094a87be6f7e9e7bea006f6c7
BLAKE2b-256 f207de5ddc2b7eeafa79e8de9987f38df305f48bf307bc6ba9ba8e4addf9bb56

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-0.2.0-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.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 c23a032aa90f39b890a884cc912a6e76c43f63ec0c23943b23c4569bddb4506b
MD5 b06e4de7f18c77c9577edf3ee55b29ac
BLAKE2b-256 7951eda98e09ad52e8582bec72257e0eb952b7eba86d56182dd67dddcff037d3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-0.2.0-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.0-cp39-cp39-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3e0dec76f90e0cd390a94fb4aa5f9233d4f98d09c8e09510db101ba989ae67fd
MD5 ef05051efa41a50d39c5ad0bcc3633b9
BLAKE2b-256 bd0c3e41456e5af7e1dce92c17d9b7f09bc33acb719f0c33ebeb190fdecb55f0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-0.2.0-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.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 a8e87c0f77d879762b4fdc111e3ae63b01031c69db50833e72faffb50e97f0b8
MD5 9ee6a42953c90e6f95f249ff0b531881
BLAKE2b-256 0062428e7077611a49d3c90e1c00ab8f989c1ca7b91bab84495f8ce7e05254bc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-0.2.0-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.0-cp38-cp38-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 444c4748c7c9bce25845f3cb767b84c1cbc2726c87c82c05af972f3d65b7a436
MD5 9be1af8a3f763cc244c39b76d382b3af
BLAKE2b-256 c093d37072b68c82a273ee43335ddeba59f49f7b5dc518b676d20b8c1a234823

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