Skip to main content

A framework to ease the burden of organizing code of a supervised machine learning system.

It provides decorators that manage data & pass it between common steps in building a machine learning system, such as: - loading the dataset - preprocessing - feature generation - model definition

While doing this, it keeps the global namespace free of clutter such as that from an endless chain of features and models.

In addition, it makes it easy to put new, real life, data through the exact same process that training data goes through.

Installation

Install simply via pip (Python 3):

$ pip install stickbugml

Dependencies: - Python 3 - sklearn - pandas - numpy

Example

Note: there is also a great example for use in Jupyter Notebooks

First, import this library:

import stickbugml
from stickbugml.decorators import dataset, feature, model

Load your dataset:

import seaborn.apionly as sns
import pandas as pd

@dataset(train_valid_test=(0.6, 0.2, 0.2)) # define your train/test/validation data splits
def raw_dataset():
    titanic_dataset = sns.load_dataset('titanic')

    # Drop NaN rows for simplicity
    titanic_dataset.dropna(inplace=True)

    # Extract X and y
    X = titanic_dataset.drop('survived', axis=1)
    y = titanic_dataset['survived']
    return X, y

print(raw_dataset.head()) # yes, this does work! raw_dataset is now a pandas DataFrame

(Optionally) do some pre-processing:

@preprocess
def preprocessed_dataset(X):
    # Encode categorical columns
    categorical_column_names = [
            'sex', 'embarked', 'class',
            'who', 'adult_male', 'deck',
            'embark_town', 'alive', 'alone']

    X = pd.get_dummies(X,
                       columns=categorical_column_names,
                       prefix=categorical_column_names)

    return X

print(preprocessed_dataset.head()) # See the first code block for explaination

Generate some features:

from sklearn import decomposition
import numpy as np

@feature('pca')
def pca_feature(X):
    pca = decomposition.PCA(n_components=3)
    pca.fit(X)
    pca_out = pca.transform(X)

    pca_out = np.transpose(pca_out, (1, 0))
    return pd.DataFrame(pca_out)

# let's preview
print(pca_feature.head()) # See the first code block for explaination

# you can add more features, btw

And define your (machine learning) model(s):

import xgboost as xgb

@model('xgboost')
def xgboost_model():
    def define(num_columns):
        return None # xgboost models aren't pre-defined


    def train(model, params, train, validation):
        params['objective'] = 'binary:logistic' # Static parameters can be defined here
        params['eval_metric'] = 'logloss'

        d_train = xgb.DMatrix(train['X'], label=train['y'])
        d_valid = xgb.DMatrix(validation['X'], label=validation['y'])

        watchlist = [(d_train, 'train'), (d_valid, 'valid')]

        trained_model = xgb.train(params, d_train, 2000, watchlist, early_stopping_rounds=50, verbose_eval=10)

        return trained_model

    def predict(model, X):
        return model.predict(xgb.DMatrix(X))

    return define, train, predict

Now you can train your model, trying out different parameters if you want:

stickbugml.train('xgboost', {
    'max_depth': 7,
    'eta': 0.01
})

The library keeps the test data’s ground truth values locked away so your models won’t train on it. After you train your model, have the framework evaluate it for you:

logloss_score = stickbugml.evaluate('xgboost')
print(logloss_score)

You can add more models and features if so desired.

Since this library is built with reality in mind, you can easily get predictions for new/real-life data:

raw_X = pd.read_csv('2018_titanic_manifesto.csv') # It will probably sink, but we don't know who will survive
processed_X = stickbugml.process(raw_X) # Process the data
del raw_X # Gotta keep that namespace clean, right?

y = stickbugml.predict('xgboost', processed_X) # Make predictions

print(y)

Contributing & Feedback

If you have any problems, or would like a new feature, submit an Issue.

If you want to help out, feel free to submit a Pull Request.

License

This project uses the Apache 2.0 License

Release files for stickbugml 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stickbugml 1.0.4
File Size Uploaded
stickbugml-1.0.4.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for stickbugml 1.0.4
File Interpreter ABI Platform
stickbugml-1.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 18.4 kB

Release files / stickbugml-1.0.4.tar.gz

Download URL stickbugml-1.0.4.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
02b9525b7304d470841943886c9469aba6e672c7d690f7648f3af685f36e8f51
BLAKE2b-256 checksum
How to use checksums
bbc26683f8588c755f50c22471d91006478f05153518ad5c367c2345f6432e59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / stickbugml-1.0.4-py3-none-any.whl

Download URL stickbugml-1.0.4-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
32ada768dac15ac3765a6a2e5e00dc513f3c8ad194b3b619ee3c4df6852438f4
BLAKE2b-256 checksum
How to use checksums
b02fd56affa33dbc9b158fc2a48dd25ddef41bb0375eb01c023259db4e1a320c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page