Source code for lale.lib.autogen.nearest_centroid


from sklearn.neighbors.nearest_centroid import NearestCentroid as SKLModel
import lale.helpers
import lale.operators
from numpy import nan, inf

[docs]class NearestCentroidImpl(): def __init__(self, metric='euclidean', shrink_threshold=None): self._hyperparams = { 'metric': metric, 'shrink_threshold': shrink_threshold}
[docs] def fit(self, X, y=None): self._sklearn_model = SKLModel(**self._hyperparams) if (y is not None): self._sklearn_model.fit(X, y) else: self._sklearn_model.fit(X) return self
[docs] def predict(self, X): return self._sklearn_model.predict(X)
_hyperparams_schema = { '$schema': 'http://json-schema.org/draft-04/schema#', 'description': 'inherited docstring for NearestCentroid Nearest centroid classifier.', 'allOf': [{ 'type': 'object', 'required': ['metric', 'shrink_threshold'], 'relevantToOptimizer': ['metric'], 'additionalProperties': False, 'properties': { 'metric': { 'anyOf': [{ 'type': 'object', 'forOptimizer': False}, { 'enum': ['euclidean', 'manhattan', 'minkowski']}], 'default': 'euclidean', 'description': 'The metric to use when calculating distance between instances in a'}, 'shrink_threshold': { 'anyOf': [{ 'type': 'number'}, { 'enum': [None]}], 'default': None, 'description': 'Threshold for shrinking centroids to remove features.'}, }}], } _input_fit_schema = { '$schema': 'http://json-schema.org/draft-04/schema#', 'description': 'Fit the NearestCentroid model according to the given training data.', 'type': 'object', 'properties': { 'X': { 'type': 'array', 'items': { 'type': 'array', 'items': { 'type': 'number'}, }, 'description': 'Training vector, where n_samples is the number of samples and'}, 'y': { 'type': 'array', 'items': { 'type': 'number'}, 'description': 'Target values (integers)'}, }, } _input_predict_schema = { '$schema': 'http://json-schema.org/draft-04/schema#', 'description': 'Perform classification on an array of test vectors X.', 'type': 'object', 'properties': { 'X': { 'type': 'array', 'items': { 'type': 'array', 'items': { 'type': 'number'}, }}, }, } _output_predict_schema = { '$schema': 'http://json-schema.org/draft-04/schema#', 'description': 'Perform classification on an array of test vectors X.', 'type': 'array', 'items': { 'type': 'number'}, } _combined_schemas = { '$schema': 'http://json-schema.org/draft-04/schema#', 'description': 'Combined schema for expected data and hyperparameters.', 'type': 'object', 'tags': { 'pre': [], 'op': ['estimator'], 'post': []}, 'properties': { 'hyperparams': _hyperparams_schema, 'input_fit': _input_fit_schema, 'input_predict': _input_predict_schema, 'output_predict': _output_predict_schema}, } if (__name__ == '__main__'): lale.helpers.validate_is_schema(_combined_schemas) NearestCentroid = lale.operators.make_operator(NearestCentroidImpl, _combined_schemas)