Compute the quantiles used for transforming. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features) The data used to scale along the features axis. If a sparse matrix is provided, it will be converted into a sparse
(self, X, y=None)
| 2873 | |
| 2874 | @_fit_context(prefer_skip_nested_validation=True) |
| 2875 | def fit(self, X, y=None): |
| 2876 | """Compute the quantiles used for transforming. |
| 2877 | |
| 2878 | Parameters |
| 2879 | ---------- |
| 2880 | X : {array-like, sparse matrix} of shape (n_samples, n_features) |
| 2881 | The data used to scale along the features axis. If a sparse |
| 2882 | matrix is provided, it will be converted into a sparse |
| 2883 | CSC matrix. Additionally, the sparse matrix needs to be |
| 2884 | nonnegative if `ignore_implicit_zeros` is False. |
| 2885 | |
| 2886 | y : None |
| 2887 | Ignored. |
| 2888 | |
| 2889 | Returns |
| 2890 | ------- |
| 2891 | self : object |
| 2892 | Fitted transformer. |
| 2893 | """ |
| 2894 | if self.subsample is not None and self.n_quantiles > self.subsample: |
| 2895 | raise ValueError( |
| 2896 | "The number of quantiles cannot be greater than" |
| 2897 | " the number of samples used. Got {} quantiles" |
| 2898 | " and {} samples.".format(self.n_quantiles, self.subsample) |
| 2899 | ) |
| 2900 | |
| 2901 | X = self._check_inputs(X, in_fit=True, copy=False) |
| 2902 | n_samples = X.shape[0] |
| 2903 | |
| 2904 | if self.n_quantiles > n_samples: |
| 2905 | warnings.warn( |
| 2906 | "n_quantiles (%s) is greater than the total number " |
| 2907 | "of samples (%s). n_quantiles is set to " |
| 2908 | "n_samples." % (self.n_quantiles, n_samples) |
| 2909 | ) |
| 2910 | self.n_quantiles_ = max(1, min(self.n_quantiles, n_samples)) |
| 2911 | |
| 2912 | rng = check_random_state(self.random_state) |
| 2913 | |
| 2914 | # Create the quantiles of reference |
| 2915 | self.references_ = np.linspace(0, 1, self.n_quantiles_, endpoint=True) |
| 2916 | if sparse.issparse(X): |
| 2917 | self._sparse_fit(X, rng) |
| 2918 | else: |
| 2919 | self._dense_fit(X, rng) |
| 2920 | |
| 2921 | return self |
| 2922 | |
| 2923 | def _transform_col(self, X_col, quantiles, inverse): |
| 2924 | """Private function to transform a single feature.""" |