热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

KaggelHousePrice数据预处理及其可视化

参考原文链接https:www.kaggle.compmarcelinocomprehensive-data-exploration-with-python数据预处理源码

参考原文链接 https://www.kaggle.com/pmarcelino/comprehensive-data-exploration-with-python

数据预处理源码(详细注释)git 地址:

https://github.com/xuman-Amy/kaggel

引入要用的包

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns #统计绘图 from sklearn.preprocessing import StandardScaler
from scipy.stats import norm
from scipy import stats #统计import warnings
warnings.filterwarnings('ignore')
#画图直接显示
%matplotlib inline

加载数据 ——训练集 测试集 train test

#bring in the six packs
df_train = pd.read_csv('G:Machine learning\\kaggel\\house prices\\train.csv')
df_test = pd.read_csv('G:Machine learning\\kaggel\\house prices\\test.csv')

 粗略查看数据各字段 利用常识简单分析之间的联系

#check the decoration
#数据.columns 各列名称 分析有哪些数据,可以将数据分为numerical (数值型)和categorical(类别型)
df_train.columns

 

1、分析【SalePrice】 

 

#describe函数用来数据的快速统计汇总
df_train['SalePrice'].describe()

可以看出min大于0,所以不用担心0的问题了

用直方图看下数据分布

#seaborn 用法 https://zhuanlan.zhihu.com/p/24464836
#seaborn的displot()集合了matplotlib的hist()与核函数估计kdeplot的功能,
#增加了rugplot分布观测条显示与利用scipy库fit拟合参数分布的新颖用途sns.distplot(df_train['SalePrice'])

 

【峰值和偏态】

#show skewness and Kurtosis 偏态和峰度
print("Skewness : %f " % df_train['SalePrice'].skew())
print("Kurtosis : %f " % df_train['SalePrice'].kurt())

 

【分析 与价钱有关的特征值】

分析价钱可能会与'GrLivArea' and 'TotalBsmtSF'.  'OverallQual' and 'YearBuilt'. 有关

分别查看关系

【 GrLivArea】

#scatter plot Grlivearea / SalePricevar = 'GrLivArea'
#pd.concat 函数 可以将数据根据不同的轴作简单的融合 axis = 0-->代表行 axis = 1 --> 代表列data = pd.concat([df_train['SalePrice'],df_train[var]],axis = 1)
data.plot.scatter(x = var, y = 'SalePrice',ylim = (0,800000));

看起来 二者呈线性分布

【TotalBsmtSF】

#scatter plot saleprice / totalbsmtsf
var = 'TotalBsmtSF'
data = pd.concat([df_train['SalePrice'],df_train[var]],axis = 1)
data.plot.scatter(x = var, y = 'SalePrice',ylim = (0,800000))

 

 

看起来也是很强的 线性关系

【OverallQual 】

#box plot overallqual / saleprice
var = 'OverallQual'
data = pd.concat([df_train['SalePrice'],df_train['OverallQual']],axis = 1)
f,ax = plt.subplots(figsize = (8,6)) #subplots 创建一个画像(figure)和一组子图(subplots)。
fig = sns.boxplot(x = var,y = 'SalePrice',data = data)
fig.axis (ymin = 0,ymax = 800000)

可以看出整体建筑质量越好,价钱越高

 

【YearBuilt】

#boxplot saleprice / yearbuilt
var = 'YearBuilt'
data = pd.concat([df_train['SalePrice'],df_train['YearBuilt']],axis = 1)
f, ax = plt.subplots(figsize = (25,10))
fig = sns.boxplot(x = var,y = 'SalePrice',data = data)
fig.axis(ymin = 0,yamx = 800000)
plt.xticks(rotation = 90) #x轴标签 转90度

 

 

 

虽然没有太大关系,但是新建的价钱稍微有高价钱趋势

 

总体来说,价钱与grlivarea totalbsmtsf有很强的线性关系

与 yearbuilt没有太大关系,与overallqua有较弱关系

2. 具体分析问题

原文作者说要“keep calm and  work smart“  哈哈哈 跟着大佬继续学习

