olpy.classifiers.Perceptron

class olpy.classifiers.Perceptron(num_iterations=1, random_state=None, positive_label=1, class_weight=None)[source]

Bases: olpy.classifiers.__base.OnlineLearningModel

The Perceptron model.

Rosenblatt, F., The perceptron: a probabilistic model for information storage and organization in the brain., Psychological review, American Psychological Association, 1958, 65, 386

num_iterations

Number of iterations to run the training for. Defaults to 1.

Type

int, optional

random_state

The random seed to use with the pseudo-random generator. Defaults to None.

Type

int, optional

positive_label

The number in the output field that represents the positive label. The value passed should be different than -1. Defaults to 1.

Type

int, optional

class_weight

Represents the relative weight of the labels in the data. Useful for imbalanced classification tasks.

Type

dict, optional

Raises

AssertionError – if positive_label is equal to -1.

Methods

decision_function

Compute the values of the decision boundaries.

fit

Fits the model to the (X,Y) pair passed to the function.

get_params

Get parameters for this estimator.

partial_fit

Trains the model on a single data point.

predict

Predict the label given the test dataset X.

predict_proba

Compute the probability that a model is from a class.

score

Compute the score performed on the dataset.

set_params

Set the parameters of this estimator.

decision_function(X)

Compute the values of the decision boundaries.

Given an unlabelled data, this method computes the probabilities of being assigned the positive label.

Parameters

X (numpy.ndarray) – Input data with n rows and m columns

Returns

The probabilities for each data point to be of the positive class label.

Return type

list[float]

fit(X: numpy.ndarray, Y: numpy.ndarray, verbose=False, **kwargs)

Fits the model to the (X,Y) pair passed to the function.

Parameters
  • X (numpy.ndarray) – Input data with n rows and m columns

  • Y (numpy.ndarray) – Output variable with binary labels.

  • verbose (bool, optional) – Specifies whether the performances should be reported or not. Defaults to False.

  • kwargs (**) – Arbitrary keyword arguments.

Returns

the trained model.

Return type

self

get_params(deep=True)

Get parameters for this estimator.

This function is for use with hyper-parameter tuning utilities such as GridSearchCV.

Parameters
  • deep (bool, optional) – If True, will return the parameters

  • are (for this estimator and contained sub-objects that) –

  • True. (estimators. Defaults to) –

partial_fit(X, Y, classes=None)

Trains the model on a single data point.

Parameters
  • X (numpy.ndarray) – Input data with n rows and m columns

  • Y (numpy.ndarray) – Output variable with binary labels.

  • classes (list or tuple, optional) – Represents the available labels in the dataset. Needs to be passed only once.

Returns

the trained model.

Return type

self

predict(X)

Predict the label given the test dataset X.

Given an unlabelled data, this function predicts the labels to be assigned to it based on the weights learned so far.

Parameters

X (numpy.ndarray or list) – unlabelled data.

Returns

np.ndarray with dimension (n,) representing the output labels.

Raises
  • NotFittedError – when the method is called without prior

  • fitting.

predict_proba(X)

Compute the probability that a model is from a class.

Given an unlabelled data, this method returns the probability for each data point to belong to either class.

Parameters

X (numpy.ndarray) – Input data with n rows and m columns

Returns

the probabilities of the

data points belonging to each class.

Return type

list of size (n, 2)

score(X, y)

Compute the score performed on the dataset.

Given a labelled data, this method evaluate the performance of the models by predicting and computing the accuracy score.

Note

Different models can override the function to return a different metric.

Parameters
  • X (numpy.ndarray) – Input data with n rows and m columns

  • Y (, `numpy.ndarray) – Output variable with binary labels.

Returns

The accuracy score of the model given the labelled data.

Return type

float

set_params(**parameters)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters

parameters (**) – Estimator parameters.

Returns

estimator instance.

Return type

self