案例:缺失值填充 | 学习笔记

简介: 快速学习案例:缺失值填充

开发者学堂课程【人工智能必备基础:概率论与数理统计:案例:缺失值填充】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/545/detail/7439


案例:缺失值填充

内容介绍:

一、 缺失值处理

 

一、 缺失值处理

第一种方法:将缺失值去掉

第二种方法:将数据中的中位数或均值、众数等作为缺失值

第三种方法:有均值有数据,先构建回归模型,那其他变量当成特征,当前缺失的表示预测结果。有存在的来构建数据集,缺失的需预测。

先指定风格 sns. set(style = " ticks“) ,碰到陌生的需上官网查找。一下的库是在进行缺失值的展示。黑的代表数值,白的代表缺失值。

Missingnno  PYpi v0.4.0 python3.4+  status stable license MIT
Messy datasets? Missing values? missingno provides a small toolsetof flexible and easy-to-use missing data visualizationsand utilities that allows you to get a quick visual summary of the completeness (or lack thereof) of your dataset. Just pipinstall missingno to get started.
Quickstart   lbunch bnder
This quickstart uses a sample of the NYPD Motor Vehicle CollisionsDataset dataset. To get the data yourself, run thefollowing on your command line:
$ pip install quilt
$ quilt install ResidentMario/missingno data
Then to load the data into memory:
>>> from quilt.data. ResidentMario import missingno data» collisions = missingno data.nyc injurious collisions()» collisions = collisions.replace("nan", np,nan)The rest of this walkthrough will draw from this collisions dataset. I additionally define nullity to mean whether aparticular variable is filled in or not.
Matrix
The msno.matrix nullity matrix is a data-dense display which lets you quickly visually pick out patterns indata completion.
>>> import missingno as msno
>>> xmatplotlib inline
>>> -sno,matrix(collisions. sample(250))

image.pngAt a glance, date, time, the distribution of injuries, and the contribution factorof the first vehicle appear to be completelypopulated, while geographic information seems mostly complete, but spottier.
The sparkline at right summarizes the general shape of the data completeness and points out the maximum and minimumrows.
This visualization willcomfortably accommodate upto 50 labelled variables Past tha range labels beginto overlap orbecome unreadable,and by default large displays omit them
If you are working with time series data, you canspecifya periodity using the fre keyword parametern
>>> null_pattern=(np .random.random(1000).reshape((50,20))>0.5).astype(bool)
>>>null pattern - pd.DataFrame(null_pattern).replace({Faise!None})
msno.matrix(null_pattern.set_index(pd.period_range(‘1/1/2011',’2/1/2015’,freq=’M’) ,freq=’ BQ’

缺失值处理

In [39]:# missing values?
sns. set(style = " ticks“)
msno. matrix (data)Out[39]: <matplotlib. axes._ subplots. AxesSubplot at 0x218b0055f60>

image.png

缺失值少的,直接将缺失值去掉。

normalized-losses 缺失比较严重

In (40]: #missing values in normalied-losses

data[pd. isnull (data[‘ normalized-losses’])]. head()

Out 40 :

image.png

In (41]: sns. set(style . "ticks “)
plt. figure(figsize = (12, 5))

c =’#366DE8’
# ECDF
plt. subplot(121)
cdf = ECDF (data[ ‘normalized- losses’ )
plt. plot(cdf.x, cdf.y, label =statmodels", color = c);

plt. xlabel( ‘normalized losses’);plt. ylabel(‘ECDF’);
# overall distribution
plt. subplot (122)
plt. hist (data[‘normalized-losses’ ]. dropna(),
bins = int (np. sqrt(len (data[‘normalized-losses’ ]))),

color = c):

image.png

可以发现 80% 的 normalized losses 是低于 200 并且绝大多数低于 125.
一个基本的想法就 是用中位数来进行填充, 但是我们得来想一 想,这个特 征跟哪些因素可能有关呢?应该是保险的情况吧,所以我们可以分组来进行填充这样会更精确一些。

首先来看一下对于不同保险情况的统计指标:
In [42]: data. groupby(‘symboling’)[‘ normalized-losses ‘]. describe()
Out[42] :

image.png

In [43]: #replacing
data: datp. dropna(subset . [‘price’, ‘bore’,’stroke’,’ peak-rpm’,’horsepower’,’num-of doors’ ]
data[‘normalized-losses’] =data. groupby ( ‘symboling’ )[ ‘normalized-losses’]. transform(lambda x: x. fillna(x. mean()))

print(‘ In total:’,data. shape)
data. head ()
In total: (193, 26)
0ut(43] :

image.png

相关文章
|
传感器 数据采集 供应链
港口智能化,我们这样做!
港口智能化,我们这样做!
655 0
港口智能化,我们这样做!
|
7月前
|
SQL 数据采集 资源调度
【SQL 周周练】爬取短视频发现数据缺失,如何用 SQL 填充
爬虫爬取抖音和快手的短视频数据时,如果遇到数据缺失的情况,如何使用 SQL 语句完成数据的补全。
194 5
|
5月前
|
存储 安全 文件存储
改造旧手机,秒变家庭存储服务器
本文介绍了如何利用旧手机安装AList Flutter搭建内网服务器,以替代传统云存储和NAS。方案兼顾文件安全与下载速度,且成本低廉,适合个人用户。内容包括所需工具、安装步骤及基础配置,帮助用户快速实现本地化文件管理。后续还将介绍如何实现公网访问。
1077 1
|
9月前
|
安全 固态存储 文件存储
Windows 7纯净版重装教程|附微软原版镜像下载+驱动安装避坑技巧
本文详细介绍如何安全、高效地重装电脑系统,解决蓝屏、崩溃等问题。基于10年经验,涵盖从官方镜像获取、启动盘制作、数据备份到系统部署的全流程,并针对老旧机型优化。提供驱动一键安装工具和系统激活指南,确保无后门风险。文中还列出常见问题解决方案及操作禁忌,帮助用户顺利完成系统重装,让电脑重获新生。建议收藏并转发给有需要的朋友,欢迎留言咨询疑难问题。
17209 3
|
存储 安全 开发工具
掌握这5个Git高级命令,让你的开发效率飞速提升!
掌握这5个Git高级命令,让你的开发效率飞速提升!
|
Shell Android开发 C++
Android通过JNI操作GPIO
Android通过JNI操作GPIO
849 0
Android通过JNI操作GPIO
|
JavaScript iOS开发 开发者
pnpm的安装与配置(Windows/macOS)
pnpm的安装与配置(Windows/macOS)
3915 0
UE5ControlRig脚部IK匹配地面(FootIK)的简易实现
UE5ControlRig脚部IK匹配地面(FootIK)的简易实现
613 0
UE5ControlRig脚部IK匹配地面(FootIK)的简易实现
|
存储 人工智能 自然语言处理
云存储,为 AI 创新提速
面向 AI 时代的云存储,必须要服务于数据全生命周期,贯穿 AI 业务全流程,在数据准备、模型训练与部署、应用与内容生成、内容分发与协作每一个关键环节,提供稳定、安全、高性能、低成本的存储能力。
8869 2
云存储,为 AI 创新提速
|
Java
springboot集成mqtt
springboot集成mqtt
998 0