Python数据分析实例:平民最强宝可梦选择方案

前言

这是 Python 语法学习系列完成后的一个数据分析入门教程,当做 Python 语法学习系列的结束篇,旨在引导学习数据分析常用方法。以一份包含着从第一代到第七代共801只宝可梦的数据集为数据原材料,实现数据分析常用方法样例,假定目标是寻找平民最强宝可梦。

实战中数据集大小会变,分析目标会变,但方法论总是不变的,只需要替换数据集代入方法,打个不恰当的比方,无非是锅里炒的是什么菜,而不变的是锅。

环境配置为:Win10,Python3,Jupyter Notebook

数据集下载

# 数据集下载
!wget -O pokemon_data.csv https://pai-public-data.oss-cn-beijing.aliyuncs.com/pokemon/pokemon.csv

数据分析

然后我们import我们最常用的三大件:Pandas, Seaborn, Matplotlib, 并且读取数据

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.read_csv("./pokemon_data.csv")

首先我们观察一下数据的尺寸,可以通过 df.shape 这个来实现。当然 df.info() 能够给我们更加详细的每个列的信息。这里我们通过这个方式,可以发现这个数据集一共收录了801行,41列的数据。说明一共有801只宝可梦,每只宝可梦我们有41个特征来描述它们。

df.head()

在这里插入图片描述

df.info()
'''
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 801 entries, 0 to 800
Data columns (total 41 columns):
abilities            801 non-null object
against_bug          801 non-null float64
against_dark         801 non-null float64
against_dragon       801 non-null float64
against_electric     801 non-null float64
against_fairy        801 non-null float64
against_fight        801 non-null float64
against_fire         801 non-null float64
against_flying       801 non-null float64
against_ghost        801 non-null float64
against_grass        801 non-null float64
against_ground       801 non-null float64
against_ice          801 non-null float64
against_normal       801 non-null float64
against_poison       801 non-null float64
against_psychic      801 non-null float64
against_rock         801 non-null float64
against_steel        801 non-null float64
against_water        801 non-null float64
attack               801 non-null int64
base_egg_steps       801 non-null int64
base_happiness       801 non-null int64
base_total           801 non-null int64
capture_rate         801 non-null object
classfication        801 non-null object
defense              801 non-null int64
experience_growth    801 non-null int64
height_m             781 non-null float64
hp                   801 non-null int64
japanese_name        801 non-null object
name                 801 non-null object
percentage_male      703 non-null float64
pokedex_number       801 non-null int64
sp_attack            801 non-null int64
sp_defense           801 non-null int64
speed                801 non-null int64
type1                801 non-null object
type2                417 non-null object
weight_kg            781 non-null float64
generation           801 non-null int64
is_legendary         801 non-null int64
dtypes: float64(21), int64(13), object(7)
memory usage: 256.6+ KB
'''

然后就迎来了我们的第一个问题:这么多特征,是否会有数据缺失呢?毕竟有些宝可梦比较神秘感,就连大木博士都不一定知道。这里我们可以通过如下代码来观察每个特征的缺失情况:

# 计算出每个特征有多少百分比是缺失的
percent_missing = df.isnull().sum() * 100 / len(df)
missing_value_df = pd.DataFrame({
    'column_name': df.columns,
    'percent_missing': percent_missing
})
# 查看Top10缺失的
missing_value_df.sort_values(by='percent_missing', ascending=False).head(10)

在这里插入图片描述

# 查看各代口袋妖怪的数量
df['generation'].value_counts().sort_values(ascending=False).plot.bar()

在这里插入图片描述

# 查看每个系口袋妖怪的数量
df['type1'].value_counts().sort_values(ascending=True).plot.barh()

在这里插入图片描述

# 相关性热力图分析
plt.subplots(figsize=(20,15))
ax = plt.axes()
ax.set_title("Correlation Heatmap")
corr = df.corr()
sns.heatmap(corr, 
            xticklabels=corr.columns.values,
            yticklabels=corr.columns.values)

在这里插入图片描述

interested = ['hp','attack','defense','sp_attack','sp_defense','speed']
sns.pairplot(df[interested])

在这里插入图片描述

# 通过相关性分析heatmap分析五个基础属性
plt.subplots(figsize=(10,8))
ax = plt.axes()
ax.set_title("Correlation Heatmap")
corr = df[interested].corr()
sns.heatmap(corr, 
            xticklabels=corr.columns.values,
            yticklabels=corr.columns.values,
            annot=True, fmt="f",cmap="YlGnBu")

在这里插入图片描述

for c in interested:
    df[c] = df[c].astype(float)
df = df.assign(total_stats = df[interested].sum(axis=1)) 
df[df.total_stats >= 525].shape
'''
(167, 42)
'''
# 种族值分布
total_stats = df.total_stats
plt.hist(total_stats,bins=35)
plt.xlabel('total_stats')
plt.ylabel('Frequency')

在这里插入图片描述

plt.subplots(figsize=(20,12))
ax = sns.violinplot(x="type1", y="total_stats",
                    data=df, palette="muted")

在这里插入图片描述

# 种族值大于570的,但是不是神兽的
df[(df.total_stats >= 570) & (df.is_legendary == 0)]['name'].head(10)
'''
2        Venusaur
5       Charizard
8       Blastoise
17        Pidgeot
64       Alakazam
79        Slowbro
93         Gengar
114    Kangaskhan
126        Pinsir
129      Gyarados
Name: name, dtype: object
'''

其他有意思的分析

sns.jointplot("base_egg_steps", "experience_growth", data=df, size=5, ratio=3, color="g")

在这里插入图片描述

sns.jointplot("attack", "hp", data=df, kind="kde")

在这里插入图片描述

# 双系宝可梦数量统计
plt.subplots(figsize=(10, 10))

sns.heatmap(
    df[df['type2']!='None'].groupby(['type1', 'type2']).size().unstack(),
    linewidths=1,
    annot=True,
    cmap="Blues"
)

plt.xticks(rotation=35)
plt.show()

在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值