当前位置:网站首页>Basic method of missing data filling (1) -- k-nearest neighbors (KNN) filling
Basic method of missing data filling (1) -- k-nearest neighbors (KNN) filling
2022-06-09 02:31:00 【wendy_ ya】
install :
pip install missingpy
One 、kNN Introduce
kNNImputer Class provides the k-Nearest Neighbors(KNN) The algorithm completes the filling of missing values . The missing values of each sample are found in the training set n_neighbors The value of the nearest neighbor , Please note that , If a sample lacks multiple features , Then the sample can have multiple groups n_neighbors Neighborhood donor , It depends on the specific characteristics of the filling .
then , Each missing feature is filled in as the weighted or unweighted average of these neighbors . If donor neighbors The number of is less than n_neighbors, The average value of the training set of the feature is used for filling . Of course , The total number of samples in the training set is always greater than or equal to the number of nearest neighbors that can be used for filling . This depends on the population sample size and the number of samples excluded from the nearest neighbor calculation due to too many missing features ( from row_max_missing control )
Two 、 Code example
The following code snippet demonstrates how to replace missing values with np.nan, Use the average eigenvalue of the two nearest neighbors of the row containing the missing value :
>>> import numpy as np
>>> from missingpy import KNNImputer
>>> nan = np.nan
>>> X = [[1, 2, nan], [3, 4, 3], [nan, 6, 5], [8, 8, 7]]
>>> imputer = KNNImputer(n_neighbors=2, weights="uniform")
>>> imputer.fit_transform(X)
array([[1. , 2. , 4. ],
[3. , 4. , 3. ],
[5.5, 6. , 5. ],
[8. , 8. , 7. ]])
3、 ... and 、API example
KNNImputer Of API as follows :
KNNImputer(missing_values="NaN", n_neighbors=5, weights="uniform",
metric="masked_euclidean", row_max_missing=0.5,
col_max_missing=0.8, copy=True)
Parameters
----------
missing_values : integer or "NaN", optional (default = "NaN")
The placeholder for the missing values. All occurrences of
`missing_values` will be imputed. For missing values encoded as
``np.nan``, use the string value "NaN".
n_neighbors : int, optional (default = 5)
Number of neighboring samples to use for imputation.
weights : str or callable, optional (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 : str or callable, optional (default = "masked_euclidean")
Distance metric for searching neighbors. Possible values:
- 'masked_euclidean'
- [callable] : a user-defined function which conforms to the
definition of _pairwise_callable(X, Y, metric, **kwds). In other
words, the function accepts two arrays, X and Y, and a
``missing_values`` keyword in **kwds and returns a scalar distance
value.
row_max_missing : float, optional (default = 0.5)
The maximum fraction of columns (i.e. features) that can be missing
before the sample is excluded from nearest neighbor imputation. It
means that such rows will not be considered a potential donor in
``fit()``, and in ``transform()`` their missing feature values will be
imputed to be the column mean for the entire dataset.
col_max_missing : float, optional (default = 0.8)
The maximum fraction of rows (or samples) that can be missing
for any feature beyond which an error is raised.
copy : boolean, optional (default = True)
If True, a copy of X will be created. If False, imputation will
be done in-place whenever possible. Note that, if metric is
"masked_euclidean" and copy=False then missing_values in the
input matrix X will be overwritten with zeros.
Attributes
----------
statistics_ : 1-D array of length {
n_features}
The 1-D array contains the mean of each feature calculated using
observed (i.e. non-missing) values. This is used for imputing
missing values in samples that are either excluded from nearest
neighbors search because they have too many ( > row_max_missing)
missing features or because all of the sample's k-nearest neighbors
(i.e., the potential donors) also have the relevant feature value
missing.
Methods
-------
fit(X, y=None):
Fit the imputer on X.
Parameters
----------
X : {
array-like}, shape (n_samples, n_features)
Input data, where ``n_samples`` is the number of samples and
``n_features`` is the number of features.
Returns
-------
self : object
Returns self.
transform(X):
Impute all missing values in X.
Parameters
----------
X : {
array-like}, shape = [n_samples, n_features]
The input data to complete.
Returns
-------
X : {
array-like}, shape = [n_samples, n_features]
The imputed dataset.
fit_transform(X, y=None, **fit_params):
Fit KNNImputer and impute all missing values in X.
Parameters
----------
X : {
array-like}, shape (n_samples, n_features)
Input data, where ``n_samples`` is the number of samples and
``n_features`` is the number of features.
Returns
-------
X : {
array-like}, shape (n_samples, n_features)
Returns imputed dataset.
Reference resources :
- 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.
边栏推荐
- Comment le modèle de diffusion a - t - il commencé à surpasser le Gan dans le domaine de la génération d'images?
- 杰理之AD 值的硬件计算原理?【篇】
- 杰理之关于 SPI 主机配置参数的几个说明:【篇】
- Acwing 792 high precision subtraction
- Go to MFC from Win32
- Export related knowledge
- Flv structure description
- Jedis tool class, adapting to a single redis and redis cluster
- 编写循环何条件任务
- Interface test series - interface test practice of transfer transaction business scenarios
猜你喜欢

接口测试系列——转转交易业务场景接口测试实践
How to implement the project practice of user registration, login and logout in flask + MySQL

toggleRowSelection()失效的2個重要影響因素

21. Class E power amplifier design of ads usage record (medium)

Blue Bridge Cup_ Multiple problem_ stack_ Remainder
![[network protocol] | [01] network byte order big end and small end](/img/9f/c69293aa8983161f4733a84389a311.png)
[network protocol] | [01] network byte order big end and small end

S series · several postures for deleting folders

Navicat tool batch imports JSON format data to Doris

Implementation of UESTC daily report based on Selenium

Docker安装Redis
随机推荐
Diffusion model has been very popular in the field of image generation recently. How do you think its popularity has begun to surpass Gan?
Redis集群搭建
badlyserialize
NSSCTF prize_ p5
21. Class E power amplifier design of ads usage record (medium)
杰理之SPI主机【篇】
【无标题】
【编码推流】SRS流媒体服务器安装及使用
How can golang set its own gopath and import the definition file normally
Jerry's SPI host [chapter]
Acwing 791 high precision addition
S系列·删除文件夹的几种姿势
Doris daily function summary I
2022.6 download and installation tutorial of the latest version of mongodb
杰理之SPI 主机如何配置驱动程序?【篇】
Binary tree chain structure
Should flying books be ecological? Analyze the first independent SaaS case of all in flybook
I'm sorry if I don't understand. The final battle
What is a security tag based access control mechanism? What are the characteristics?
PHP replicated vulnerability