数据清洗之 异常值处理

异常值处理

  • 指那些偏离正常范围的值,不是错误值
  • 异常值出现频率较低,但又会对实际项目分析造成偏差
  • 异常值一般用过箱线图法(分位差法)或者分布图(标准差法)来判断
  • 异常值检测可以使用均值的二倍标准差范围,也可以使用上下4分位数差方法
  • 异常值往往采取盖帽法或者数据离散化
import pandas as pd
import numpy as np
import os
os.getcwd()
'D:\\Jupyter\\notebook\\Python数据清洗实战\\数据清洗之数据预处理'
os.chdir('D:\\Jupyter\\notebook\\Python数据清洗实战\\数据')
df = pd.read_csv('MotorcycleData.csv', encoding='gbk', na_values='Na')
def f(x):
    if '$' in str(x):
        x = str(x).strip('$')
        x = str(x).replace(',', '')
    else:
        x = str(x).replace(',', '')
    return float(x)
df['Price'] = df['Price'].apply(f)
df['Mileage'] = df['Mileage'].apply(f)
df.head(5)
ConditionCondition_DescPriceLocationModel_YearMileageExterior_ColorMakeWarrantyModel...Vehicle_TitleOBOFeedback_PercWatch_CountN_ReviewsSeller_StatusVehicle_TileAuctionBuy_NowBid_Count
0Usedmint!!! very low miles11412.0McHenry, Illinois, United States2013.016000.0BlackHarley-DavidsonUnspecifiedTouring...NaNFALSE8.1NaN2427Private SellerClearTrueFALSE28.0
1UsedPerfect condition17200.0Fort Recovery, Ohio, United States2016.060.0BlackHarley-DavidsonVehicle has an existing warrantyTouring...NaNFALSE10017657Private SellerClearTrueTRUE0.0
2UsedNaN3872.0Chicago, Illinois, United States1970.025763.0Silver/BlueBMWVehicle does NOT have an existing warrantyR-Series...NaNFALSE100NaN136NaNClearTrueFALSE26.0
3UsedCLEAN TITLE READY TO RIDE HOME6575.0Green Bay, Wisconsin, United States2009.033142.0RedHarley-DavidsonNaNTouring...NaNFALSE100NaN2920DealerClearTrueFALSE11.0
4UsedNaN10000.0West Bend, Wisconsin, United States2012.017800.0BlueHarley-DavidsonNO WARRANTYTouring...NaNFALSE10013271OWNERClearTrueTRUE0.0

5 rows × 22 columns

# 对价格异常值处理
# 计算价格均值
x_bar = df['Price'].mean()
# 计算价格标准差
x_std = df['Price'].std()
# 异常值上限检测
any(df['Price'] > x_bar + 2 * x_std)
True
# 异常值下限检测
any(df['Price'] < x_bar - 2 * x_std)
False
# 描述性统计
df['Price'].describe()
count      7493.000000
mean       9968.811557
std        8497.326850
min           0.000000
25%        4158.000000
50%        7995.000000
75%       13000.000000
max      100000.000000
Name: Price, dtype: float64
# 25% 分位数
Q1 = df['Price'].quantile(q = 0.25)
# 75% 分位数
Q3 = df['Price'].quantile(q = 0.75)
# 分位差
IQR = Q3 - Q1
any(df['Price'] > Q3 + 1.5 * IQR)
True
any(df['Price'] < Q1 - 1.5 * IQR)
False
import matplotlib.pyplot as plt
%matplotlib inline
df['Price'].plot(kind='box')
<matplotlib.axes._subplots.AxesSubplot at 0x11ddad20ac8>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PioEYXZs-1587367435767)(output_21_1.png)]

# 设置绘图风格
plt.style.use('seaborn')
# 绘制直方图
df.Price.plot(kind='hist', bins=30, density=True)
# 绘制核密度图
df.Price.plot(kind='kde')
# 图形展现
plt.show()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JWb6qAoD-1587367435770)(output_22_0.png)]

# 用99分位数和1分位数替换
# 计算P1和P99
P99 = df['Price'].quantile(q=0.99)
P1 = df['Price'].quantile(q=0.01)
P99
39995.32
df['Price_new'] = df['Price']
# 盖帽法
df.loc[df['Price'] > P99, 'Price_new'] = P99
df.loc[df['Price'] < P1, 'Price_new'] = P1
df[['Price', 'Price_new']].describe()
PricePrice_new
count7493.0000007493.000000
mean9968.8115579821.220873
std8497.3268507737.092537
min0.000000100.000000
25%4158.0000004158.000000
50%7995.0000007995.000000
75%13000.00000013000.000000
max100000.00000039995.320000
# df['Price_new'].plot(kind='box')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值