文章目录
博文配套视频课程:24小时实现从零到AI人工智能
数据获取、结构分析
如何获取seaborn提供数据源
在python中基本所有的核心库都提供了自己的数据源,例如:seaborn、sklearn、tensorflow… 而且这些数据源基本都托管在github中,如果想了解获取数据原理,单击load_dataset源码就可以看到数据的下载地址
def load_dataset(name, cache=True, data_home=None, **kws):
"""Load a dataset from the online repository (requires internet).
Parameters
----------
name : str
Name of the dataset (`name`.csv on
https://github.com/mwaskom/seaborn-data). You can obtain list of
available datasets using :func:`get_dataset_names`
cache : boolean, optional
If True, then cache data locally and use the cache on subsequent calls
data_home : string, optional
The directory in which to cache data. By default, uses ~/seaborn-data/
kws : dict, optional
Passed to pandas.read_csv
"""
path = ("https://raw.githubusercontent.com/"
"mwaskom/seaborn-data/master/{}.csv")
full_path = path.format(name)
通过github下载数据如下, 如果数据量比较大可以保存到本地的csv格式
titanic = sns.load_dataset("titanic")
print(titanic.head())
titanic.to_csv("../data/titanic.csv")
titanic = pd.read_csv("../data/titanic.csv")
titanic.info()
# 检查是否有缺失值
print(titanic.isnull().sum())
特征列分析
在此套课程中我们仅仅讲解关于特征列的清洗、数据分析、列的缺失、异常值处理等… 后续在机器学习课程中还会讲解到特征工程、PCA主特征分析等重要概念。
- Survived: 生存情况,0代表不幸遇难,1代表存活;
- Pclass: 仓位等级,1为一等舱,2为二等舱,3为三等舱;
- Name: 乘客姓名;
- Sex: 性别;
- Age: 年龄;
- SibSp: 乘客在船上的兄妹姐妹数/配偶数(即同代直系亲属数)
- Parch: 乘客在船上的父母数/子女数(即不同代直系亲属数);
- Ticket: 船票编号;
- Fare: 船票价格;
- Embarked: 登船港口 (S: Southampton; C: Cherbourg Q: Queenstown)
- alive:活着
- alone:单身
缺失值与数据清洗
年龄缺失值处理
sns.set_style("darkgrid")
print(titanic[titanic['age'].notnull()])
sns.distplot(a = titanic[titanic['age'].notnull()]['age'],hist=True,kde=True)
plt.show()
# 把年龄的缺失值填充为平均值
titanic['age'] = titanic['age'].fillna(titanic['age'].mean())
sns.distplot(titanic['age'])
plt.show()
填充embarked
print(titanic['embarked'].unique())
# 首先会对输入的列进行分组,然后对每一组进行统计(count),最后进行可视化 Plot
# sns.countplot(x=titanic['embarked'],data=titanic)
sns.countplot(y=titanic['embarked'],hue='sex',data=titanic)
plt.show()
# inplace=True: 则会修改操作的数据集
titanic['embarked'].fillna(value = 'S',inplace=True)
print(titanic.isnull().sum())
删除不必要的列
一般我们会把不重要的列,缺失值比较多的列删除掉。
titanic.drop(labels=['survived','pclass','sibsp','parch','who','adult_male','deck','embark_town'],axis=1,inplace=True)
print(titanic.head(n=5))
titanic.to_csv("../data/titanic2.csv",index=False)
幸存者分析
分析性别对存活率的影响
虽然男士体力较好,但是在这次海难中体现了绅士风度,把生存的机会留给了女士,所以性别对存活率影响比较大
# 1: 分析性别对存活率的影响,结论: 在事故中,女士优先,存活率高于男士
titanic = pd.read_csv("../data/titanic2.csv")
print(titanic.head(n=3))
sns.set_style("darkgrid")
sns.countplot(x="alive",hue='sex',data=titanic)
plt.show()
分析年龄对存活率影响
由于年龄是连续型的数据, 连续型数据不方便分组,因此建议编写一个函数让连续型数据转化为离散型数据
# 2: 分析年龄对存活率影响,结论: 年龄对存活率的影响没有明显的相关性
def agelevel(age):
if age <=16:
return "child" # 青年
elif age >=60:
return 'aged' # 老年
else:
return "midlife" # 中年
print(agelevel)
titanic['age_level'] = titanic['age'].map(agelevel)
print(titanic.head(n=10))
sns.countplot(x="alive",hue='age_level',data=titanic)
plt.show()
分析舱位等级与存活率影响
# 3:分析舱位等级与存活率影响, 结论: vip一等舱存活率比较高
sns.countplot(x="alive",hue='class',data=titanic)
plt.show()