`L i9:ddlmZddlZddlmZddlmZddlm Z ddl m Z ddl m Z dd lmZdd lmZmZmZdd lmZmZmZmZd d lmZGddeZy))IntegralN) _fit_context)pairwise_distances_chunked) _NAN_METRICS) _get_weights) _get_mask) is_scalar_nan)HiddenInterval StrOptions) FLOAT_DTYPES_check_feature_names_incheck_is_fitted validate_data) _BaseImputerceZdZUdZiej eedddgeddhe e dgee e e gdgd Ze ed <ej d dd d dddfd ZdZed dfd ZfdZddZxZS) KNNImputeraImputation for completing missing values using k-Nearest Neighbors. Each sample's missing values are imputed using the mean value from `n_neighbors` nearest neighbors found in the training set. Two samples are close if the features that neither is missing are close. Read more in the :ref:`User Guide `. .. versionadded:: 0.22 Parameters ---------- missing_values : int, float, str, np.nan or None, default=np.nan The placeholder for the missing values. All occurrences of `missing_values` will be imputed. For pandas' dataframes with nullable integer dtypes with missing values, `missing_values` should be set to np.nan, since `pd.NA` will be converted to np.nan. n_neighbors : int, default=5 Number of neighboring samples to use for imputation. weights : {'uniform', 'distance'} or callable, default='uniform' Weight function used in prediction. Possible values: - 'uniform' : uniform weights. All points in each neighborhood are weighted equally. - 'distance' : weight points by the inverse of their distance. in this case, closer neighbors of a query point will have a greater influence than neighbors which are further away. - callable : a user-defined function which accepts an array of distances, and returns an array of the same shape containing the weights. metric : {'nan_euclidean'} or callable, default='nan_euclidean' Distance metric for searching neighbors. Possible values: - 'nan_euclidean' - callable : a user-defined function which conforms to the definition of ``func_metric(x, y, *, missing_values=np.nan)``. `x` and `y` corresponds to a row (i.e. 1-D arrays) of `X` and `Y`, respectively. The callable should returns a scalar distance value. copy : bool, default=True If True, a copy of X will be created. If False, imputation will be done in-place whenever possible. add_indicator : bool, default=False If True, a :class:`MissingIndicator` transform will stack onto the output of the imputer's transform. This allows a predictive estimator to account for missingness despite imputation. If a feature has no missing values at fit/train time, the feature won't appear on the missing indicator even if there are missing values at transform/test time. keep_empty_features : bool, default=False If True, features that consist exclusively of missing values when `fit` is called are returned in results when `transform` is called. The imputed value is always `0`. .. versionadded:: 1.2 Attributes ---------- indicator_ : :class:`~sklearn.impute.MissingIndicator` Indicator used to add binary indicators for missing values. ``None`` if add_indicator is False. n_features_in_ : int Number of features seen during :term:`fit`. .. versionadded:: 0.24 feature_names_in_ : ndarray of shape (`n_features_in_`,) Names of features seen during :term:`fit`. Defined only when `X` has feature names that are all strings. .. versionadded:: 1.0 See Also -------- SimpleImputer : Univariate imputer for completing missing values with simple strategies. IterativeImputer : Multivariate imputer that estimates values to impute for each feature with missing values from all the others. References ---------- * `Olga Troyanskaya, Michael Cantor, Gavin Sherlock, Pat Brown, Trevor Hastie, Robert Tibshirani, David Botstein and Russ B. Altman, Missing value estimation methods for DNA microarrays, BIOINFORMATICS Vol. 17 no. 6, 2001 Pages 520-525. `_ Examples -------- >>> import numpy as np >>> from sklearn.impute import KNNImputer >>> X = [[1, 2, np.nan], [3, 4, 3], [np.nan, 6, 5], [8, 8, 7]] >>> imputer = KNNImputer(n_neighbors=2) >>> imputer.fit_transform(X) array([[1. , 2. , 4. ], [3. , 4. , 3. ], [5.5, 6. , 5. ], [8. , 8. , 7. ]]) For a more detailed example see :ref:`sphx_glr_auto_examples_impute_plot_missing_values.py`. rNleft)closeduniformdistanceboolean) n_neighborsweightsmetriccopy_parameter_constraints nan_euclideanTF)missing_valuesrrrr add_indicatorkeep_empty_featurescbt||||||_||_||_||_y)N)r"r#r$)super__init__rrrr) selfr"rrrrr#r$ __class__s Y/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/impute/_knn.pyr'zKNNImputer.__init__s? )' 3  '   c@tj||dz dddd|f}|tj|jddddf|f}t ||j }|d|tj |<n-tj|}d|tj |<|j|}|j|} tjj|| }tjj|d|jS)aHelper function to impute a single column. Parameters ---------- dist_pot_donors : ndarray of shape (n_receivers, n_potential_donors) Distance matrix between the receivers and potential donors from training set. There must be at least one non-nan distance between a receiver and a potential donor. n_neighbors : int Number of neighbors to consider. fit_X_col : ndarray of shape (n_potential_donors,) Column of potential donors from training set. mask_fit_X_col : ndarray of shape (n_potential_donors,) Missing mask for fit_X_col. Returns ------- imputed_values: ndarray of shape (n_receivers,) Imputed values for receiver. raxisNrgmask)r.r) np argpartitionarangeshaperrisnan ones_liketakemaarrayaveragedata) r(dist_pot_donorsr fit_X_colmask_fit_X_col donors_idx donors_dist weight_matrixdonors donors_masks r* _calc_imputezKNNImputer._calc_imputes2___kAoAN | |O & IIj&&q) *1d7 3Z ? %[$,,?   $58M"((=1 2LL5M36M"((;/ 0 +$))*5 V+6uu}}V!]}CHHHr+)prefer_skip_nested_validationcRt|jsd}nd}t||dt||j}||_t |j |j|_tj|jd|_ t|1|j|S)aFit the imputer on X. Parameters ---------- X : array-like shape of (n_samples, n_features) Input data, where `n_samples` is the number of samples and `n_features` is the number of features. y : Ignored Not used, present here for API consistency by convention. Returns ------- self : object The fitted `KNNImputer` class instance. T allow-nanF) accept_sparsedtypeensure_all_finiterrr-) r r"rrr_fit_Xr _mask_fit_Xr1all _valid_maskr&_fit_indicator)r(XyrJr)s r*fitzKNNImputer.fits&T001 $  +    /   $T[[$2E2EFFF4#3#3!<< t//0 r+c  ttjsd}nd}tdtd|j dt j j j t) }tj dd fs2jr }d|dd f<n dd f}t9||Stj dd fjd tj  tj"j$dt& tj( j$d < fd }t+ ddfj,j.j|| }|D]}jr }d|dd f<n dd f}t9||S) aImpute all missing values in X. Parameters ---------- X : array-like of shape (n_samples, n_features) The input data to complete. Returns ------- X : array-like of shape (n_samples, n_output_features) The imputed dataset. `n_output_features` is the number of features that is not always missing during `fit`. TrGF)rHrIforce_writeablerJrresetNrrr-)rIc B||t|z}t jdD]q}|s ||f}tj|s'tj dd|f\}|tj |}|||z dd|f}tj|jd}||} | jrtjjjdd|fdd|fj} | | |f<t| t|k(r ||}|||z dd|f}tjt|} j!|| j||f||f} | ||f<ty)Nrr-r/)lenranger4r1anynonzero flatnonzeror5rMsizer8r9rKmeanminrrD) dist_chunkstartrow_missing_chunkcolcol_maskpotential_donors_idx receivers_idx dist_subsetall_nan_dist_maskall_nan_receivers_idxcol_meanrvaluerP dist_idx_mapr0 mask_fit_Xnon_missing_fix_Xrow_missing_idxr( valid_masks r* process_chunkz+KNNImputer.transform..process_chunk:s /J8O P QWWQZ(/ .!# 13 67vvh'*,**5Fq#v5N*O'%!2"..2J K )m)Du)LM++ %'HH[$9$=$=1$=$E!(56G(H%(--!uu{{ AsF+*QV2D + df5=A+S0101S5GG %23D2D$EM",\--H5-P"Q//#K"$"2"2C8L4MN ))KK 4c 9:3S89  ).-$%_/ .r+)rr"rJ reduce_func)rr r"rrrr rLrNr&_transform_indicatorr1rYr$_concatenate_indicatorr[ logical_notzerosr4intr3rrKr)r(rPrJ X_indicatorXcrpgenchunkrkr0rlrmrnror)s`` @@@@@@r* transformzKNNImputer.transforms T001 $  +     / D//0%% %% g248 vvd1j=)*''%&1zk>"q*}% 71"kB B..am)<)@)@a)@)HINN:6xx #6 (* /2G2G2J(K _%3 .3 .l) oq ! KK;;../%   E    # #B!"Bq:+~ 1j=!Bw-b+>>r+ctt|dt||}||j}|j||S)aGet output feature names for transformation. Parameters ---------- input_features : array-like of str or None, default=None Input features. - If `input_features` is `None`, then `feature_names_in_` is used as feature names in. If `feature_names_in_` is not defined, then the following input feature names are generated: `["x0", "x1", ..., "x(n_features_in_ - 1)"]`. - If `input_features` is an array-like, then `input_features` must match `feature_names_in_` if `feature_names_in_` is defined. Returns ------- feature_names_out : ndarray of str objects Transformed feature names. n_features_in_)rrrN(_concatenate_indicator_feature_names_out)r(input_featuresnamess r*get_feature_names_outz KNNImputer.get_feature_names_outs>( ./0~Ft//0<rsD0+*#*BB  CTCTr+