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.0.2.tar.gz (58.1 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.0.2-cp312-cp312-win_amd64.whl (503.9 kB view details)

Uploaded CPython 3.12Windows x86-64

hapc-2.0.2-cp312-cp312-macosx_15_0_universal2.whl (213.9 kB view details)

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

hapc-2.0.2-cp311-cp311-win_amd64.whl (501.0 kB view details)

Uploaded CPython 3.11Windows x86-64

hapc-2.0.2-cp311-cp311-macosx_15_0_universal2.whl (213.0 kB view details)

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

hapc-2.0.2-cp310-cp310-win_amd64.whl (499.0 kB view details)

Uploaded CPython 3.10Windows x86-64

hapc-2.0.2-cp310-cp310-macosx_15_0_universal2.whl (212.2 kB view details)

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

hapc-2.0.2-cp39-cp39-win_amd64.whl (504.9 kB view details)

Uploaded CPython 3.9Windows x86-64

hapc-2.0.2-cp39-cp39-macosx_15_0_universal2.whl (212.2 kB view details)

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

hapc-2.0.2-cp38-cp38-win_amd64.whl (498.6 kB view details)

Uploaded CPython 3.8Windows x86-64

hapc-2.0.2-cp38-cp38-macosx_15_0_universal2.whl (212.0 kB view details)

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

File details

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

File metadata

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

File hashes

Hashes for hapc-2.0.2.tar.gz
Algorithm Hash digest
SHA256 aba9909a6920bda07544947407c966ec5bda98e548e61daf6a60630bb25af58f
MD5 8e69e7c20a12079f9c4a57dba2169801
BLAKE2b-256 82e26111272dbbe743ae9f0c725154f442b7fba86c3a7b40a0782e6ed8431df7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.0.2-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 503.9 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.0.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d81452f9bdadb8d97caf3c355cd2484f45931809b6fe6d4b0c8d2ef4001f5301
MD5 2d9b5d56358cbd46fcf6b2e774ede1de
BLAKE2b-256 27d3dc24361ed1e0449b71670810aa4ba1d96c5a58a0340da3f910283d06f82e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.0.2-cp312-cp312-macosx_15_0_universal2.whl
  • Upload date:
  • Size: 213.9 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.13

File hashes

Hashes for hapc-2.0.2-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 7c314a5295e75c6aa47c6515d865efd5ba5fd8bf1e63e6080ca29b1a3e4f648f
MD5 05be20786a94a5af22143280e7340e98
BLAKE2b-256 d15a34dd98e40faead5b0d6231651c9299c36de703ac37c68ff0975d0e533696

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.0.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 501.0 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.0.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2aa587ac59027ddfe85139adc586151ddfd641ff99195c9f53e7005299519ae1
MD5 57080461f6aa1eca1777a3967c7ded35
BLAKE2b-256 bf042d4ddf2e834020163be1d5d891f9d89ed34f2b4ee4a07b6881bbdfe54930

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.0.2-cp311-cp311-macosx_15_0_universal2.whl
  • Upload date:
  • Size: 213.0 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.13

File hashes

Hashes for hapc-2.0.2-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 cae1f996291f0eb5ebdedb05ba96b68d788b9da4b180addf088d87891de70e37
MD5 259e7eab80cce6b84b062c71f661e3a1
BLAKE2b-256 c74f5881fe880b6b1fcb5acb83ab50c3599a9851ad585f08b2e1ff1e09603cf6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.0.2-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 499.0 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.0.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 01e0a46f252521f7982db78a00dcaeb4f51adc38c5840ea8158e3a5acdfb887c
MD5 94e94c1c3bca820f10a24a9a8ba2721a
BLAKE2b-256 b2209c0d2dd4e8438fba66a8d4f8b9145010a108cdf9fa1b73eca9c30dbd57a8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.0.2-cp310-cp310-macosx_15_0_universal2.whl
  • Upload date:
  • Size: 212.2 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.13

File hashes

Hashes for hapc-2.0.2-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 6c1bfbcc22f3a003e0c370a87012737a90b8509716b158d95fba9648c2f1f6d6
MD5 d952249dc4b3bfa926b6c0ef89286f88
BLAKE2b-256 9dca147facd1bab1c68ba29c2c52dc519ac34dabc897cffec78330c7a6947638

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.0.2-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 504.9 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.0.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 a7966fc09f46062290a3603bda9bb2551dc20d4386afa472c7b1d503b0a8cf6b
MD5 e1d846665d328f1f35c83378add10a33
BLAKE2b-256 6e3afa27cdd1c9ca177ec749c3d32f11fc4777bde1240b59922533f37ab00988

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.0.2-cp39-cp39-macosx_15_0_universal2.whl
  • Upload date:
  • Size: 212.2 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.0.2-cp39-cp39-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 271743b1b3a97262b722a86b3604bca09eed1c34feb1b6a6f7f618442e6f9d86
MD5 a71cce6526e2efc340487349de5313a9
BLAKE2b-256 f33cd4492a8506a59246b0e0413f93f3ff66f52b743affcc8c2fc3fa3785e2c8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.0.2-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 498.6 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.0.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 58b4c2c28736114c72dd97d327143cb040feab5ad6e894b8f9ebb79bfbbdb5a3
MD5 c68f36144d47921d72e08a2f52450b9a
BLAKE2b-256 69e70b96e35873037290254d5fcc540045bf6dc2892009592ffc8f6f5f6c43c0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hapc-2.0.2-cp38-cp38-macosx_15_0_universal2.whl
  • Upload date:
  • Size: 212.0 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.0.2-cp38-cp38-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 59c449fb9d7a43a1ed2995479b34123b02a0a82050140a7afabd76aa81655a6a
MD5 ba8fd53afda7489cac761b79a43437b6
BLAKE2b-256 41cd0c9c99304626edf6bfa9ef4d227a665913b3cae09d8f2fa56a94f75b1051

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