coremltools.models.nearest_neighbors.builder

Classes

KNearestNeighborsClassifierBuilder(…) KNearestNeighborsClassifierBuilder class to construct a CoreML KNearestNeighborsClassifier specification.
class coremltools.models.nearest_neighbors.builder.KNearestNeighborsClassifierBuilder(input_name, output_name, number_of_dimensions, default_class_label, **kwargs)

KNearestNeighborsClassifierBuilder class to construct a CoreML KNearestNeighborsClassifier specification.

Please see the Core ML Nearest Neighbors protobuf message for more information on KNearestNeighborsClassifier parameters.

See also

MLModel, save_spec

Examples

from coremltools.models.nearest_neighbors import KNearestNeighborsClassifierBuilder
from coremltools.models.utils import save_spec

# Create a KNearestNeighborsClassifier model that takes 4-dimensional input data and outputs a string label.
>>> builder = KNearestNeighborsClassifierBuilder(input_name='input',
...                                              output_name='output',
...                                              number_of_dimensions=4,
...                                              default_class_label='default_label')

# save the spec by the builder
>>> save_spec(builder.spec, 'knnclassifier.mlmodel')
__init__(self, input_name, output_name, number_of_dimensions, default_class_label, **kwargs)

Create a KNearestNeighborsClassifierBuilder object. :param input_name: Name of the model input :param output_name: Name of the output :param number_of_dimensions: Number of dimensions of the input data :param default_class_label: The default class label to use for predictions. Must be either an int64 or a string. :param number_of_neighbors: Number of neighbors to use for predictions. Default = 5 with allowed values between 1-1000. :param weighting_scheme: Weight function used in prediction. One of ‘uniform’ (default) or ‘inverse_distance’ :param index_type: Algorithm to compute nearest neighbors. One of ‘linear’ (default), or ‘kd_tree’. :param leaf_size: Leaf size for the kd-tree. Ignored if index type is ‘linear’. Default = 30.

add_samples(self, data_points, labels)

Add some samples to the KNearestNeighborsClassifier model :param data_points: List of input data points :param labels: List of corresponding labels :return: None

author

Get the author for the KNearestNeighborsClassifier model :return: the author

description

Get the description for the KNearestNeighborsClassifier model :return: the description

index_type

Get the index type for the KNearestNeighborsClassifier model :return: the index type

is_updatable

Check if the KNearestNeighborsClassifier is updatable :return: is updatable

leaf_size

Get the leaf size for the KNearestNeighborsClassifier :return: the leaf size

license

Get the author for the KNearestNeighborsClassifier model :return: the author

number_of_dimensions

Get the number of dimensions of the input data for the KNearestNeighborsClassifier model :return: number of dimensions

number_of_neighbors

Get the number of neighbors value for the KNearestNeighborsClassifier model :return: the number of neighbors default value

number_of_neighbors_allowed_range(self)

Get the range of allowed values for the numberOfNeighbors parameter. :return: tuple of (min_value, max_value) or None if the range hasn’t been set

number_of_neighbors_allowed_set(self)

Get the set of allowed values for the numberOfNeighbors parameter. :return: set of allowed values or None if the set of allowed values hasn’t been populated

set_index_type(self, index_type, leaf_size=30)

Set the index type for the KNearestNeighborsClassifier model :param index_type: One of [ ‘linear’, ‘kd_tree’ ] :param leaf_size: For kd_tree indexes, the leaf size to use (default = 30) :return: None

set_number_of_neighbors_with_bounds(self, number_of_neighbors, allowed_range=None, allowed_set=None)

Set the numberOfNeighbors parameter for the KNearestNeighborsClassifier model. :param allowed_range: tuple of (min_value, max_value) defining the range of allowed values :param allowed_values: set of allowed values for the number of neighbors :return: None

weighting_scheme

Get the weighting scheme for the KNearestNeighborsClassifier model :return: the weighting scheme