Python之探索式数据分析
探究数据分析的目的是为了找到有助于清理/准备/转换数据的思路,这些数据最终将用于机器学习算法/模型的建立。我们将采取以下行动:
import numpy as np
import pandas as pdimport matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inlineplt.style.use('bmh')
Seaborn是一个基于matplotlib的Python数据可视化库。它提供了一个绘制信息统计图形的高级界面。
即使是漂亮的图表bokeh。
考虑住房价格及其影响因素。
data = pd.read_csv('data/train.csv')
data.head()
以上为部分数据截图
data.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 1460 entries, 0 to 1459 Data columns (total 81 columns): Id 1460 non-null int64 MSSubClass 1460 non-null int64 MSZoning 1460 non-null object LotFrontage 1201 non-null float64 LotArea 1460 non-null int64 Street 1460 non-null object Alley 91 non-null object LotShape 1460 non-null object LandContour 1460 non-null object Utilities 1460 non-null object LotConfig 1460 non-null object LandSlope 1460 non-null object Neighborhood 1460 non-null object Condition1 1460 non-null object Condition2 1460 non-null object BldgType 1460 non-null object HouseStyle 1460 non-null object OverallQual 1460 non-null int64 OverallCond 1460 non-null int64 YearBuilt 1460 non-null int64 YearRemodAdd 1460 non-null int64 RoofStyle 1460 non-null object RoofMatl 1460 non-null object Exterior1st 1460 non-null object Exterior2nd 1460 non-null object MasVnrType 1452 non-null object MasVnrArea 1452 non-null float64 ExterQual 1460 non-null object ExterCond 1460 non-null object Foundation 1460 non-null object BsmtQual 1423 non-null object BsmtCond 1423 non-null object BsmtExposure 1422 non-null object BsmtFinType1 1423 non-null object BsmtFinSF1 1460 non-null int64 BsmtFinType2 1422 non-null object BsmtFinSF2 1460 non-null int64 BsmtUnfSF 1460 non-null int64 TotalBsmtSF 1460 non-null int64 Heating 1460 non-null object HeatingQC 1460 non-null object CentralAir 1460 non-null object Electrical 1459 non-null object 1stFlrSF 1460 non-null int64 2ndFlrSF 1460 non-null int64 LowQualFinSF 1460 non-null int64 GrLivArea 1460 non-null int64 BsmtFullBath 1460 non-null int64 BsmtHalfBath 1460 non-null int64 FullBath 1460 non-null int64 HalfBath 1460 non-null int64 BedroomAbvGr 1460 non-null int64 KitchenAbvGr 1460 non-null int64 KitchenQual 1460 non-null object TotRmsAbvGrd 1460 non-null int64 Functional 1460 non-null object Fireplaces 1460 non-null int64 FireplaceQu 770 non-null object GarageType 1379 non-null object GarageYrBlt 1379 non-null float64 GarageFinish 1379 non-null object GarageCars 1460 non-null int64 GarageArea 1460 non-null int64 GarageQual 1379 non-null object GarageCond 1379 non-null object PavedDrive 1460 non-null object WoodDeckSF 1460 non-null int64 OpenPorchSF 1460 non-null int64 EnclosedPorch 1460 non-null int64 3SsnPorch 1460 non-null int64 ScreenPorch 1460 non-null int64 PoolArea 1460 non-null int64 PoolQC 7 non-null object Fence 281 non-null object MiscFeature 54 non-null object MiscVal 1460 non-null int64 MoSold 1460 non-null int64 YrSold 1460 non-null int64 SaleType 1460 non-null object SaleCondition 1460 non-null object SalePrice 1460 non-null int64 dtypes: float64(3), int64(35), object(43) memory usage: 924.0+ KB
从这些信息中我们已经可以看出,有些数据将与我们的目的无关,因为它包含了太多的NaN值(Drummer、Alley和PoolQC)。此外,即使没有这些数据,表中也有足够的证据进行分析,所以我们可能不会考虑其中的一些数据。让我们去掉变量Id和含有30%以上NaN值的变量。
# pandas.DataFrame.count() does not include NaN values
data_without_nan = data[[column for column in data if data[column].count() / len(data) >= 0.3]]
data_without_nan = data_without_nan.drop(columns=['Id'])dropped_columns = [col for col in data.columns if col not in data_without_nan.columns ]
print("List of dropped columns: ", dropped_columns)
现在我们来看看房价的分布情况。
data_without_nan['SalePrice'].describe()
# histogram and normal probability plot
from scipy import stats
from scipy.stats import normsns.distplot(data_without_nan['SalePrice'], fit=norm);
fig = plt.figure()
res = stats.probplot(data_without_nan['SalePrice'], plot=plt)
我们可以看到,价格是向右倾斜的,而且有一些排放量在~500000以上。最终,我们将需要摆脱它们,以获得自变量('SalePrice')的正态分布。
右斜分布有一个长长的右尾。右斜分布也叫正斜分布,因为数值线上有一个正方向的长尾。均值和中位数也是在峰值的右侧。
在偏移量为正值的情况下,数据的对数化效果非常好。
此外,你还可以使用Box-Cox转换。
# transformed histogram and normal probability plot
sns.distplot(np.log(data_without_nan['SalePrice']), fit=norm);
fig = plt.figure()
res = stats.probplot(np.log(data_without_nan['SalePrice']), plot=plt)
我们来看看其他星座的分布情况。要做到这一点,您必须首先只选择数字数据。
list(set(data_without_nan.dtypes.tolist()))
data_without_nan_num = data_without_nan.select_dtypes(include = ['float64', 'int64'])
data_without_nan_num.head()
以下为部分结果截图
现在让我们把它们都建起来。
data_without_nan_num.hist(figsize=(16, 20), bins=50, xlabelsize=8, ylabelsize=8);
# ; avoid having the matplotlib verbose informations
如 "1stFlrSF"、"TotalBsmtSF"、"LotFrontage"、"GrLiveArea "等功能......。图形上与 "SalePrice "变量的分布相似。
相关性
让我们试着找到与 "SalePrice "相关的标志。
corr_data = data_without_nan_num.corr()['SalePrice'][:-1]
correlated_features_list = corr_data[abs(corr_data) > 0.5].sort_values(ascending=False)
print("There are {} strongly correlated values with SalePrice:\n{}".format(len(correlated_features_list), correlated_features_list))
现在,我们有了一份与住房成本相关的变量清单,但这份清单并不完整,因为我们知道,这种相关性取决于排放量。因此,我们可以进行如下操作:
得到一张数字特征图,看看哪些人的排放很少或解释的排放。
删去排放,重新计算相关。
顺便说一下,相关性本身不一定能解释数据之间的关系。例如,比线性关系更复杂的关系,就不能简单地看关联度的值来猜测。所以,我们也来看看我们从关联表中排除的征兆,并建立它们,看看它们和房价之间是否有关联性。
考虑下图中的许多相关领域(Correlation)。值的分布(
Python之探索式数据分析相关推荐
- 【Python-ML】探索式数据分析EDA(Exploratory Data Analysis)
# -*- coding: utf-8 -*- ''' Created on 2018年1月24日 @author: Jason.F @summary: 有监督回归学习-探索式数据分析(EDA,Exp ...
- 流式数据分析_流式大数据分析
流式数据分析 The recent years have seen a considerable rise in connected devices such as IoT [1] devices, ...
- python基础知识及数据分析工具安装及简单使用(Numpy/Scipy/Matplotlib/Pandas/StatsModels/Scikit-Learn/Keras/Gensim))
Python介绍. Unix & Linux & Window & Mac 平台安装更新 Python3 及VSCode下Python环境配置配置 python基础知识及数据分 ...
- python的excel数据分析_excel VS python 谁更适合数据分析?
我常常会听到这样的问题,"金融分析中,为什么我要学习像python这样的编程语言,甚至使用它超过excel呢?" 在金融领域,python成为炙手可热的分析工具,这几乎已经成为共识 ...
- python数据分析软件_Python数据分析工具
Numpy Python没有提供数组功能,虽然列表可以完成基本的数组功能,但他不是真正的数组.Numpy内置函数处理速度是c语言级别,因此尽量使用内置函数,避免出现效率瓶颈的现象. Numpy的安装: ...
- 用python进行简单的数据分析和数据可视化
用python进行简单的数据分析和数据可视化 本篇文章主要是初步探索数据分析,简单了解数据分析大致流程 数据来源:来自于Kaggle平台上的一个项目:Explore San Francisco cit ...
- BI与ClickHouse:探索式BI的OLAP技术演进之路
尚学堂给同学们带来全新的Java300集课程啦!java零基础小白自学Java必备优质教程_手把手图解学习Java,让学习成为一种享受_哔哩哔哩_bilibili 1. BI系统的沉浮 1.1 传统B ...
- 最全知乎专栏合集:爬取11088个知乎专栏,打破发现壁垒(编程、python、爬虫、数据分析..)
最近逛博客,看到一篇很好的文章,整合了知乎上所有优秀的编程.算法专栏,对学习的帮助非常大,转载过来分享给大家: 众所周知,知乎官方没有搜素专栏的功能,于是我通过爬取几十万用户个人主页所关注的专栏从而获 ...
- 117.精读《Tableau 探索式模型》
1. 引言 Tableau 探索式分析功能非常强大,各种功能组合似乎有着无限的可能性. 今天笔者会分析这种探索式模型解题思路,一起看看这种探索式分析功能是如何做到的. 2. 精读 要掌握探索式分析,先 ...
最新文章
- 自然语言处理(NLP)之用深度学习实现命名实体识别(NER)
- DISK 100% BUSY,谁造成的?(ok)
- SQL Server:向 SQL Server 自增字段插入值 (转)
- WPF 如何实现颜色值拾取
- Linux字符界面和图形界面
- 机械制图国家标准的绘图模板_如何使用p5js构建绘图应用
- 史上最全java架构师技能图谱(上)
- python并发编程之semaphore(信号量)_浅谈Python并发编程之进程(守护进程、锁、信号量)...
- php中常见的header类型
- java程序员_哪些书是不可错过的?Java程序员书单分享
- 计算机辅助平面绘图是干嘛的,【1人回答】AutoCAD画图是什么,干什么用的?-3D溜溜网...
- 计算机应用物联网应用技术论文,计算机应用技术论文:物联网垃圾回收站设计与实现...
- php创建数组填充数组的方法
- 股票python量化交易002-常见量化指标(技术面)
- 计算机专业毕业设计致谢,计算机本科毕业设计致谢
- 皮边油消泡剂消除泡沫另一视角曝光
- 想运行游戏,在Pycharm中安装Pygame库一直报错,我只是忽视了它
- [转]商业模式新生代
- linux命令dstat,dstat命令
- Bootstrap 引入 栅栏系统 文本 表格 类