Python——sklearn包中数据标准化以及缺失值的处理函数汇集

  1. sklearn.preprocessing:
    博主写的很详细:
    https://blog.csdn.net/pipisorry/article/details/52247679
    在这里插入图片描述

The preprocessing module further provides a utility class StandardScaler that implements the Transformer API to computethe mean and standard deviation on a training set so as to beable to later reapply the same transformation on the testing set.This class is hence suitable for use in the early steps of a sklearn.pipeline.Pipeline:

scaler = preprocessing.StandardScaler().fit(X)
scaler
StandardScaler(copy=True, with_mean=True, with_std=True)

scaler.mean_
array([ 1. …, 0. …, 0.33…])

scaler.scale_
array([ 0.81…, 0.81…, 1.24…])

scaler.transform(X)
array([[ 0. …, -1.22…, 1.33…],
[ 1.22…, 0. …, -0.26…],
[-1.22…, 1.22…, -1.06…]])
The scaler instance can then be used on new data to transform it thesame way it did on the training set:

scaler.transform([[-1., 1., 0.]])
array([[-2.44…, 1.22…, -0.26…]])


作者:-柚子皮-
来源:CSDN
原文:https://blog.csdn.net/pipisorry/article/details/52247679
版权声明:本文为博主原创文章,转载请附上博文链接!

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

二值化 Binarization

sklearn.preprocessing.Binarizer

binarizer=preprocessing.Binarizer().fit(x)# here fit does nothing
binarizer
Out[136]: Binarizer(copy=True, threshold=0.0)
binarizer.transform(x)
Out[137]:
array([[1, 0, 1],
[1, 0, 0],
[0, 1, 0]])
x
Out[138]:
array([[ 1, -1, 2],
[ 2, 0, 0],
[ 0, 1, -1]])
在这里插入图片描述

SimpleImputer的用法见sklearn相关的documentation:
https://scikit-learn.org/stable/modules/generated/sklearn.impute.SimpleImputer.html#sklearn.impute.SimpleImputer
在这里插入图片描述
SimpleImputer fills value for each feature也即对每列的空值进行填充,故而和老版本(即将be deprecated in newer version)的Imputer相比,没有了axis=0(即逐行)的参数的位置。

import numpy as np
from sklearn.impute import SimpleImputer

#类似于填补模型imp造出来
imp=SimpleImputer(missing_values=np.nan,strategy='mean')
imp.fit([[1,2],[np.nan,3],[7,6]])

Out[163]:
SimpleImputer(copy=True, fill_value=None, missing_values=nan, strategy=‘mean’,
verbose=0)

x=[[np.nan,2],[6,np.nan],[7,6]]
#用imp模型填补并且转换x
print(imp.transform(x))

[[4. 2. ]
[6. 3.66666667]
[7. 6. ]]

fit_transform与transform的区别见博文:
https://blog.csdn.net/quiet_girl/article/details/72517053

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值