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.1.5.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.1.5-cp312-cp312-win_amd64.whl (10.5 kB view details)

Uploaded CPython 3.12Windows x86-64

hapc-0.1.5-cp312-cp312-macosx_10_13_universal2.whl (10.4 kB view details)

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

hapc-0.1.5-cp311-cp311-win_amd64.whl (10.5 kB view details)

Uploaded CPython 3.11Windows x86-64

hapc-0.1.5-cp311-cp311-macosx_10_9_universal2.whl (10.4 kB view details)

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

hapc-0.1.5-cp310-cp310-win_amd64.whl (10.5 kB view details)

Uploaded CPython 3.10Windows x86-64

hapc-0.1.5-cp310-cp310-macosx_10_9_universal2.whl (10.4 kB view details)

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

hapc-0.1.5-cp39-cp39-win_amd64.whl (10.5 kB view details)

Uploaded CPython 3.9Windows x86-64

hapc-0.1.5-cp39-cp39-macosx_10_9_universal2.whl (10.4 kB view details)

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

hapc-0.1.5-cp38-cp38-win_amd64.whl (10.4 kB view details)

Uploaded CPython 3.8Windows x86-64

hapc-0.1.5-cp38-cp38-macosx_11_0_universal2.whl (10.3 kB view details)

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

File details

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

File metadata

  • Download URL: hapc-0.1.5.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.1.5.tar.gz
Algorithm Hash digest
SHA256 8a2e59e64a23e6d1083976ab7330305062d7ee6a0102779b4c22b3ce0f4ef846
MD5 62fd1ee00bfc05b862f0605ad740047a
BLAKE2b-256 e1cb401c73406d15240ca0c7a3310a5415ca004a0b6d3dc40340cbb9aacb9cb2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-0.1.5-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 10.5 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.1.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6cc60e1cb345cfe57c2297aff25442b38db5b52a2a0ea3671a6cb73fc1910d86
MD5 cbf9a9f09b8a479b853cd4ddfe67766a
BLAKE2b-256 5a9187805f9c7d995e3fff0bc39a66717e853ed79f611410329f3ed95d3a0c13

See more details on using hashes here.

File details

Details for the file hapc-0.1.5-cp312-cp312-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for hapc-0.1.5-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 6aa1d4294aeef828c398f63d5c60d215a3e70e5942cf02e55d71d838fb0a5851
MD5 4ae1f5bb464ebc794a8a8dffb52c614c
BLAKE2b-256 070c83e250f0f5313f1f8d45f2b5c812db7761bf0f60304f10c1586e54951e90

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-0.1.5-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 10.5 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.1.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a690e0d804fff54905e59d9c3e90423c7b89dd458671967cc02fa296dcbf03ce
MD5 3b9fba13b372ba5d664132cbb55e1534
BLAKE2b-256 2a918b57ab371398f61b92313c331865c77be7015b304c39638234f5fe27b425

See more details on using hashes here.

File details

Details for the file hapc-0.1.5-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for hapc-0.1.5-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 f4cbb608a29c8954f98c8e50ccc218906d51397a200e12ae4de744e2992e8dd4
MD5 365154b9c3a4bfbaefa25fc6d63d87bb
BLAKE2b-256 20bea1c39822550768c9a87724b3a4c81e92259d880c6f37e4fd1c114c33028a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-0.1.5-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 10.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.1.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 cb3459042495a057c60f7f4c20d7185060376938b32a4f88c795c099360c176b
MD5 0dbf237c01d17fc717cc7c8c5f8c17e3
BLAKE2b-256 0cb99ddf19eacb56851f8b7718c8b6582005d4da7912e0b3101cdfc0e12e35ac

See more details on using hashes here.

File details

Details for the file hapc-0.1.5-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for hapc-0.1.5-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 361a5a98a251582e55a0541c2959c1fc626b17ebbb727edbbab881207c2370c4
MD5 23c2ef6acc5a0e59bfe2923211d0481f
BLAKE2b-256 8a0ebd1a47cac9076f6335e9ccae798c6b6027a587d2b86ce8f7ca250514de86

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-0.1.5-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 10.5 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.1.5-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 4c136392b554a027f378dfc49b1c82367cdbe2f8febbe98dfad659e4c9013aa5
MD5 4ec87969013a47c308fe19f16f1b20ad
BLAKE2b-256 5993b8aaf3b17474bd9a966f8fe553645763a2ea192542fc693068e020d06ce5

See more details on using hashes here.

File details

Details for the file hapc-0.1.5-cp39-cp39-macosx_10_9_universal2.whl.

File metadata

  • Download URL: hapc-0.1.5-cp39-cp39-macosx_10_9_universal2.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: CPython 3.9, macOS 10.9+ 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.1.5-cp39-cp39-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 811f3cd1e754dc8ec5bef0c25af6b28c4d0dd8bcdb94adda37b67baba5e3e75f
MD5 1ee79356a971245b5de3c0dff900a8d7
BLAKE2b-256 4029fdbbc73204089f45116a09a856f0ba18a3a3274c16a3ed0ce281c508ea44

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-0.1.5-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 10.4 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.1.5-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 046a94487a55a77deb6970da6c1281ae7f389b38025e39eeec1bf197d8e32430
MD5 173bae00e08089996930781e0b9dbffc
BLAKE2b-256 12092e9a74f73c4087bd94b57ef2688d2189dd2799e1ccdf3cf921db7a0884ff

See more details on using hashes here.

File details

Details for the file hapc-0.1.5-cp38-cp38-macosx_11_0_universal2.whl.

File metadata

  • Download URL: hapc-0.1.5-cp38-cp38-macosx_11_0_universal2.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: CPython 3.8, macOS 11.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.1.5-cp38-cp38-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 afbfef295afb69ad0c51e89bef958b21b4a0955d0f311c85b49c034808cb52d4
MD5 3300aab30f7490112259c63e1836ce38
BLAKE2b-256 fc06dc0a15ff7e8a9be413b4fbfc3368077a8c084c0e76157c87d59fbbeddc0e

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