Classification

Module for classification tasks. The classes in this module accept a ndarray of the values to be inputted (X) and a ndarray of the classes of the output. Their predict methods output the class of the supplied values.

class mapyl.classification.SoftMaxClasser(c=2, lr=0.01)[source]

SoftMax classifier instance

Parameters:

c (int): The number of classes. Defaults to 2

lr (float): The learning rate. Defaults to 0.01

fit(X, y, iters)[source]

Trains the SoftMaxClass instance

Paramaters:

X (ndarray): ndarray of the input of shape (num_samples, num_features)

y (ndarray): ndarray of the output of shape (num_samples,)

iters (int): The number of iterations

Returns: none

predict(X)[source]

Predicts the class of the X instance

Parameter:

X (ndarray): ndarray of shape (num_samples,num_features) to be predicted

Returns: class of the supplied X

Usage:

>>> X = np.array([[4, -3],
                    [9, -6],
                    [3, -5],
                    [4, -3],
                    [9, -5,]])
>>> y = np.array([[0],
                    [1],
                    [0],
                    [0],
                    [1]])

>>> SMC = SoftMaxClasser(2)
>>> SMC.fit(X, y)
>>> print(SMC.predict(np.array([[9, -7]])))
>>> [1]
class mapyl.classification.SVM(lr=0.001, lam=0.01, degree=1)[source]

Support Vector Machine instance

Parameters:

lr (float): float of the learning rate. Defaults to 0.001

lam (float): float of the regularization parameter. defaults to 0.01

degree (int): int of the poly degree (1 for linear). Defaults to 1

fit(X, y, iters=300)[source]

Fits the instance

Parameters:

X (ndarray): ndarray of shape (num_samples, num_features) of the input

y (ndarray): ndarray of shape (num_samples,) of the output

iters (int): The number of iterations. Defaults to 300

Returns:

self: The fitted instance

predict(X)[source]

Predicts the class of the supplied X

Parameter:

X (ndarray): ndarray of shape (num_samples, num_features) to be classified

Returns: int classifying as 1 or -1

Warning

The SVM instance does a polynomial expansion in order to work when the degree is over 1, so if there are too many degrees or too many features you can have overflow

Usage:
>>> X = np.array([[4, -3],
                  [9, -6],
                  [3, -5],
                  [4, -3],
                  [9, -5,]])
>>> y = np.array([[0],
                  [1],
                  [0],
                  [0],
                  [1]])

>>> SVM_ = SVM(lr=0.01, lam=0.01, iters=300, degree=1)
>>> SVM_.fit(X, y)
>>> print(SVM_.predict(np.array([[9, -7]])))
>>> [1]
class mapyl.classification.KNearestNeighbors(K=5)[source]

KNearest Neighbors instance for classification

Parameter:

K (int): the number of nearest neighbors. Defaults to 5

fit(X, y)[source]

Fits the instance

Parameters:

X (ndarray): ndarray of shape (num_samples, num_features) of the input

y (ndarray): ndarray of shape (num_samples,) of the output

Returns:

self: The fitted instance

predict(X)[source]

Predicts the class of an X value

Parameter:

X (ndarray): The X values to be predicted

Returns:

int: The index of the class of the supplied X

Usage:
>>> X = np.array([[4, -3],
                  [9, -6],
                  [3, -5],
                  [4, -3],
                  [9, -5,]])
>>> y = np.array([[0],
                  [1],
                  [0],
                  [0],
                  [1]])

>>> KNN = KNearestNeighbors(K=3)
>>> KNN.fit(X, y)
>>> print(KNN.predict(np.array([[9, -7]])))
>>> [1]