Skip to main content

machine learning utilities by boy-dad

Project description

boyd-ml-utils

Overview

I just want to try publishing a package in PyPI. I'll add unit tests, more functionalities and GitHub pages in the future.

Sample usage

OptunaSearchCV works like RandomizedSearchCV in scikit-learn but the parameter distributions must be specified using distribution classes in optuna.

import pandas as pd
from boyd_ml_utils.model_selection import OptunaSearchCV
from optuna.distributions import IntDistribution
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split

# Load the iris dataset
iris = load_iris()
X = pd.DataFrame(iris.data, columns=iris.feature_names)
y = X.pop("petal width (cm)")  # Treat petal width as the target variable

# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Define a Random Forest Regressor model
rf = RandomForestRegressor(random_state=42)

# Define hyperparameter space for RandomizedSearchCV
param_dist = {
    "n_estimators": IntDistribution(low=10, high=200),
    "max_depth": IntDistribution(low=3, high=20),
    "min_samples_split": IntDistribution(low=2, high=10),
    "min_samples_leaf": IntDistribution(low=1, high=5),
}

# Perform Randomized Search
random_search = OptunaSearchCV(
    estimator=rf,
    param_distributions=param_dist,
    n_iter=100,
    scoring="neg_mean_squared_error",
    cv=5,
    n_jobs=5,
)

random_search.fit(X_train, y_train)

# Best parameters and model evaluation
best_model = random_search.best_estimator_
y_pred = best_model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)

print("Best Parameters:", random_search.best_params_)
print("Mean Squared Error on Test Data:", mse)

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

boyd_ml_utils-0.4.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

boyd_ml_utils-0.4.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file boyd_ml_utils-0.4.1.tar.gz.

File metadata

  • Download URL: boyd_ml_utils-0.4.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.3

File hashes

Hashes for boyd_ml_utils-0.4.1.tar.gz
Algorithm Hash digest
SHA256 a88fbc1eb73c7972148c3245da80a2b2119257411799f8eb528b919cb4ffbe71
MD5 652da189eba37fc4c88aaa664f2462eb
BLAKE2b-256 0e97165de64302b530978bd20e073d3000b56518882ed27cbd19d144aff26e60

See more details on using hashes here.

File details

Details for the file boyd_ml_utils-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for boyd_ml_utils-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ec335c13306cb4db9160de3eff9eb9627651891ee4d18f49c5f8bde879aabf68
MD5 b9d463b2fc7abd50aad6efc7bd19dedc
BLAKE2b-256 55ff39b712c8d47799e99771cbe8117410762ab4014b91a5c6a34474abb723e8

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