nimble.trainAndApply¶
- nimble.trainAndApply(learnerName, trainX, trainY=None, testX=None, arguments=None, multiClassStrategy=None, randomSeed=None, tuning=None, scoreMode=None, *, useLog=None, **kwarguments)¶
Train a model and apply it to the test data.
The learner will be trained using the training data, then prediction, transformation, etc. as appropriate to the learner will be applied to the test data and returned. For learners that offer a scoring method, the
scoreMode
can be set to ‘bestScore’ or ‘allScores’. The ‘bestScore’ option returns two features, the predicted class and score for that class. The ‘allScores’ option will return a matrix with a feature of scores for each class.- Parameters:
learnerName (str) – The learner to be called. This can be a string in the form ‘package.learner’ or the learner class object.
trainX (nimble Base object) – Data to be used for training.
trainY (identifier, nimble Base object) – A name or index of the feature in
trainX
containing the labels or another nimble Base object containing the labels that correspond totrainX
.testX (nimble Base object) – Data set on which the trained learner will be applied (i.e. performing prediction, transformation, etc. as appropriate to the learner).
arguments (dict) – Mapping argument names (strings) to their values, to be used during training and application (e.g., {‘dimensions’:5, ‘k’:5}). Multiple values for arguments can be provided by using a
Tune
object (e.g., {‘k’: Tune([3, 5, 7])}) to initiate hyperparameter tuning and return the learner trained on the best set of arguments. To provide an argument that is an object from the same package as the learner, use animble.Init
object with the object name and its instantiation arguments (e.g., {‘optimizer’: nimble.Init(‘SGD’, learning_rate=0.01}). Note: learner arguments can also be passed askwarguments
so this dictionary will be merged with any keyword arguments.multiClassStrategy (str, None) – May be ‘OneVsAll’ or ‘OneVsOne’ to train the learner using that multiclass strategy. When None, the learner is trained on the data as provided.
randomSeed (int) – Set a random seed for the operation. When None, the randomness is controlled by Nimble’s random seed. Ignored if learner does not depend on randomness.
tuning (nimble.Tuning, performanceFunction, None) – Required when hyperparameter tuning is initiated by
Tune
objects in the arguments. A Tuning instance details how the argument sets will be selected and validated. For convenience, a performanceFunction may instead be provided and this will trigger construction of a Tuning instance using the default consecutive selection method with 5-fold cross validation.scoreMode (str, None) – For learners that offer a scoring method, this can be set to ‘bestScore’ or ‘allScores’. The ‘bestScore’ option returns two features, the predicted class and score for that class. The ‘allScores’ option will construct a matrix where each feature represents the scores for that class.
useLog (bool, None) – Local control for whether to send results/timing to the logger. If None (default), use the value as specified in the “logger” “enabledByDefault” configuration option. If True, send to the logger regardless of the global option. If False, do NOT send to the logger, regardless of the global option.
kwarguments – Keyword arguments specified variables that are passed to the learner. These are combined with the
arguments
parameter. Multiple values for arguments can be provided by using aTune
object (e.g., k=Tune([3, 5, 7])) to initiate hyperparameter tuning and return the learner trained on the best set of arguments. To provide an argument that is an object from the same package as the learner, use animble.Init
object with the object name and its instantiation arguments (e.g., optimizer=nimble.Init(‘SGD’, learning_rate=0.01)).
- Returns:
results – The resulting output of applying learner.
See also
train
,nimble.Init
,nimble.Tune
,nimble.Tuning
,nimble.core.interfaces.TrainedLearner.apply
Examples
Train dataset which contains the labels.
>>> lstTrain = [[1, 0, 0, 1], ... [0, 1, 0, 2], ... [0, 0, 1, 3], ... [1, 0, 0, 1], ... [0, 1, 0, 2], ... [0, 0, 1, 3]] >>> lstTestX = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] >>> trainData = nimble.data(lstTrain) >>> testX = nimble.data(lstTestX) >>> predict = nimble.trainAndApply('nimble.KNNClassifier', ... trainX=trainData, trainY=3, ... testX=testX) >>> predict <Matrix 3pt x 1ft 0 ┌── 0 │ 1 1 │ 2 2 │ 3 >
Passing arguments to the learner. Both the arguments parameter and kwarguments can be utilized, they will be merged. Below,
C
andkernel
are parameters for scikit-learn’s SVC learner.>>> lstTrainX = [[1, 0, 0], ... [0, 1, 0], ... [0, 0, 1], ... [1, 0, 0], ... [0, 1, 0], ... [0, 0, 1]] >>> lstTrainY = [[1], [2], [3], [1], [2], [3]] >>> lstTestX = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] >>> trainX = nimble.data(lstTrainX) >>> trainY = nimble.data(lstTrainY) >>> testX = nimble.data(lstTestX) >>> pred = nimble.trainAndApply('sciKitLearn.SVC', trainX=trainX, ... trainY=trainY, testX=testX, ... arguments={'C': 0.1}, kernel='linear') >>> pred <Matrix 3pt x 1ft 0 ┌── 0 │ 1 1 │ 2 2 │ 3 >
Keywords: predict, prediction, transformation, encode, standardize, model, supervised, unsupervised, pred, transform, fit_predict, fit_transform, training, machine learning