【相关矩阵】

#correlation matrix 相关矩阵
corrmat = df_train.corr()
f ,ax = plt.subplots(figsize = (12,9))
sns.heatmap(corrmat,vmax = 8,square = True,cmap = 'hot')

 

我显示的效果不如原文,也没找到原文的效果是用的什么参数

原文图如下:

 

可以看出  

可以看出很多特征之间有很强的相关性

【SalePrice 与其他特征之间的相关性 Top 10】

#saleprice correlation matrix
k = 10
cols = corrmat.nlargest(k,'SalePrice')['SalePrice'].index #取出与saleprice相关性最大的十项
cm = np.corrcoef(df_train[cols].values.T) #相关系数
sns.set(font_scale = 1.25)
hm = sns.heatmap(cm,cbar = True,annot = True,square = True ,fmt = '.2f',annot_kws = {'size': 10},yticklabels = cols.values,xticklabels = cols.values)
plt.show()

从相关矩阵图 top10 中可以看出,OverallQual', 'GrLivArea' and 'TotalBsmtSF'与saleprice有很强相关性

garagecar与garagearea 相关性差不多,取其一,取garagecars

TotalBsmtSF' and '1stFloor' 有很强相关性,取其一,取TotalBsmtSF' 

同理,'TotRmsAbvGrd' and 'GrLivArea',取grlivarea

【saleprice 与相关变量的散点图】

#scatterplot
sns.set()
cols = ['SalePrice','OverallQual','GrLivArea','GarageCars','TotalBsmtSF','FullBath','YearBuilt']
sns.pairplot(df_train[cols],size = 2.5)

3.缺失值处理

了解缺失值是否重要

了解缺失值缺失是随机的还是有规律有原因的

【查看各字段的缺失 以百分比的形式显示 】

#missing data
#pandas.isnull() 判断数据是否为空 返回false / true
#sort_values()
total = df_train.isnull().sum().sort_values(ascending = False)
percent = (df_train.isnull().sum() / df_train.isnull().count()).sort_values(ascending = False)
missing_data = pd.concat([total,percent],axis = 1,keys = ['Total','Percent'])
missing_data.head(20)

对缺失数据进行分析:

对于缺失大于数据量的15%的字段,可以考虑删掉这一字段()

对于GarageXX项可忽略,因为已经用garageCars代表

同理,BsmtXX不用管,有TotalBsmtSF代表

MasVnrArea' and 'MasVnrType 不重要,可删除 

只剩下electric 缺失一项,可以删掉缺失这一项的 

【删除无用的缺失项】

df_train = df_train.drop(missing_data[(missing_data['Total'] > 1)].index,axis = 1)

df_train = df_train.drop(df_train.loc[df_train['Electrical'].isnull()].index)

df_train.isnull().sum().max()

d到这一步 没有缺失项了~~~~

接下来 ,另一个比较重要的是outliars

 

【单变量 离群值 Univariate analysis】

 

 

 

【数据的标准化 】

利用StandardScaler 

#standardizing data --> converting data values to have mean of 0 and standard deviation of 1
#http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html
# fit : compute mean and std deviation
#transform : Perform standardization by centering and scaling
#np.newaxis 增加新维度
#argsort() Returns the indices that would sort an array.将x中的元素从小到大排列,提取其对应的index(索引),然后输出

saleprice_scaled = StandardScaler().fit_transform(df_train['SalePrice'][:,np.newaxis])
low_range = saleprice_scaled[saleprice_scaled[:,0].argsort()][:10]
high_range = saleprice_scaled[saleprice_scaled[:,0].argsort()][-10:]
print('outer range(low) of the distribution :','\n',low_range)
print ('outer range (high) of the distribution :','\n',high_range)
sns.distplot(saleprice_scaled)

【双变量数据分析  Bivariate analysis】

(GrLivArea - SalePrice)

var = 'GrLivArea'
data = pd.concat([df_train['SalePrice'],df_train[var]],axis = 1)
data.plot.scatter(x = var, y = 'SalePrice',ylim =(0,800000))

删除这两个不符合规律的值。

