Package: mlprimitives
An Open Source Project from theData to AI Lab, at MITMLPrimitivesPipelines and primitives for machine learning and data science.Documentation:https://MLBazaar.github.io/MLPrimitivesGithub:https://github.com/MLBazaar/MLPrimitivesLicense:MITDevelopment Status:Pre-AlphaOverviewThis repository contains primitive annotations to be used by the MLBlocks library, as well as
the necessary Python code to make some of them fully compatible with the MLBlocks API requirements.There is also a collection of custom primitives contributed directly to this library, which either
combine third party tools or implement new functionalities from scratch.Why did we create this library?Too many libraries in a fast growing fieldHuge societal need to build machine learning appsDomain expertise resides at several places (knowledge of math)No documented information about hyperparameters, behavior...InstallationRequirementsMLPrimitiveshas been developed and tested onPython 3.6, 3.7 and 3.8Also, although it is not strictly required, the usage of avirtualenvis highly recommended in order to avoid
interfering with other software installed in the system whereMLPrimitivesis run.Install with pipThe easiest and recommended way to installMLPrimitivesis usingpip:pipinstallmlprimitivesThis will pull and install the latest stable release fromPyPi.If you want to install from source or contribute to the project please read theContributing Guide.QuickstartThis section is a short series of tutorials to help you getting started with MLPrimitives.In the following steps you will learn how to load and run a primitive on some data.Later on you will learn how to evaluate and improve the performance of a primitive by tuning
its hyperparameters.Running a PrimitiveIn this first tutorial, we will be executing a single primitive for data transformation.1. Load a PrimitiveThe first step in order to run a primitive is to load it.This will be done using themlprimitives.load_primitivefunction, which will
load the indicated primitive as anMLBlock Object from MLBlocksIn this case, we will load themlprimitives.custom.feature_extraction.CategoricalEncoderprimitive.frommlprimitivesimportload_primitiveprimitive=load_primitive('mlprimitives.custom.feature_extraction.CategoricalEncoder')2. Load some dataThe CategoricalEncoder is a transformation primitive which applies one-hot encoding to all the
categorical columns of apandas.DataFrame.So, in order to be able to run our primitive, we will first load some data that contains
categorical columns.This can be done with themlprimitives.datasets.load_censusfunction:frommlprimitives.datasetsimportload_censusdataset=load_census()This dataset object has an attributedatawhich contains a table with several categorical
columns.We can have a look at this table by executingdataset.data.head(), which will return a
table like this:0                    1                   2
age                         39                   50                  38
workclass            State-gov     Self-emp-not-inc             Private
fnlwgt                   77516                83311              215646
education            Bachelors            Bachelors             HS-grad
education-num               13                   13                   9
marital-status   Never-married   Married-civ-spouse            Divorced
occupation        Adm-clerical      Exec-managerial   Handlers-cleaners
relationship     Not-in-family              Husband       Not-in-family
race                     White                White               White
sex                       Male                 Male                Male
capital-gain              2174                    0                   0
capital-loss                 0                    0                   0
hours-per-week              40                   13                  40
native-country   United-States        United-States       United-States3. Fit the primitiveIn order to run our pipeline, we first need to fit it.This is the process where it analyzes the data to detect which columns are categoricalThis is done by calling itsfitmethod and assing thedataset.dataasX.primitive.fit(X=dataset.data)4. Produce resultsOnce the pipeline is fit, we can process the data by calling theproducemethod of the
primitive instance and passing agin thedataasX.transformed=primitive.produce(X=dataset.data)After this is done, we can see how the transformed data contains the newly generated
one-hot vectors:0      1       2       3       4
age                                            39     50      38      53      28
fnlwgt                                      77516  83311  215646  234721  338409
education-num                                  13     13       9       7      13
capital-gain                                 2174      0       0       0       0
capital-loss                                    0      0       0       0       0
hours-per-week                                 40     13      40      40      40
workclass= Private                              0      0       1       1       1
workclass= Self-emp-not-inc                     0      1       0       0       0
workclass= Local-gov                            0      0       0       0       0
workclass= ?                                    0      0       0       0       0
workclass= State-gov                            1      0       0       0       0
workclass= Self-emp-inc                         0      0       0       0       0
...                                             ...    ...     ...     ...     ...Tuning a PrimitiveIn this short tutorial we will teach you how to evaluate the performance of a primitive
and improve its performance by modifying its hyperparameters.To do so, we will load a primitive that can learn from the transformed data that we just
generated and later on make predictions based on new data.1. Load another primitiveFirs of all, we will load thexgboost.XGBClassifierprimitive that we will use afterwards.primitive=load_primitive('xgboost.XGBClassifier')2. Split the datasetBefore being able to evaluate the primitive perfomance, we need to split the data in two
parts: train, which will be used for the primitive to learn, and test, which will be used
to make the predictions that later on will be evaluated.In order to do this, we will get the first 75% of rows from the transformed data that we
obtained above and call itX_train, and then set the next 25% of rows asX_test.train_size=int(len(transformed)*0.75)X_train=transformed.iloc[:train_size]X_test=transformed.iloc[train_size:]Similarly, we need to obtain they_trainandy_testvariables containing the corresponding
output values.y_train=dataset.target[:train_size]y_test=dataset.target[train_size:]3. Fit the new primitiveOnce we have have splitted the data, we can fit the primitive by passingX_trainandy_trainto itsfitmethod.primitive.fit(X=X_train,y=y_train)4. Make predictionsOnce the primitive has been fitted, we can produce predictions using theX_testdata as input.predictions=primitive.produce(X=X_test)5. Evalute the performanceWe can now evaluate how good the predictions from our primitive are by using thescoremethod from thedatasetobject on both the expected output and the real output from the
primitive:dataset.score(y_test,predictions)This will output a float value between 0 and 1 indicating how good the predicitons are, being
0 the worst score possible and 1 the best one.In this case we will obtain a score around 0.8666. Set new hyperparameter valuesIn order to improve the performance of our primitive we will try to modify a couple of its
hyperparameters.First we will see which hyperparameter values the primitive has by calling itsget_hyperparametersmethod.primitive.get_hyperparameters()which will return a dictionary like this:{"n_jobs":-1,"n_estimators":100,"max_depth":3,"learning_rate":0.1,"gamma":0,"min_child_weight":1}Next, we will see which are the valid values for each one of those hyperparameters by calling itsget_tunable_hyperparametersmethod:primitive.get_tunable_hyperparameters()For example, we will see that themax_depthhyperparameter has the following specification:{"type":"int","default":3,"range":[3,10]}Next, we will choose a valid value, for example 7, and set it into the pipeline using theset_hyperparametersmethod:primitive.set_hyperparameters({'max_depth':7})7. Re-evaluate the performanceOnce the new hyperparameter value has been set, we repeat the fit/train/score cycle to
evaluate the performance of this new hyperparameter value:primitive.fit(X=X_train,y=y_train)predictions=primitive.produce(X=X_test)dataset.score(y_test,predictions)This time we should see that the performance has improved to a value around 0.724What's Next?Do you want tolearn more about how the project,
abouthow to contribute to itor browse theAPI Reference?
Please check the corresponding sections of thedocumentation!History0.3.5 - 2023-04-14General ImporvementsUpdatemlblockscap -Issue #278by @sarahmish0.3.4 - 2023-01-24General ImporvementsUpdatemlblockscap -Issue #277by @sarahmish0.3.3 - 2023-01-20General ImporvementsUpdate dependencies -Issue #276by @sarahmishAdapter ImprovementsBuilding model within fit in keras adapter-Issue #267by @sarahmish0.3.2 - 2021-11-09Adapter ImprovementsInferring data shapes with single dimension for keras adapter -Issue #265by @sarahmish0.3.1 - 2021-10-07Adapter ImprovementsDynamic target_shape in keras adapter -Issue #263by @sarahmishSave keras primitives in Windows environment -Issue #261by @sarahmishGeneral ImporvementsUpdate TensorFlow and NumPy dependency -Issue #259by @sarahmish0.3.0 - 2021-01-09New PrimitivesAdd primitivesklearn.naive_bayes.GaussianNB-Issue #242by @sarahmishAdd primitivesklearn.linear_model.SGDClassifier-Issue #241by @sarahmishPrimitive ImprovementsAdd offset to rolling_window_sequence primitive -Issue #251by @skyeeiskowitzRename the time_index column to time -Issue #252by @pvk-developerUpdate featuretools dependency -Issue #250by @pvk-developerGeneral ImprovementsUdpate dependencies and add python3.8 -Issue #246by @csalaDrop Python35 -Issue #244by @csala0.2.5 - 2020-07-29Primitive ImprovementsAccept timedeltawindow_sizeincutoff_window_sequences-Issue #239by @joanvaquerBug FixesImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow viapip install tensorflow-Issue #237by @joanvaquerNew PrimitivesAddpandas.DataFrame.set_indexprimitive -Issue #222by @JDTheRipperPC0.2.4 - 2020-01-30New PrimitivesAdd RangeScaler and RangeUnscaler primitives -Issue #232by @csalaPrimitive ImprovementsExtract input_shape from X in keras.Sequential -Issue #223by @csalaBug Fixesmlprimitives.custom.text.TextCleaner fails if text is empty -Issue #228by @csalaError when loading the reviews dataset -Issue #230by @csalaCurate dependencies: specify an explicit prompt-toolkit version range -Issue #224by @csala0.2.3 - 2019-11-14New PrimitivesAdd primitive to make window_sequences based on cutoff times -Issue #217by @csalaCreate a keras LSTM based TimeSeriesClassifier primitive -Issue #218by @csalaAdd pandas DataFrame primitives -Issue #214by @csalaAdd featuretools.EntitySet.normalize_entity primitive -Issue #209by @csalaPrimitive ImprovementsMake featuretools.EntitySet.entity_from_dataframe entityset arg optional -Issue #208by @csalaAdd text regression dataset -Issue #206by @csalaBug Fixespandas.DataFrame.resample crash when grouping by integer columns -Issue #211by @csala0.2.2 - 2019-10-08New PrimitivesAdd primitives for GAN based time-series anomaly detection -Issue #200by @AlexanderGeigerAddnumpy.reshapeandnumpy.ravelprimitives -Issue #197by @AlexanderGeigerAdd feature selection primitive based on Lasso -Issue #194by @csalaPrimitive Improvementsfeature_extraction.CategoricalEncodersupport dtype category -Issue #196by @csala0.2.1 - 2019-09-09New PrimitivesTimeseries Intervals to Mask Primitive -Issue #186by @AlexanderGeigerAdd new primitive: Arima model -Issue #168by @AlexanderGeigerPrimitive ImprovementsCurate PCA primitive hyperparameters -Issue #190by @AlexanderGeigerAdd option to drop rolling window sequences -Issue #186by @AlexanderGeigerBug Fixesscikit-image==0.14.3 crashes when installed on Mac -Issue #188by @csala0.2.0New FeaturesPublish the pipelines as anentry_pointIssue #175by @csalaPrimitive ImprovementsImprove pandas.DataFrame.resample primitiveIssue #177by @csalaImprovefeature_extractorprimitivesIssue #183by @csalaImprovefind_anomaliesprimitiveIssue #180by @AlexanderGeigerBug FixesTypo in the primitive keras.Sequential.LSTMTimeSeriesRegressorIssue #176by @DanielCalvoCerezo0.1.10New FeaturesAdd function to run primitives without a pipelineIssue #43by @csalaNew PipelinesAdd pipelines for all the MLBlocks examplesIssue #162by @csalaPrimitive ImprovementsAdd Early Stopping tokeras.Sequential.LSTMTimeSeriesRegressorprimitiveIssue #156by @csalaMake FeatureExtractor primitives accept Numpy arraysIssue #165by @csalaAdd window size and pruning to thetimeseries_anomalies.find_anomaliesprimitiveIssue #160by @csala0.1.9New FeaturesAdd a single table binary classification datasetIssue #141by @csalaNew PrimitivesAdd Multilayer Perceptron (MLP) primitive for binary classificationIssue #140by @Hector-hedb12Add primitive for Sequence classification with LSTMIssue #150by @Hector-hedb12Add VGG-like convnet primitiveIssue #149by @Hector-hedb12Add Multilayer Perceptron (MLP) primitive for multi-class softmax classificationIssue #139by @Hector-hedb12Add primitive to count feature matrix columnsIssue #146by @csalaPrimitive ImprovementsAdd additional fit and predict arguments to keras.SequentialIssue #161by @csalaAdd suport for keras.Sequential CallbacksIssue #159by @csalaAdd fixed hyperparam to control keras.Sequential verbosityIssue #143by @csala0.1.8New Primitivesmlprimitives.custom.timeseries_preprocessing.time_segments_average -Issue #137New FeaturesAdd target_index output in timseries_preprocessing.rolling_window_sequences -Issue #1360.1.7General ImprovementsValidate JSON format inmake lint-Issue #133Add demo datasets -Issue #131Improve featuretools.dfs primitive -Issue #127New Primitivespandas.DataFrame.resample -Issue #123pandas.DataFrame.unstack -Issue #124featuretools.EntitySet.add_relationship -Issue #126featuretools.EntitySet.entity_from_dataframe -Issue #126Bug FixesBug in timeseries_anomalies.py -Issue #1190.1.6General ImprovementsAdd Contributing DocumentationRemove upper bound in pandas version given new release offeaturetoolsv0.6.1Improve LSTMTimeSeriesRegressor hyperparametersNew Primitivesmlprimitives.candidates.dsp.SpectralMaskmlprimitives.custom.timeseries_anomalies.find_anomaliesmlprimitives.custom.timeseries_anomalies.regression_errorsmlprimitives.custom.timeseries_preprocessing.rolling_window_sequencesmlprimitives.custom.timeseries_preprocessing.time_segments_averagesklearn.linear_model.ElasticNetsklearn.linear_model.Larssklearn.linear_model.Lassosklearn.linear_model.MultiTaskLassosklearn.linear_model.Ridge0.1.5New Primitivessklearn.impute.SimpleImputersklearn.preprocessing.MinMaxScalersklearn.preprocessing.MaxAbsScalersklearn.preprocessing.RobustScalersklearn.linear_model.LinearRegressionGeneral ImprovementsSeparate curated from candidate primitivesSetupentry_pointsin setup.py to improve compaitibility with MLBlocksAdd a test-pipelines command to test all the existing pipelinesClean sklearn example pipelinesChange theauthorentry to acontributorslistChange the name ofmlblocks_primitivesfolderPip installrequirements_dev.txtfail documentationBug FixesFix LSTMTimeSeriesRegressor primitive. Issue #90Fix timeseries primitives. Issue #91Negative index anomalies intimeseries_errors. Issue #89Keep pandas version below 0.24.0. Issue #870.1.4New Primitivesmlprimitives.timeseries primitives for timeseries data preprocessingmlprimitives.timeseres_error primitives for timeseries anomaly detectionkeras.Sequential.LSTMTimeSeriesRegressorsklearn.neighbors.KNeighbors Classifier and Regressorseveral sklearn.decomposition primitivesseveral sklearn.ensemble primitivesBug FixesFix typo in mlprimitives.text.TextCleaner primitiveFix bug in index handling in featuretools.dfs primitiveFix bug in SingleLayerCNNImageClassifier annotationRemove old vlaidation tags from JSON annotations0.1.3New FeaturesFix and re-enable featuretools.dfs primitive.0.1.2New FeaturesAdd pipeline specification language and Evaluation utilities.Add pipelines for graph, text and tabular problems.New primitives ClassEncoder and ClassDecoderNew primitives UniqueCounter and VocabularyCounterBug FixesFix TrivialPredictor bug when working with numpy arraysChange XGB default learning rate and number of estimators0.1.1New FeaturesAdd more keras.applications primitives.Add a Text Cleanup primitive.Bug FixesAdd keywords tokeras.preprocessingprimtives.Fix theimage_transformmethod.Addepochas a fixed hyperparameter forkeras.Sequentialprimitives.0.1.0First release on PyPI.