口袋妖怪数据集探索
数据集下载
!wget -O pokemon_data.csv https://pai-public-data.oss-cn-beijing.aliyuncs.com/pokemon/pokemon.csv
--2020-09-15 23:21:34-- https://pai-public-data.oss-cn-beijing.aliyuncs.com/pokemon/pokemon.csv
Resolving pai-public-data.oss-cn-beijing.aliyuncs.com (pai-public-data.oss-cn-beijing.aliyuncs.com)... 59.110.185.63
Connecting to pai-public-data.oss-cn-beijing.aliyuncs.com (pai-public-data.oss-cn-beijing.aliyuncs.com)|59.110.185.63|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 160616 (157K) [text/csv]
Saving to: ‘pokemon_data.csv’
100%[======================================>] 160,616 --.-K/s in 0.1s
2020-09-15 23:21:34 (1.60 MB/s) - ‘pokemon_data.csv’ saved [160616/160616]
!pip install seaborn --user -q
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv("./pokemon_data.csv")
df.head()
abilities | against_bug | against_dark | against_dragon | against_electric | against_fairy | against_fight | against_fire | against_flying | against_ghost | ... | percentage_male | pokedex_number | sp_attack | sp_defense | speed | type1 | type2 | weight_kg | generation | is_legendary | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | ['Overgrow', 'Chlorophyll'] | 1.0 | 1.0 | 1.0 | 0.5 | 0.5 | 0.5 | 2.0 | 2.0 | 1.0 | ... | 88.1 | 1 | 65 | 65 | 45 | grass | poison | 6.9 | 1 | 0 |
1 | ['Overgrow', 'Chlorophyll'] | 1.0 | 1.0 | 1.0 | 0.5 | 0.5 | 0.5 | 2.0 | 2.0 | 1.0 | ... | 88.1 | 2 | 80 | 80 | 60 | grass | poison | 13.0 | 1 | 0 |
2 | ['Overgrow', 'Chlorophyll'] | 1.0 | 1.0 | 1.0 | 0.5 | 0.5 | 0.5 | 2.0 | 2.0 | 1.0 | ... | 88.1 | 3 | 122 | 120 | 80 | grass | poison | 100.0 | 1 | 0 |
3 | ['Blaze', 'Solar Power'] | 0.5 | 1.0 | 1.0 | 1.0 | 0.5 | 1.0 | 0.5 | 1.0 | 1.0 | ... | 88.1 | 4 | 60 | 50 | 65 | fire | NaN | 8.5 | 1 | 0 |
4 | ['Blaze', 'Solar Power'] | 0.5 | 1.0 | 1.0 | 1.0 | 0.5 | 1.0 | 0.5 | 1.0 | 1.0 | ... | 88.1 | 5 | 80 | 65 | 80 | fire | NaN | 19.0 | 1 | 0 |
5 rows × 41 columns
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
首先我们观察一下数据的尺寸,可以通过 df.shape 这个来实现。当然 df.info() 能够给我们更加详细的每个列的信息。这里我们通过这个方式,可以发现这个数据集一共收录了801行,41列的数据。说明一共有801只宝可梦,每只宝可梦我们有41个特征来描述它们。
然后就迎来了我们的第一个问题:这么多特征,是否会有数据缺失呢?毕竟有些宝可梦比较神秘感,就连大木博士都不一定知道。这里我们可以通过如下代码来观察每个特征的缺失情况:
# 计算出每个特征有多少百分比是缺失的
percent_missing = df.isnull().sum() * 100 / len(df)
missing_value_df = pd.DataFrame({
'column_name': df.columns,
'percent_missing': percent_missing
})
missing_value_df.sort_values(by='percent_missing', ascending=False).head(10)
column_name | percent_missing | |
---|---|---|
type2 | type2 | 47.940075 |
percentage_male | percentage_male | 12.234707 |
weight_kg | weight_kg | 2.496879 |
height_m | height_m | 2.496879 |
name | name | 0.000000 |
capture_rate | capture_rate | 0.000000 |
classfication | classfication | 0.000000 |
defense | defense | 0.000000 |
experience_growth | experience_growth | 0.000000 |
hp | hp | 0.000000 |
通过查看以上数据,我们可以发现,type2 这个字段缺失的比率最高,达到了 48% 左右。说明超过半数的宝可梦还是单纯的只有一个属性,剩下一般的则具有两种属性。
然后第二个问题就是:这么多宝可梦,每代分别有几只?这里我们可以通过简单的 df[‘generation’].value_counts() 来得到。但是为了更加直观的表现出不同代的宝可梦的数量差别,这里我们可以用pandas自带的画图的功能来绘制一个柱状图:通过查看以上数据,我们可以发现,type2 这个字段缺失的比率最高,达到了 48% 左右。说明超过半数的宝可梦还是单纯的只有一个属性,剩下一般的则具有两种属性。
然后第二个问题就是:这么多宝可梦,每代分别有几只?这里我们可以通过简单的 df[‘generation’].value_counts() 来得到。但是为了更加直观的表现出不同代的宝可梦的数量差别,这里我们可以用pandas自带的画图的功能来绘制一个柱状图:
# 查看各代口袋妖怪的数量
df['generation'].value_counts().sort_values(ascending=False).plot.bar()
不难发现,宝可梦数量最多的是在第5代,最少的是在第6代。然后我们再来看不同的主属性的分布。这里我们可以先做一些简单的假设,比如虫属性的宝可梦种类比较多因为在剧中出现的频率相当高,而且有很多种进化。
# 查看每个系口袋妖怪的数量
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)
<AxesSubplot:title={'center':'Correlation Heatmap'}>
来了解不同特征之间的关联,这对我们了解宝可梦的特性很有帮助。比如通过观察 attack 这一个特征和 height_m 是正相关的,我们可以得出:越高的宝可梦,攻击力越高。但是再看 height_m,我们会发现它和 base_happiness 是负相关的。这个时候我们可以作出另外一个结论:长得高的宝可梦可能都不太开心。
接下来我们从宝可梦在实战中的角度来分析这组数据。这里我们只关注六个基础值:血量,攻击力,防御力,特攻,特防,速度。因为只有这六个基础值决定了一只宝可梦的战斗力在不考虑派系克制的情况下。
interested = ['hp','attack','defense','sp_attack','sp_defense','speed']
sns.pairplot(df[interested])
<seaborn.axisgrid.PairGrid at 0x7f3c3793b208>
这里我们可以看到大部分都是成正比例的,一个值的提高往往会拉高另外一个值。这点我们通过相关性热力图也可以看到
# 通过相关性分析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")
<AxesSubplot:title={'center':'Correlation Heatmap'}>
看完这些以后,我们就可以开始计算种族值然后来选取我们的平民神兽了。毕竟不是每个人都能收服代欧奇希斯,超梦,梦幻这种传说级别的宝可梦。这里我们可以通过如下方式,先做一个特征类型转化,然后再计算
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 这个字段来存储种族值这一特征。我们可以做个柱状图可视化来看看种族值的分布是什么样的:
# 种族值分布
total_stats = df.total_stats
plt.hist(total_stats,bins=35)
plt.xlabel('total_stats')
plt.ylabel('Frequency')
Text(0, 0.5, '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
从结果上来看,我们平民宝可梦训练师应该考虑的Top10宝可梦应该是:妙蛙花,喷火龙,水箭龟,比雕,胡地,呆河马,耿鬼,袋兽,大甲,暴鲤龙。这样,我们就通过简单的数据分析,完成了大多数宝可梦动漫中的训练师不可能完成的任务。这么想想,升职加薪,出任CEO,赢取白富美,当上研究所所长的日子简直指日可待呀!
不过要不是有DSW 探索者版这么方便的工具,这真是一个无法完成的任务。现在,这份宝可梦分析已经放在了这里,希望读到这篇文章的你也能像我一样,通过数据分析成为一个伟大的宝可梦训练师。
其他有意思的分析
sns.jointplot("base_egg_steps", "experience_growth", data=df, size=5, ratio=3, color="g")
/opt/conda/lib/python3.6/site-packages/seaborn/axisgrid.py:2264: UserWarning: The `size` parameter has been renamed to `height`; please update your code.
warnings.warn(msg, UserWarning)
<seaborn.axisgrid.JointGrid at 0x7f3c353736a0>
sns.jointplot("attack", "hp", data=df, kind="kde")
<seaborn.axisgrid.JointGrid at 0x7f3c35314198>
# 双系宝可梦数量统计
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()