Python4学习笔记--宝可梦数据分析

运用数据分析的方式来了解宝可梦这种神奇的生物

首先进行数据集下载

# 数据集下载
!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个特征来描述他们

#查看41个特征
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)

通过查看以上数据,可以发现,type2 这个字段缺失的比率最高,达到了 48% 左右。说明超过半数的宝可梦还是单纯的只有一个属性,剩下一般的则具有两种属性。

宝可梦有这么多只,也有很多代,那么每代分别有多少只呢?可以通过df[‘generation’].value_counts()来得到,而且为 了更加直观的表现出不同代的宝可梦的数量差别,这里可以用pandas自带的画图的功能来绘制一个柱状图:

# 查看各代口袋妖怪的数量
df['generation'].value_counts().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)

通过生成的相关性图可以观察到attack 这一个特征和 height_m 是正相关的,说明:越高的宝可梦,攻击力越高。但是再看 height_m,会发现它和base_happiness 是负相关的。可说明:长得高的宝可梦可能都不太开心。

看完这些以后,我们就可以开始计算种族值然后来选取我们的平民神兽了。毕竟不是每个人都能收服代欧奇希斯,超梦,梦幻这种传说级别的宝可梦。这里我们可以通过如下方式,先做一个特征类型转化,然后再计算

for c in interested:
    df[c] = df[c].astype(float)
df = df.assign(total_stats = df[interested].sum(axis=1)) 

这样我们就完成了用 total_stats 这个字段来存储种族值这一特征。我们可以做个柱状图可视化来看看种族值的分布是什么样的:

# 种族值分布
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")

最后我们就可以通过简单的过滤和排序来找到我们应该去捕捉的宝可梦了:

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宝可梦应该是:妙蛙花,喷火龙,水箭龟,比雕,胡地,呆河马,耿鬼,袋兽,大甲,暴鲤龙。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值