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.3.0.tar.gz (61.6 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.3.0-cp312-cp312-win_amd64.whl (518.5 kB view details)

Uploaded CPython 3.12Windows x86-64

hapc-2.3.0-cp312-cp312-macosx_15_0_universal2.whl (219.7 kB view details)

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

hapc-2.3.0-cp311-cp311-win_amd64.whl (515.4 kB view details)

Uploaded CPython 3.11Windows x86-64

hapc-2.3.0-cp311-cp311-macosx_15_0_universal2.whl (218.5 kB view details)

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

hapc-2.3.0-cp310-cp310-win_amd64.whl (513.7 kB view details)

Uploaded CPython 3.10Windows x86-64

hapc-2.3.0-cp310-cp310-macosx_15_0_universal2.whl (217.4 kB view details)

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

hapc-2.3.0-cp39-cp39-win_amd64.whl (519.5 kB view details)

Uploaded CPython 3.9Windows x86-64

hapc-2.3.0-cp39-cp39-macosx_15_0_universal2.whl (217.6 kB view details)

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

hapc-2.3.0-cp38-cp38-win_amd64.whl (513.3 kB view details)

Uploaded CPython 3.8Windows x86-64

hapc-2.3.0-cp38-cp38-macosx_15_0_universal2.whl (217.2 kB view details)

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

File details

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

File metadata

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

File hashes

Hashes for hapc-2.3.0.tar.gz
Algorithm Hash digest
SHA256 86c4980e6560843afbd13613b947b142045db106c3b96e633acf909b57b780fd
MD5 c3d275ff9d5ee6c49f978d9eabe250bc
BLAKE2b-256 950f18368777178a36a5914f55ac4fa4fd2eed541fd64d758a813a554c4420bf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.3.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 518.5 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.3.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 22159591292cd3040ef85202e26146e6d97ce857c5e7ef74411bd02eea0103e2
MD5 4e572853b946bdc2e07092e96055159c
BLAKE2b-256 3906db64b258820fc08bcc383aa9ca715c023944ac89abb561b074aed44c8dbc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hapc-2.3.0-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b7a8509c892beeae1e03e10954fbb0b678035044a652661cc21823151a635580
MD5 4adc19b1c636606bdd49d8e249988fe7
BLAKE2b-256 029e94dabc4e3d5eb21942f41ced18e5d9775c205d8326424d298b8283891c85

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.3.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 515.4 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.3.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 32cd0b853b0da058354a3b34c8c68ce4eec577d4a38c8b39502de0346a6bcc18
MD5 b0205f403ce6aae69ce6cc6463447fee
BLAKE2b-256 47cce2106e24ba87f94e07f8121abc16320731ec00e17c9b6b250ca69b441eaa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hapc-2.3.0-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 2be66f5f038ba21ec3fd78271036b2fbbbb2acc2199e4f6fbba37ae7695894c8
MD5 40bc29a8277f1a245412ca396d7dd558
BLAKE2b-256 74e276ee5e048e9e3ffa3a467a6d98ead78dbcd31db8db4babcb4e79a915d52a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.3.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 513.7 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.3.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 159aacb3a3ef1ff52eebb49441dfad933c92c6396dcd53de2adc016fd24de2e0
MD5 db1052d7cd6aa91ce559d41fa3bf0f52
BLAKE2b-256 bbf366645f73a1038075ce457d8756b88b7e7aeea9f3813150647307f65b6103

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hapc-2.3.0-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 99bcc021c0ec510b0906674fe0f8e5eb8b53a2eb1cd34e0b14b83bc9de058aca
MD5 8dde2450d61bd92936964615c859ef9c
BLAKE2b-256 22d71613be56fc9c3c49e9a574630a35c1be5ba0703791a2887c501e2bb4dee8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.3.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 519.5 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.3.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 736908615072036200be618d5d50817a5c20ba854a039e34c652395c6617333b
MD5 b2f36452e1c9156aa64e822cc7e23997
BLAKE2b-256 f462391638668703e4a78f9a590445a85b93f2913ccaeb6f8292107492522eca

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.3.0-cp39-cp39-macosx_15_0_universal2.whl
  • Upload date:
  • Size: 217.6 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.3.0-cp39-cp39-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 a8c010a9cbccc720a42c15db48ba0432b20bf5b675cf67602c7b04bad51a0742
MD5 ae4dd349325e09e9cf4dc9fbd81ab319
BLAKE2b-256 578011875ce352f63a520a16712415a9204e1eafff4314759472b7a19c41288d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.3.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 513.3 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.3.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 e21ab00a02625c26f6d782ce45bf3ceb756f97d37dbf600cd23fef29e56ebe56
MD5 3b5ba86cbc8dcdbba3b5130dabcf6040
BLAKE2b-256 c6c585196dbf9f4c4343449106b3b40eb2a85812f5f9bdd0925434de5a4268d2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.3.0-cp38-cp38-macosx_15_0_universal2.whl
  • Upload date:
  • Size: 217.2 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.3.0-cp38-cp38-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 d6e8015643269f056cd090eb3bcd9922b0b6e334ba979e4e292b3e7f396a0c87
MD5 d75e96707a6da8a11b7c1ee67a3669b6
BLAKE2b-256 4d2c00486e9a2e1d263541633cc54d4a2bce2eb128c9dd1ec58633e68ceccd24

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