#delete point
df_train.sort_values(by = 'GrLivArea', ascending = False)[:2]
df_train = df_train.drop(df_train[df_train['Id'] == 1299 ].index)
df_train = df_train.drop(df_train[df_train['Id'] == 524].index)

 

作者对于数据处理的见解如下:
5 Getting hard core

According to Hair et al. (2013), four assumptions should be tested:

Normality (正态性)- When we talk about normality what we mean is that the data should look like a normal distribution. This is important because several statistic tests rely on this (e.g. t-statistics). In this exercise we'll just check univariate normality for 'SalePrice' (which is a limited approach). Remember that univariate normality doesn't ensure multivariate normality (which is what we would like to have), but it helps. Another detail to take into account is that in big samples (>200 observations) normality is not such an issue. However, if we solve normality, we avoid a lot of other problems (e.g. heteroscedacity) so that's the main reason why we are doing this analysis.

Homoscedasticity(方差齐性) - I just hope I wrote it right. Homoscedasticity refers to the 'assumption that dependent variable(s) exhibit equal levels of variance across the range of predictor variable(s)' (Hair et al., 2013). Homoscedasticity is desirable because we want the error term to be the same across all values of the independent variables.

Linearity(线性)- The most common way to assess linearity is to examine scatter plots and search for linear patterns. If patterns are not linear, it would be worthwhile to explore data transformations. However, we'll not get into this because most of the scatter plots we've seen appear to have linear relationships.

Absence of correlated errors (无相关错误) - Correlated errors, like the definition suggests, happen when one error is correlated to another. For instance, if one positive error makes a negative error systematically, it means that there's a relationship between these variables. This occurs often in time series, where some patterns are time related. We'll also not get into this. However, if you detect something, try to add a variable that can explain the effect you're getting. That's the most common solution for correlated errors.

 

对于正态性,可以用直方图 正太概率分布图 分析

【SalePrice】

# in the search for normality
#histogram and normal probability plot 直方图和正态概率图
sns.distplot(df_train['SalePrice'],fit = norm) #fit 控制拟合的参数分布图形
fig = plt.figure()
# probplot :Calculate quantiles for a probability plot, and optionally show the plot. 计算概率图的分位数
res = stats.probplot(df_train['SalePrice'],plot = plt)

图中看出 呈现正偏态, 线性也不好,进行log转换 

df_train['GrLivArea'] = np.log(df_train['GrLivArea'])
sns.distplot(df_train['GrLivArea'],fit = norm)
fig = plt.figure()
res = stats.probplot(df_train['GrLivArea'],plot = plt)

 

呈现出了很好的正态性 以及线性

依次对每个选出来的特征值处理

【TotalBsmtSF】

#TotalBsmtSF
sns.distplot(df_train['TotalBsmtSF'],fit = norm)
fig = plt.figure()
res = stats.probplot(df_train['TotalBsmtSF'],plot = plt)

 

看出 有0值,不能直接进行log转换, 把大于0的筛选出来然后log转换

 

log处理后

#create column for new varible
#if area > 0 ,it gets 1; for area == 0 it gets 0

df_train['HasBsmt'] = pd.Series(len(df_train['TotalBsmtSF']),index = df_train.index)
df_train['HasBsmt'] = 0
df_train.loc[df_train['TotalBsmtSF'] > 0 ,'HasBsmt'] = 1
df_train.loc[df_train['HasBsmt'] == 0 ,'TotalBsmtSF'] = np.log(df_train['TotalBsmtSF'])

sns.distplot(df_train[df_train['TotalBsmtSF']>0]['TotalBsmtSF'],fit = norm)
fig = plt.figure()
res = stats.probplot(df_train[df_train['TotalBsmtSF'] > 0 ]['TotalBsmtSF'],plot = plt)


【方差齐性 】

 

The best approach to test homoscedasticity for two metric variables is graphically. Departures from an equal dispersion are shown by such shapes as cones (small dispersion at one side of the graph, large dispersion at the opposite side) or diamonds (a large number of points at the center of the distribution).

 

要么是像conic 锥形 ,少量数据分散在一侧,大量数据分散在另一侧,

