coding utf8 ensure inputs to an estimator are in the expected format ensures x and y are stored as numpy ndarrays by converting from an arraylike object if necessary enables estimators to define whether they require a set of y target values or not with yrequired e g kmeans clustering requires no target labels and is fit against only x parameters x arraylike feature dataset y arraylike target values by default is required but if yrequired false then may be omitted coding utf 8 ensure inputs to an estimator are in the expected format ensures x and y are stored as numpy ndarrays by converting from an array like object if necessary enables estimators to define whether they require a set of y target values or not with y_required e g kmeans clustering requires no target labels and is fit against only x parameters x array like feature dataset y array like target values by default is required but if y_required false then may be omitted
import numpy as np


class BaseEstimator:
    y_required = True
    fit_required = True

    def _setup_input(self, X, y=None):
        
        if not isinstance(X, np.ndarray):
            X = np.array(X)

        if X.size == 0:
            raise ValueError("Got an empty matrix.")

        if X.ndim == 1:
            self.n_samples, self.n_features = 1, X.shape
        else:
            self.n_samples, self.n_features = X.shape[0], np.prod(X.shape[1:])

        self.X = X

        if self.y_required:
            if y is None:
                raise ValueError("Missed required argument y")

            if not isinstance(y, np.ndarray):
                y = np.array(y)

            if y.size == 0:
                raise ValueError("The targets array must be no-empty.")

        self.y = y

    def fit(self, X, y=None):
        self._setup_input(X, y)

    def predict(self, X=None):
        if not isinstance(X, np.ndarray):
            X = np.array(X)

        if self.X is not None or not self.fit_required:
            return self._predict(X)
        else:
            raise ValueError("You must call `fit` before `predict`")

    def _predict(self, X=None):
        raise NotImplementedError()