from sklearn.linear_model.perceptron import Perceptron as SKLModel
import lale.helpers
import lale.operators
from numpy import nan, inf
[docs]class PerceptronImpl():
def __init__(self, penalty=None, alpha=0.0001, fit_intercept=True, max_iter=None, tol=None, shuffle=True, verbose=0, eta0=1.0, n_jobs=None, random_state=None, early_stopping=False, validation_fraction=0.1, n_iter_no_change=5, class_weight='balanced', warm_start=False, n_iter=None):
self._hyperparams = {
'penalty': penalty,
'alpha': alpha,
'fit_intercept': fit_intercept,
'max_iter': max_iter,
'tol': tol,
'shuffle': shuffle,
'verbose': verbose,
'eta0': eta0,
'n_jobs': n_jobs,
'random_state': random_state,
'early_stopping': early_stopping,
'validation_fraction': validation_fraction,
'n_iter_no_change': n_iter_no_change,
'class_weight': class_weight,
'warm_start': warm_start,
'n_iter': n_iter}
[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 Perceptron Perceptron',
'allOf': [{
'type': 'object',
'required': ['penalty', 'alpha', 'fit_intercept', 'max_iter', 'tol', 'shuffle', 'verbose', 'eta0', 'n_jobs', 'random_state', 'early_stopping', 'validation_fraction', 'n_iter_no_change', 'class_weight', 'warm_start', 'n_iter'],
'relevantToOptimizer': ['alpha', 'fit_intercept', 'max_iter', 'tol', 'shuffle', 'eta0', 'n_iter'],
'additionalProperties': False,
'properties': {
'penalty': {
'XXX TODO XXX': "None, 'l2' or 'l1' or 'elasticnet'",
'description': 'The penalty (aka regularization term) to be used. Defaults to None.',
'enum': [None],
'default': None},
'alpha': {
'type': 'number',
'minimumForOptimizer': 1e-10,
'maximumForOptimizer': 1.0,
'distribution': 'loguniform',
'default': 0.0001,
'description': 'Constant that multiplies the regularization term if regularization is'},
'fit_intercept': {
'type': 'boolean',
'default': True,
'description': 'Whether the intercept should be estimated or not. If False, the'},
'max_iter': {
'anyOf': [{
'type': 'integer',
'minimumForOptimizer': 10,
'maximumForOptimizer': 1000,
'distribution': 'uniform'}, {
'enum': [None]}],
'default': None,
'description': 'The maximum number of passes over the training data (aka epochs).'},
'tol': {
'anyOf': [{
'type': 'number',
'minimumForOptimizer': 1e-08,
'maximumForOptimizer': 0.01,
'distribution': 'loguniform'}, {
'enum': [None]}],
'default': None,
'description': 'The stopping criterion. If it is not None, the iterations will stop'},
'shuffle': {
'type': 'boolean',
'default': True,
'description': 'Whether or not the training data should be shuffled after each epoch.'},
'verbose': {
'type': 'integer',
'default': 0,
'description': 'The verbosity level'},
'eta0': {
'type': 'number',
'minimumForOptimizer': 0.01,
'maximumForOptimizer': 1.0,
'distribution': 'uniform',
'default': 1.0,
'description': 'Constant by which the updates are multiplied. Defaults to 1.'},
'n_jobs': {
'anyOf': [{
'type': 'integer'}, {
'enum': [None]}],
'default': None,
'description': 'The number of CPUs to use to do the OVA (One Versus All, for'},
'random_state': {
'anyOf': [{
'type': 'integer'}, {
'type': 'object'}, {
'enum': [None]}],
'default': None,
'description': 'The seed of the pseudo random number generator to use when shuffling'},
'early_stopping': {
'type': 'boolean',
'default': False,
'description': 'Whether to use early stopping to terminate training when validation.'},
'validation_fraction': {
'type': 'number',
'default': 0.1,
'description': 'The proportion of training data to set aside as validation set for'},
'n_iter_no_change': {
'type': 'integer',
'default': 5,
'description': 'Number of iterations with no improvement to wait before early stopping.'},
'class_weight': {
'XXX TODO XXX': 'dict, {class_label: weight} or "balanced" or None, optional',
'description': 'Preset for the class_weight fit parameter.',
'enum': ['balanced'],
'default': 'balanced'},
'warm_start': {
'type': 'boolean',
'default': False,
'description': 'When set to True, reuse the solution of the previous call to fit as'},
'n_iter': {
'anyOf': [{
'type': 'integer',
'minimumForOptimizer': 5,
'maximumForOptimizer': 1000,
'distribution': 'uniform'}, {
'enum': [None]}],
'default': None,
'description': 'The number of passes over the training data (aka epochs).'},
}}, {
'XXX TODO XXX': 'Parameter: max_iter > only impacts the behavior in the fit method'}, {
'description': 'validation_fraction, only used if early_stopping is true',
'anyOf': [{
'type': 'object',
'properties': {
'validation_fraction': {
'enum': [0.1]},
}}, {
'type': 'object',
'properties': {
'early_stopping': {
'enum': [True]},
}}]}],
}
_input_fit_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'description': 'Fit linear model with Stochastic Gradient Descent.',
'type': 'object',
'properties': {
'X': {
'type': 'array',
'items': {
'type': 'array',
'items': {
'type': 'number'},
},
'description': 'Training data'},
'y': {
'type': 'array',
'items': {
'type': 'number'},
'description': 'Target values'},
'coef_init': {
'type': 'array',
'items': {
'type': 'array',
'items': {
'type': 'number'},
},
'description': 'The initial coefficients to warm-start the optimization.'},
'intercept_init': {
'type': 'array',
'items': {
'type': 'number'},
'description': 'The initial intercept to warm-start the optimization.'},
'sample_weight': {
'anyOf': [{
'type': 'array',
'items': {
'type': 'number'},
}, {
'enum': [None]}],
'default': None,
'description': 'Weights applied to individual samples.'},
},
}
_input_predict_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'description': 'Predict class labels for samples in X.',
'type': 'object',
'properties': {
'X': {
'anyOf': [{
'type': 'array',
'items': {
'XXX TODO XXX': 'item type'},
'XXX TODO XXX': 'array_like or sparse matrix, shape (n_samples, n_features)'}, {
'type': 'array',
'items': {
'type': 'array',
'items': {
'type': 'number'},
}}],
'description': 'Samples.'},
},
}
_output_predict_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'description': 'Predicted class label per sample.',
'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)
Perceptron = lale.operators.make_operator(PerceptronImpl, _combined_schemas)