当前位置:网站首页>任务五 处理连续型数据
任务五 处理连续型数据
2022-08-09 00:56:00 【Heihei_study】
目录
函数sklearn.preprocessing.Binarizer
函数preprocessing.KBinsDiscretizer
函数sklearn.preprocessing.Binarizer
根据阈值将数据二值化(将特征值设置为0或1),用于处理连续型变量。大于阈值的值映射为1,而小于或等于阈值的值映射为0。
sklearn.preprocessing.Binarizer(threshold=0.0, copy=True)X = [[ -3., 5., 15 ],
[ 0., 6., 14 ],
[ 6., 3., 11 ]]print(type(X)) ![]()
test=Binarizer(threshold=6).fit_transform(X)
print(test) 
函数preprocessing.KBinsDiscretizer
class sklearn.preprocessing.KBinsDiscretizer(n_bins=5,
encode='onehot',
strategy='quantile'
)
encode="ordinal"




encode="onehot"
X = np.array([[ -3., 5., 15 ],
[ 0., 6., 14 ],
[ 6., 3., 11 ]])
X=X.reshape(-1,1)
print(X)
est = KBinsDiscretizer(n_bins=3,encode='onehot').fit(X)
print(est.fit_transform(X))
print(est.fit_transform(X).toarray())

处理糖尿病数据集
导入数据
函数read_excel
data = pd.read_excel('./input/diabetes_missing_value.xlsx')
缺失值统计
print(data.isnull().sum())

缺失值填补
主要是两种方式
缺失值较少时直接用中值填补
data["plas"] = data["plas"].fillna(data["plas"].median())缺失值较多时,用热力图查看与该特征关联度较大的几个特征,根据这几个特征求取中值,没有时求取直接使用整段中值
#skin的缺失值比较多 通过热力图可以看出skin和mass、pres、plas三种数据相关性较大
# Filling missing value of skin
index_NaN_age = list(data["skin"][data["skin"].isnull()].index)
#注意这里的空置表示方式:dataset['skin'][dataset.skin.isnull()].index,属性后面紧跟着一个[筛选条件].index,返回的是raw序号.
for i in index_NaN_age :
skin_med = data["skin"].median()#非空skin 值的中位数
skin_pred = data["skin"][((data['mass'] == data.iloc[i]["mass"]) & (data['pres'] == data.iloc[i]["pres"]) & (data['plas'] == data.iloc[i]["plas"]))].median()#在所有的记录中,寻找与Age为空值记录,SibSp\Parch\Pclass都想同的记录,例如有5条,取这5条记录中Age的中位数填充空的Age值
if not np.isnan(skin_pred) :
data['skin'].iloc[i] = skin_pred
else :
data['skin'].iloc[i] = skin_med
标签转换
标签转换 将class里的b'tested_positive'转为1 negative转为0
data.iloc[:,-1]=LabelEncoder().fit_transform(data.iloc[:,-1])处理连续型数据
preg怀孕次数
X=data.iloc[:,0].values.reshape(-1,1)
使用 KBinsDiscretizer
test=KBinsDiscretizer(n_bins=3,encode='onehot',strategy='uniform').fit_transform(X).toarray()
newdata=pd.concat([pd.DataFrame(test),data],axis=1)
print(newdata.head())

newdata.drop(["preg"],axis=1,inplace=True)
newdata.columns=["preg_small","preg_middle","preg_large","plas","pres","skin","insu","mass","pedi","age","class"]
print(newdata.head())
边栏推荐
猜你喜欢

微信企业号开发之获取AccessToken

神经网络基本原理

在Ubuntu/Linux环境下使用MySQL:修改数据库sql_mode,可解决“this is incompatible with sql_mode=only_full_group_by”问题

Using MySQL in Ubuntu/Linux environment: Solve the problem of com.mysql.jdbc.PacketTooBigException: Packet for query is too large

容器运维平台的故障处理-1

4-10 Matplotlib 多图布局

在Windows环境下使用MySQL:自动定时备份

4-2 Matplotlib库 基本使用(绘制折线图)

Region Proposal Network(RPN)

网络宽度扩充--Inception v1-v4,xception变式学习记录
随机推荐
经典卷积神经网络ZFNet--解卷积可视化
Rollup 编译资源离不开 plugin
Network In Network学习记录
登录退出功能
全新Swagger3.0教程,OAS3快速配置指南,实现API接口文档自动化!
网络宽度扩充--Inception v1-v4,xception变式学习记录
观察者模式
轻量级神经网络SqueezeNext--考虑硬件提速
在Ubuntu/Linux环境下使用MySQL:解决com.mysql.jdbc.PacketTooBigException: Packet for query is too large的问题
在vscode中编辑、编译、下载Keil工程
微信企业号开发之接收响应消息
tf.pad()--填充操作
C语言-大端存储和小端存储
动态style定义背景渐变
【科研-学习-pytorch】2-线性回归
轻量级网络ESPNet系列 空洞卷积简介
易周金融分析 | 互联网系小贷平台密集增资;上半年银行理财子公司综合评价指数发布
LeetCode精选200道--字符串篇
Use jdbc to handle MySQL's utf8mb4 character set (transfer)
轻量化网络ChannelNet--channel-wize Conv在channel维度卷积