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.0.tar.gz (5.9 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.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for boyd_ml_utils-0.4.0.tar.gz
Algorithm Hash digest
SHA256 b7f0118d4ed195dd208043da7f02076619631f23cc4dd518fb8791424be5db06
MD5 40bbb15037ffe95808db51e6c952e81a
BLAKE2b-256 0ef63dcb48ef245f39c3bf71e628621813ff716ec776fab4adf6613d98035e85

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for boyd_ml_utils-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8b8e04ff2f54983f8fdabf1179f1468638ce5f65360d520a281b097f240f283e
MD5 f409044a9f7668069657a97c375628ff
BLAKE2b-256 d4800c457155420db9adc1c5e553f4cc5ecb108ada21e4406a295089423c685f

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