Modeling

Simple Machine Learning Tool Kit package

This package contains the modules to simplify your code for your modeling processes.

It is part of the educational repositories (https://github.com/pandle/materials) to learn how to write stardard code and common uses of the TDD.

Package contents two classes to manage modeling.

>>> import smltk
>>> help(smltk)
>>> from smltk.modeling import Modeling
>>> help(Modeling)

# license MIT # support https://github.com/bilardi/smltk/issues

Modeling

The class Modeling contains methods to manage the modeling, from evaluation to save the mode.

ObjectDetection

Modeling

Modeling.split_tuples

Splits tuples of sample and its target in the relative lists

Modeling.prediction

Predicts with your model

Modeling.create_confusion_matrix

Creates and prints confusion matrix

Modeling.get_classification_metrics

Gets classification metrics

Modeling.scoring

Gets classification metrics after prediction

Modeling.modeling

Gets classification metrics after training and prediction

Modeling.print_metrics

Prints metrics

Modeling.save_model

Saves model

Modeling.resume_model

Resumes model

ObjectDetection.get_inference_objects

Rescale boxes with probability greater than the threshold

ObjectDetection.get_inference_objects_df

Create a DataFrame from the prediction of object detection

ObjectDetection.plot_inference_objects

Plot image with boxes

Detailed list

class smltk.modeling.Modeling

The class Modeling contains methods to manage the modeling, from evaluation to save the mode.

Here’s an example:

>>> from smltk.modeling import Modeling
>>> y_test, y_pred = self.load_prediction()
>>> mdl = Modeling()
>>> confusion_matrix = mdl.create_confusion_matrix(y_test, y_pred, True)
>>> print(confusion_matrix)
[[2, 0], [0, 2]]
clean_binary_classification(y_test, y_pred)

Transforms the target and prediction in integer 0 and 1

Arguments:
y_test (list[]):

list of targets

y_pred (list[]):

list of predictions

Returns:

y_test and y_pred with only 0 and 1 values

create_confusion_matrix(y_test, y_pred, is_test=False)

Creates and prints confusion matrix

Arguments:
y_test (list[]):

list of targets

y_pred (list[]):

list of predictions

is_test (bool):

default is False

Returns:

confusion matrix

fit_exists(model)

Get a boolean True if fit method exists

Arguments:
model (obj):

object of your model

Returns:

boolean

get_classification_metrics(params={})

Gets classification metrics

Arguments: params (dict) with the keys below
model (obj):

object of your model

X_train (list[]|list[tuple]):

list of samples or list of tuples with sample and its target

y_train (list[]):

list of targets

X_test (list[] | list[tuple]):

list of samples or list of tuples with sample and its target

y_test (list[]):

list of targets

y_pred (list[]):

list of predictions

loss (str):

parameter of bias_variance_decomp, default mse

num_rounds (int):

parameter of bias_variance_decomp, default 200

random_seed (int):

parameter of bias_variance_decomp, default 3

Returns:

dictionary with Loss, Bias, Variance, MCC, ROC_AUC, Accuracy, Precision, Recall, Fscore

is_binary_classification(y_test, y_pred)

Gets if the classification is binary or not

Arguments:
y_test (list[]):

list of targets

y_pred (list[]):

list of predictions

Returns:

boolean

modeling(model, X_train, y_train, X_test, y_test)

Gets classification metrics after training and prediction

Arguments:
model (obj):

object of your model

X_train (list[]|list[tuple]):

list of samples or list of tuples with sample and its target

y_train (list[]):

list of targets

X_test (list[] | list[tuple]):

list of samples or list of tuples with sample and its target

y_test (list[]):

list of targets

Returns:

dictionary with Loss, Bias, Variance, MCC, ROC_AUC, Accuracy, Precision, Recall, Fscore

predict_exists(model)

Get a boolean True if predict method exists

Arguments:
model (obj):

object of your model

Returns:

boolean

prediction(model, method, X_test, y_test=[])

Predicts with your model

Arguments:
model (obj):

object of your model

method (str):

name of method

X_test (list[]|list[tuple]):

list of samples or list of tuples with sample and its target

y_test (list[]):

list of targets

Returns:

tuple of list of targets and list of predictions

print_metrics(metrics)

Prints metrics

Arguments:
metrics (dict):

dictionary of metrics with their value

Returns:

only the print of metrics

resume_model(filename)

Resumes model

Arguments:
filename (str):

pathname and filename where you want to save your model

Returns:

object of your model

save_model(model, filename)

Saves model

Arguments:
model (obj):

object of your model

filename (str):

pathname and filename where you want to save your model

scoring(model, X_test, y_test)

Gets classification metrics after prediction

Arguments:
model (obj):

object of your model

X_test (list[] | list[tuple]):

list of samples or list of tuples with sample and its target

y_test (list[]):

list of targets

Returns:

dictionary with Loss, Bias, Variance, MCC, ROC_AUC, Accuracy, Precision, Recall, Fscore

split_tuples(tuples)

Splits tuples of sample and its target in the relative lists

Arguments:
tuples (list[tuple]):

list of tuples with sample and its target

Returns:

tuple of list of samples and list of targets

class smltk.modeling.ObjectDetection
bboxes_cxcywh_to_xyxy(bboxes)

Concatenate a sequence of tensors along a new dimension

Arguments:
bboxes (sequence of Tensors):

list of boxes Tensors

Returns:

sequence of Tensors

get_inference_objects(image, prediction, threshold=0.7)

Rescale boxes with probability greater than the threshold

Arguments:
image (PIL Image):

object of type PIL Image

prediction (dict):

prediction of the model with pred_logits and pred_boxes

threshold (float):

probability value used like threshold, default 0.7

Returns:

tuple of sequences of Tensors about probabilities and boxes

get_inference_objects_df(probability, boxes)

Create a DataFrame from the prediction of object detection

Arguments:
probability (sequence of Tensors):

list of probabilities Tensors

boxes (sequence of Tensors):

list of boxes Tensors

Returns:

Pandas DataFrame

plot_inference_objects(image, probability, boxes)

Plot image with boxes

Arguments:
image (PIL Image):

object of type PIL Image

probability (sequence of Tensors):

list of probabilities Tensors

boxes (sequence of Tensors):

list of boxes Tensors

Returns:

plot

rescale_bboxes(bboxes, size)

Rescale boxes on image size

Arguments:
bboxes (sequence of Tensors):

list of boxes Tensors

size (tuple):

width and height of image

Returns:

sequence of Tensors