要么像钻石,集中分散在中间

查看两个变量之间的方差齐性

'SalePrice' and 'GrLivArea'...

#scatter plot
plt.scatter(df_train['SalePrice'],df_train['GrLivArea'])

SalePrice' with 'TotalBsmtSF'.

plt.scatter(df_train[df_train['TotalBsmtSF']>0] ['TotalBsmtSF'],df_train[df_train['TotalBsmtSF'] > 0]
['SalePrice'])

最后 dummy variables

进行非数转换,数据全是数值型

#convert categorical variable into dummy
df_train = pd.get_dummies(df_train)

 

数据预处理至此。博客就是看的原文基本copy下来,跑了一遍,也学到了很多数据预处理 可视化的技巧,希望以后能有更大进步!

 

 


推荐阅读
  • 2018年人工智能大数据的爆发,学Java还是Python?
    本文介绍了2018年人工智能大数据的爆发以及学习Java和Python的相关知识。在人工智能和大数据时代,Java和Python这两门编程语言都很优秀且火爆。选择学习哪门语言要根据个人兴趣爱好来决定。Python是一门拥有简洁语法的高级编程语言,容易上手。其特色之一是强制使用空白符作为语句缩进,使得新手可以快速上手。目前,Python在人工智能领域有着广泛的应用。如果对Java、Python或大数据感兴趣,欢迎加入qq群458345782。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 第四章高阶函数(参数传递、高阶函数、lambda表达式)(python进阶)的讲解和应用
    本文主要讲解了第四章高阶函数(参数传递、高阶函数、lambda表达式)的相关知识,包括函数参数传递机制和赋值机制、引用传递的概念和应用、默认参数的定义和使用等内容。同时介绍了高阶函数和lambda表达式的概念,并给出了一些实例代码进行演示。对于想要进一步提升python编程能力的读者来说,本文将是一个不错的学习资料。 ... [详细]
  • 本文介绍了Python语言程序设计中文件和数据格式化的操作,包括使用np.savetext保存文本文件,对文本文件和二进制文件进行统一的操作步骤,以及使用Numpy模块进行数据可视化编程的指南。同时还提供了一些关于Python的测试题。 ... [详细]
  • 如何使用Python从工程图图像中提取底部的方法?
    本文介绍了使用Python从工程图图像中提取底部的方法。首先将输入图片转换为灰度图像,并进行高斯模糊和阈值处理。然后通过填充潜在的轮廓以及使用轮廓逼近和矩形核进行过滤,去除非矩形轮廓。最后通过查找轮廓并使用轮廓近似、宽高比和轮廓区域进行过滤,隔离所需的底部轮廓,并使用Numpy切片提取底部模板部分。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 怀疑是每次都在新建文件,具体代码如下 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了如何使用python从列表中删除所有的零,并将结果以列表形式输出,同时提供了示例格式。 ... [详细]
  • 前言:拿到一个案例,去分析:它该是做分类还是做回归,哪部分该做分类,哪部分该做回归,哪部分该做优化,它们的目标值分别是什么。再挑影响因素,哪些和分类有关的影响因素,哪些和回归有关的 ... [详细]
  • Allegro总结:1.防焊层(SolderMask):又称绿油层,PCB非布线层,用于制成丝网印板,将不需要焊接的地方涂上防焊剂.在防焊层上预留的焊盘大小要比实际的焊盘大一些,其差值一般 ... [详细]
  • pythonMatplotlib(二)
    Matplotlib+pandas作图一、对csv文件进行提取ruixi.csv对上述表格进行提取并做图画出图像二、对.xlsx进行提取:rui ... [详细]
  • 动量|收益率_基于MT策略的实战分析
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了基于MT策略的实战分析相关的知识,希望对你有一定的参考价值。基于MT策略的实战分析 ... [详细]
  • 人脸检测 pyqt+opencv+dlib
    一、实验目标绘制PyQT界面,调用摄像头显示人脸信息。在界面中,用户通过点击不同的按键可以实现多种功能:打开和关闭摄像头, ... [详细]
author-avatar
achih
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有