End-to-end machine learning on your desktop or server.
Project description
pre-alpha; in active development
Value Proposition
PyDataSci is an open source, automated machine learning (AutoML) tool for data scientists that reduces the amount of code needed to perform best practice machine learning by 95%; more science with less code.
It is a Python package that records experiments in a lightweight, file-based database that works on Mac/ Linux/ Windows without any configuration required by the user. By tracking the input (samples and settings) and output (models and metrics) of each experiment, it makes machine learning reproducible; less of a black box.
Users can either (a) queue many experiments on their desktop/ server, or (b) delegate them to run in the PyDataSci cloud if they outgrow their local resources. From there, model performance metrics can be visually compared in interactive charts. It is designed for use within Jupyter notebooks, but runs in any Python shell.
TLDR
$ pip install pydatasci
>>> import pydatasci as pds
>>> from pydatasci import aidb
Mission
-
Accelerating Research at Universities & Institutes Everywhere.
We empower non-cloud users: the academic/ institute HPCers, the private clouders, the remote server SSH'ers, and everyday desktop hackers - with the same quality ML tooling as present in public clouds (e.g. AWS SageMaker). This toolset provides research teams a standardized method for ML-based evidence, rather than each researcher spending time cobbling together their own approach. -
Reproducible Experiments.
No more black boxes. No more screenshotting loss-accuracy graphs and hyperparameter combinations. A record of every: dataset, feature, label, sample, split, fold, parameter, model, training job, and result - is automatically persisted in a lightweight, file-based database that is automatically configured when you import the package. Submit your aidb database file alongside your publications/ papers and model zoo entries as a proof. -
Queue Hypertuning Jobs.
Design a batch of runs to test many hypotheses at once. Queue many hypertuning jobs locally, or delegate big jobs to the cloud to run in parallel by settingcloud_queue = True. -
Visually Compare Performance Metrics.
Compare models using pre-defined plots for assessing performance, including: quantitative metrics (e.g. accuracy, loss, variance, precision, recall, etc.), training histories, and confusion/ contingency matrices. -
Code-Integrated & Agnostic.
We don’t disrupt the natural workflow of data scientists by forcing them into the confines of a GUI app or specific IDE. Instead, we weave automated tracking into their existing scripts so that PyDataSci is compatible with any data science toolset.
Functionality
Initially focusing on tabular data before expanding to multi-file use cases.
- [Done] Compress an immutable dataset (csv, tsv, parquet, pandas dataframe, numpy ndarray) to be analyzed.
- [Done] Split stratified samples by index while treating validation sets (3rd split) and cross-folds (k-fold) as first-level citizens.
- [Done] Generate hyperparameter combinations for model building, training, and evaluation.
- [Done] Preprocess samples to encode them for specific algorithms.
- [Done] Queue hypertuning jobs and batches based on hyperparameter combinations.
- [In progress] Evaluate and save the performance metrics of each model.
- [Next] Visually compare model metrics in Jupyter Notebooks with Plotly Dash to find the best one.
- [Next] Derive informative featuresets from that dataset using supervised and unsupervised methods.
- [Next] Behind the scenes, stream rows from your datasets and use generators to keep a low memory footprint.
- [Next] Scale out to run cloud jobs in parallel by toggling
cloud_queue = True.
Community
Much to automate there is. Simple it must be. ML is a broad space with a lot of challenges to solve. Let us know if you want to get involved. We plan to host monthly dev jam sessions and data science lightning talks.
- Data types to support: tabular, time series, image, graph, audio, video, gaming.
- Analysis types to support for each data type: classification, regression, dimensionality reduction, feature engineering, recurrent, generative, NLP, reinforcement.
Installation
Requires Python 3+ (check your deep learning library's Python requirements). You will only need to perform these steps the first time you use the package.
Enter the following commands one-by-one and follow any instructions returned by the command prompt to resolve errors should they arise.
Starting from the command line:
$ pip install --upgrade pydatasci
$ python
Once inside a Python shell:
>>> import pydatasci as pds
>>> pds.create_folder()
>>> pds.create_config()
>>> from pydatasci import aidb
>>> aidb.create_db()
PyDataSci makes use of the Python package,
appdirs, for an operating system (OS) agnostic location to store configuration and database files. This not only keeps your$HOMEdirectory clean, but also helps prevent careless users from deleting your database.The installation process checks not only that the corresponding appdirs folder exists on your system but also that you have the permissions neceessary to read from and write to that location. If these conditions are not met, then you will be provided instructions during the installation about how to create the folder and/ or grant yourself the appropriate permissions.
We have attempted to support both Windows (
icaclspermissions and backslashesC:\\) as well as POSIX including Mac and Linux including containers & Google Colab (chmod letterspermissions and slashes/). Note: due to variations in the ordering of appdirs author and app directories in different OS', we do not make use of the appdirsappauthordirectory, only theappnamedirectory.If you run into trouble with the installation process on your OS, please submit a GitHub issue so that we can attempt to resolve, document, and release a fix as quickly as possible.
Installation Location Based on OS
import appdirs; appdirs.user_data_dir('pydatasci');:
- Mac:
/Users/Username/Library/Application Support/pydatasci- Linux - Alpine and Ubuntu:
/root/.local/share/pydatasci- Windows:
C:\Users\Username\AppData\Local\pydatasci
create_db()is equivalent to a migration in Django or Rails in that it creates the tables found in the Object Relational Model (ORM). We use thepeeweeORM as it is simpler than SQLAlchemy, has good documentation, and found the project to be actively maintained (saw same-day GitHub response to issues on a Saturday). With the addition of Dash-Plotly, this will make for a full-stack experience that also works directly in an IDE like Jupyter or VS Code.
Deleting & Recreating the Database
When deleting the database, you need to either reload the aidb module or restart the Python shell before you can attempt to recreate the database.
>>> from pydatasci import aidb
>>> aidb.delete_db(True)
>>> from importlib import reload
>>> reload(aidb)
>>> create_db()
Usage
If you've already completed the Installation section above, let's get started.
1. Import the Library
import pydatasci as pds
from pydatasci import aidb
2. Ingest a Dataset as a compressed file.
Supported tabular file formats include CSV, TSV, Apache Parquet.
# From a file.
dataset = aidb.Dataset.from_file(
path = 'iris.tsv' # files must have column names as their first row
, file_format = 'tsv'
, name = 'tab-separated plants'
, perform_gzip = True
, dtype = 'float64' # or a dict or dtype by column name.
)
# From an in-memory data structure.
dataset = aidb.Dataset.from_pandas(
dataframe = df
, file_format = 'csv'
, name = 'comma-separated plants'
, perform_gzip = False
, dtype = None # None infers from dataframe provided
, rename_columns = None
)
dataset = aidb.Dataset.from_numpy(
ndarray = arr
, file_format = 'parquet'
, name = 'chunking plants'
, perform_gzip = True
, dtype = None # feeds pd.Dataframe(dtype)
, column_names = None # feeds pd.Dataframe(columns)
)
Apart from
read_numpy(), it's best if you provide your own column names ahead of time as the first row of your files and DataFrames that you want to ingest.
The bytes of the data will be stored as a BlobField in the SQLite database file. Storing the data in the database not only (a) provides an entity that we can use to keep track of experiments and link relational data to but also (b) makes the data less mutable than keeping it in the open filesystem.
You can choose whether or not you want to gzip compress the file when importing it with the
perform_gzip=boolparameter. This compression not only enables you to store up to 90% more data on your local machine, but also helps overcome the maximum BlobField size of 2.147 GB. We handle the zipping and unzipping on the fly for you, so you don't even notice it.
Optionally,
dtype, as seen inpandas.DataFrame.astype(dtype), can be specified as either a single type for all columns, or as a dict that maps a specific type to each column name. This encodes features for analysis. We read NumPy into Pandas before persisting it, socolumnsanddtypeare read directly bypd.DataFrame().
At this point, the project's support for Parquet is extremely minimal.
If you leave
nameblank, it will default to a human-readble timestamp with the appropriate file extension (e.g. '2020_10_13-01_28_13_PM.tsv').
Fetch a Dataset with either Pandas or NumPy.
Supported in-memory formats include NumPy Structured Array and Pandas DataFrame.
# Implicit IDs
df = dataset.to_pandas()
df.head()
arr = dataset.to_numpy()
arr[:4]
# Explicit IDs
df = aidb.Dataset.to_pandas(id=dataset.id)
df.head()
arr = aidb.Dataset.to_numpy(id=dataset.id)
arr[:4]
For the sake of simplicity, we are reading into NumPy via Pandas. That way, if we want to revert to a simpler ndarray in the future, then we won't have to rewrite the function to read NumPy.
3. Select the Label column(s).
From a Dataset, pick the column(s) that you want to train against/ predict. If you are planning on training an unsupervised model, then you don't need to do this.
# Implicit IDs
label_column = 'species'
label = dataset.make_label(columns=[label_column])
# Explicit IDs
label = aidb.Label.from_dataset(
dataset_id=dataset.id
, columns=[label_column]
)
Again, read a Label into memory with
to_pandas()andto_numpy()methods.
Labels accept multiple columns for situations like one-hot encoding (OHE).
4. Select the Featureset column(s).
Creating a Featureset won't duplicate your data! It simply records the columns to be used in training.
Here, we'll just exclude a Label column in preparation for supervised learning, but you can either exlcude or include any columns you see fit.
# Implicit IDs
featureset = dataset.make_featureset(exclude_columns=[label_column])
# Explicit IDs
featureset = aidb.Featureset.from_dataset(
dataset_id = dataset.id
, include_columns = None
, exclude_columns = [label_column]
)
>>> featureset.columns
['sepal length (cm)',
'sepal width (cm)',
'petal length (cm)',
'petal width (cm)']
>>> featureset.columns_excluded
['species']
Again, read a Featureset into memory with to_pandas() and to_numpy() methods.
The
include_columnsandexclude_columnsparameters are provided to expedite column extraction:
- If both
include_columns=Noneandexclude_columns=Nonethen all columns in the Dataset will be used.- If
exclude_columns=[...]is specified, then all other columns will be included.- If
include_columns=[...]is specified, then all other columns will be excluded.- Remember, these parameters accept [lists], not raw strings.
5. Generate splits of samples.
Divide the Dataset rows into Splitsets based on how you want to train, validate (optional), and test your models.
Again, creating a Splitset won't duplicate your data. It simply records the samples (aka rows) to be used in your train, validation, and test splits.
splitset_train70_test30 = featureset.make_splitset(label_id=label.id)
splitset_train70_test30 = featureset.make_splitset(
label_id = label.id
, size_test = 0.30
)
splitset_train68_val12_test20 = featureset.make_splitset(
label_id = label.id
, size_test = 0.20
, size_validation = 0.12
)
splitset_unsupervised = featureset.make_splitset()
Label-based stratification is used to ensure equally distributed label classes for both categorical and continuous data.
The
size_testandsize_validationparameters are provided to expedite splitting samples:
- If you leave
size_test=None, it will default to0.30when a Label is provided.- You cannot specify
size_validationwithout also specifyingsize_test. If you want more control over stratification of continuous features, then you can specify the number ofcontinuous_bin_countfor grouping.
Again, read a Splitset into memory with to_pandas() and to_numpy() methods. Note: this will return a dict of either data frames or arrays.
>>> splitset_train68_val12_test20.sizes
{
'train': {
'percent': 0.68,
'count': 102},
'validation': {
'percent': 0.12,
'count': 18},
'test': {
'percent': 0.2,
'count': 30}
}
>>> splitset_train68_val12_test20.to_numpy()
{
'train': {
'features': <ndarray>,
'labels': <ndarray>},
'validation': {
'features': <ndarray>,
'labels': <ndarray>},
'test': {
'features': <ndarray>,
'labels': <ndarray>}
}
6. Optionally, create Folds of samples for cross-fold validation.
Reference the scikit-learn documentation to learn more about folding.
As seen in the image above, different cuts through the training data are produced while the test data remains untouched. The training data is divided into stratified folds where one fold is left out each time, while the test data is left untouched.
We refer to the left out fold as the fold_validation and the remaining training data as the folds_train_combined. The samples of the validation fold are still recorded if you wanted to generate performance metrics against them.
In a scenario where a validation split was specified in the original Splitset, the validation split is also untouched. Only the training data is folded. The implication is that you can have 2 validations in the form of the validation split and the validation fold.
foldset = splitset_train68_val12_test20.make_foldset(fold_count=5)
#generates `foldset.fold[0:4]`
Again, read a Foldset into memory with to_pandas() and to_numpy() methods. Note: this will return a dict of either data frames or arrays.
7. Create an Algorithm aka model.
We'll use two variables in our functions: **hyperdata and **hyperparameters.
**hyperdatamakes training and evaluation samples available to your model. It knows whether or not we specified validation splits in your Splitset as well as how to handle Folds.**hyperparametersare simply variables that we specify with the values we want to experiment with.
Create a function to build the model.
Here you can see we specified the variables l1_neuron_count, l2_neuron_count, and optimizer. We also made an entire layer optional with a simple if statement on l2_exists!
In a moment, we will make a dictionary of these variables, and they will be fed in via the **hyperparameters parameter.
def function_model_build(**hyperparameters, **hyperdata):
training_size = train_features.shape[1]
model = Sequential()
model.add(Dense(l1_neuron_count, input_shape=(training_size,), activation='relu', kernel_initializer='he_uniform', name='fc1'))
if l2_exists is True:
model.add(Dense(l2_neuron_count, activation='relu', kernel_initializer='he_uniform', name='fc2'))
model.add(Dense(3, activation='softmax', name='output'))
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
return model
Create a function to train the model.
def function_model_train(**hyperparameters, **hyperdata):
history = model.fit(
train_features
, train_labels
, validation_data = (
eval_features
, eval_labels
)
, verbose = 1
, batch_size = batch_size
, epochs = epochs
)
return history
If you specified a validation split in your Splitset, then
eval_featuresandeval_labelswill point to your validation split, otherwise, it will point to your test split. If your model doesn't provide an evaluation history then you don't need to specify it.
Create a function to evaluate the model.
def function_model_evaluate(**hyperparameters, **hyperdata):
results = model.evaluate(eval_features, eval_features, verbose=0)
return results
Both validation and test splits will be fed through
eval_featuresandeval_labels.
Pull it all together in creating the Algorithm.
algorithm = aidb.Algorithm.create(
description = "dense with 1 or 2 layers"
, function_model_build = function_model_build
, function_model_train = function_model_train
, function_model_evaluate = function_model_evaluate
)
8. Optionally, create a Preprocess.
If you want to either encode, standardize, normalize, or scale you Features and/ or Labels - then you can make use of sklearn.preprocessing methods.
# refactoring this
preprocess = aidb.Preprocess.from_splitset(
splitset_id = splitset_id
, description = "standard scaling on features"
, encoder_features = encoder_features
, encoder_labels = encoder_labels
)
9. Optionally, create combinations of Hyperparamsets for our model.
Remember those variables we specified in our model functions? Here we provide values for them, which will be used in **hyperparameters.
In the future, we will provide different strategies for generating and selecting parameters to experiment with.
hyperparameter_lists = {
"l1_neuron_count": [9, 18]
, "l2_exists": [True, False]
, "l2_neuron_count": [9, 18]
, "optimizer": ["adamax", "adam"]
, "epochs": [30, 60, 90]
, "batch_size": [3, 5]
}
hyperparamset = aidb.Hyperparamset.from_algorithm(
algorithm_id = algorithm.id
, preprocess_id = preprocess.id
, description = "experimenting with neuron count, layers, and epoch count"
, hyperparameter_lists = hyperparameter_lists
)
10. Create a Batch of Jobs to keep track of training.
batch = aidb.Batch.from_algorithm(
algorithm_id = algorithm.id
, splitset_id = splitset.id
, hyperparamset_id = hyperparamset.id
, foldset_id = foldset.id
, only_folded_training = False
)
When you are ready, run the Jobs.
The jobs will be asynchronously executed on a background thread, so that you can continue to code on the main thread. You can poll the job status.
batch.run_jobs()
batch.get_statuses()
You can stop the execution of a batch if you need to, and later resume it. If your kernel crashes then you can likewise resume the execution.
batch.stop_jobs()
batch.run_jobs()
11. Examine Results.
Artifacts like the model object, training history, and performance metrics are automatically be written to Job.results.
compiled_model = batch.jobs[0].results[0].get_model()
compiled_model
<tensorflow.python.keras.engine.sequential.Sequential at 0x14d4f4310>
12. Visually compare the performance of your hypertuned Algorithms.
PyPI Package
Steps to Build & Upload
$ pyenv activate pydatasci
$ pip3 install --upgrade wheel twine
$ python3 setup.py sdist bdist_wheel
$ python3 -m twine upload --repository pypi dist/*
$ rm -r build dist pydatasci.egg-info
# proactively update the version number in setup.py next time
$ pip install --upgrade pydatasci; pip install --upgrade pydatasci
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydatasci-0.0.53.tar.gz.
File metadata
- Download URL: pydatasci-0.0.53.tar.gz
- Upload date:
- Size: 33.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b42b6d19cbaff7529bce826f8f49c7f512b4a5ddae2dc4511d1744bf6444e4ff
|
|
| MD5 |
80e03109fb8c6e661860afb62d4bde25
|
|
| BLAKE2b-256 |
596651e24e4e3dfb4c8de7b834b44225c55acb9cac1dd73b4466d02f1736a1a9
|
File details
Details for the file pydatasci-0.0.53-py3-none-any.whl.
File metadata
- Download URL: pydatasci-0.0.53-py3-none-any.whl
- Upload date:
- Size: 37.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac14858b2efc5cf52d811d2a108d9403de2b64a41d103bb9433e66a23dc0a8c4
|
|
| MD5 |
7d6a409b0987f115a8178ddc4079fbc4
|
|
| BLAKE2b-256 |
9347e9991a4ccb36f862577d4c93525c5059bcccfdd7d115209196cf488d28f2
|