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
(self, X, y=None)
| 7 | fit_required = True |
| 8 | |
| 9 | def _setup_input(self, X, y=None): |
| 10 | """Ensure inputs to an estimator are in the expected format. |
| 11 | |
| 12 | Ensures X and y are stored as numpy ndarrays by converting from an |
| 13 | array-like object if necessary. Enables estimators to define whether |
| 14 | they require a set of y target values or not with y_required, e.g. |
| 15 | kmeans clustering requires no target labels and is fit against only X. |
| 16 | |
| 17 | Parameters |
| 18 | ---------- |
| 19 | X : array-like |
| 20 | Feature dataset. |
| 21 | y : array-like |
| 22 | Target values. By default is required, but if y_required = false |
| 23 | then may be omitted. |
| 24 | """ |
| 25 | if not isinstance(X, np.ndarray): |
| 26 | X = np.array(X) |
| 27 | |
| 28 | if X.size == 0: |
| 29 | raise ValueError("Got an empty matrix.") |
| 30 | |
| 31 | if X.ndim == 1: |
| 32 | self.n_samples, self.n_features = 1, X.shape |
| 33 | else: |
| 34 | self.n_samples, self.n_features = X.shape[0], np.prod(X.shape[1:]) |
| 35 | |
| 36 | self.X = X |
| 37 | |
| 38 | if self.y_required: |
| 39 | if y is None: |
| 40 | raise ValueError("Missed required argument y") |
| 41 | |
| 42 | if not isinstance(y, np.ndarray): |
| 43 | y = np.array(y) |
| 44 | |
| 45 | if y.size == 0: |
| 46 | raise ValueError("The targets array must be no-empty.") |
| 47 | |
| 48 | self.y = y |
| 49 | |
| 50 | def fit(self, X, y=None): |
| 51 | self._setup_input(X, y) |