美文网首页R
kaggle案例重复:科比的投篮选择之一

kaggle案例重复:科比的投篮选择之一

作者: 小明的数据分析笔记本 | 来源:发表于2019-04-27 22:00 被阅读43次

以下内容为kaggle网站上的一个案例;原文地址 Kobe Bryant Shot Selection。主要内容是探索科比20年NBA生涯的数据,包括进攻方式,出手距离和出手区域,命中率等。

原文很长,准备分成几个部分来重复,今天是第一部分

读入数据、查看数据维度、删除缺失值等
shots<-read.csv("data.csv")
dim(shots)
[1] 30697    25

可以看到原数据集总共包括25个变量,30697行数据
删除缺失值所在的行

shots<-na.omit(shots)
dim(shots)
[1] 25697    25

删除缺失值后数据少了5000条。因为kaggle贴出的数据集目的为:是否可以根据科比20年职业生涯的出手数据来预测下一次投篮是否可以命中。所以在原数据集中删除了5000条shot_made_flags。这部分用来做测试集。(学英语学英语:Using 20 years of data on Kobe's swishes and misses, can you predict which shots will find the bottom of the net?)。这句话中的两个生词:swishes and misses;find the bottom of the net

加载本次分析所需要的R包
library(ggplot2)  
library(tidyverse)
library(gridExtra)

ggplot2用来作图
tidyverse用来整合数据
gridExtra用来拼图(ggplot2出图拼接有一个专门的R包ggpubr,很好用)

数据可视化

散点图看一下科比的投篮方式(shot type)

首先看一下数据集中的combined_shot_type变量中都包括哪些值

unique(shots$combined_shot_type)
[1] Jump Shot Dunk      Layup     Tip Shot  Hook Shot Bank Shot
Levels: Bank Shot Dunk Hook Shot Jump Shot Layup Tip Shot

投篮方式主要包括六种类型

Jump Shot Dunk Layup Tip Shot Hook Shot Bank Shot
跳投 扣篮 上篮 补篮 勾手 擦板

散点图可视化

ggplot()+
  geom_point(data= shots %>% filter(combined_shot_type == "Jump Shot"),
             aes(x=lon,y=lat),color="grey",alpha=0.3)+
  geom_point(data = shots %>% filter(combined_shot_type != "Jump Shot"),
             aes(x=lon,y=lat,color=combined_shot_type),alpha=0.8)+
  labs(title="Shot type")+
  ylim(c(33.7,34.0883))+theme_void()+
  theme(legend.title = element_blank(),
        plot.title = element_text(hjust=0.5))
Rplot10.png

可以看出绝大部分进攻都以跳投结束

散点图出手距离、柱形图每个距离范围出手次数

shot_zone_range变量中包含的值

unique(shots$shot_zone_range)
[1] 8-16 ft.        16-24 ft.       Less Than 8 ft. 24+ ft.        
[5] Back Court Shot
Levels: 16-24 ft. 24+ ft. 8-16 ft. Back Court Shot Less Than 8 ft.

feet 英尺;1英尺等于0.3048米
NBA三分线7.25米;23.9英尺

p1<-ggplot(data=shots,aes(x=lon,y=lat))+
  geom_point(aes(color=shot_zone_range))+
  labs(title="Shot zone range")+
  ylim(c(33.7,34.0883))+
  theme_void()+
  theme(legend.position="none",
        plot.title=element_text(hjust=0.5))
p2<-ggplot(shots,aes(x=fct_infreq(shot_zone_range)))+
  geom_bar(aes(fill=shot_zone_range))+labs(y="Frequency")+
  theme_bw()+
  theme(axis.title.x=element_blank(),legend.position="none")
grid.arrange(p1,p2,layout_matrix=cbind(c(1,2)))
Rplot11.png
根据柱形图可以看出篮下和长两分是科比的主要进攻方式
这里遇到了一个新函数fac_infreq(),之前没有接触过。作用是因子变量排序。小例子
f<-factor(c("b","b","a","c","c","c"))
f
[1] b b a c c c
Levels: a b c
fct_infreq(f)
[1] b b a c c c
Levels: c b a
fct_inorder(f)
[1] b b a c c c
Levels: b a c

这里很神奇的是p1和p2的颜色可以对应上,暂时还搞不懂是什么原理。

出手区域散点图,柱形图
unique(shots$shot_zone_area)
[1] Left Side(L)          Left Side Center(LC) 
[3] Right Side Center(RC) Center(C)            
[5] Right Side(R)         Back Court(BC)       
6 Levels: Back Court(BC) Center(C) ... Right Side(R)

出手区域被分为六个部分

p3<-ggplot(shots,aes(x=lon,y=lat))+
  geom_point(aes(colour=shot_zone_area))+
  labs(title="Shot zone area")+
  ylim(c(33.7,34.0883))+theme_void()+
  theme(legend.position = "none",
        plot.title=element_text(hjust=0.8))
p4<-ggplot(shots,aes(x=fct_infreq(shot_zone_area)))+
  geom_bar(aes(fill=shot_zone_area))+
  labs(y="Frequency")+theme_bw()+
  theme(axis.text.x=element_text(size=7),
        axis.title.x=element_blank(),
        legend.position="none")
grid.arrange(p3,p4,layout_matrix=cbind(c(1,2)))
Rplot12.png

从上图可以看出右侧区域是科比比较喜欢的进攻区域

投篮区域根据另一个原则划分(散点图、柱形图)
unique(shots$shot_zone_basic)
[1] Mid-Range             Restricted Area       In The Paint (Non-RA)
[4] Above the Break 3     Right Corner 3        Backcourt            
[7] Left Corner 3        
7 Levels: Above the Break 3 Backcourt ... Right Corner 3

Mid-Rnage
Restricted Area 限制区
In the Paint(Non-RA) 限制区以外的油漆区
Above the Break 3 踩线三分???
Right Corner 3 Left Corner 3
Backcourt后场

p5 <- ggplot(shots, aes(x=lon, y=lat)) +
  geom_point(aes(color=shot_zone_basic)) +
  labs(title="Shot zone basic") +
  ylim(c(33.7, 34.0883)) +
  theme_void() +
  theme(legend.position="none", 
        plot.title=element_text(hjust=0.5))


p6 <- ggplot(shots, aes(x=fct_infreq(shot_zone_basic))) + 
  geom_bar(aes(fill=shot_zone_basic)) +
  labs(y="Frequency") +
  theme_bw() +
  theme(axis.text.x=element_text(size=6.3),
        axis.title.x=element_blank(), 
        legend.position="none")

grid.arrange(p5, p6, layout_matrix=cbind(c(1,2)))
Rplot13.png

从上图可以看出中距离是科比的主要进攻方式

To be continue

欢迎大家关注我的公众号 小明的数据分析笔记本

公众号二维码.jpg

相关文章

  • kaggle案例重复:科比的投篮选择之二

    今天继续重复kaggle案例:科比的投篮选择。原文地址https://www.kaggle.com/xvivanc...

  • kaggle案例重复:科比的投篮选择之一

    以下内容为kaggle网站上的一个案例;原文地址 Kobe Bryant Shot Selection。主要内容是...

  • Kaggle入门案例——从进网站到获得评测结果

    最近写了Kaggle的一个playground项目——预测科比投篮是否命中https://www.kaggle...

  • tensorflow笔记 - bug - onehot

    数据预处理时用pandas 做了一个科比的投篮预测,数据集在kaggle上可以找到,我们对flag(投中与否)进行...

  • 当你想成功的欲望和呼吸一样强烈时。

    科比·布莱恩特 (美国篮球运动员) 科比是NBA最好的得分手之一,生涯赢得无数奖项[1],突破、投篮、罚球、三分球...

  • 别总一样

    喜欢篮球的人应该对于科比·比恩·布莱恩特如雷贯耳,他是NBA最好的得分手之一,突破、投篮、罚球、三分球他都驾轻就熟...

  • 音频37

    音频37 【学习】天才来自刻意的练习 成功等于简单的事情重复做。科比的成功,来自于每天早上4点钟去练习投篮1000...

  • 无论如何都要持续

    持续是硬道理,天才的成功来自刻意的重复。科比一身的伤病有多少?可他依然每天看到洛杉矶4点钟的样子,而且投篮1000...

  • 转述《时间管理100讲》37-学习的规律

    科比他每天早起打篮球投篮1000个。 成功就是这样,简单的事情重复做。 成功等于简单的事情重复做,其实只说了一半。...

  • 时间管理100讲第37讲

    天才来自刻意的练习 科比每天早上4点起床,会去打篮球投篮1000个,成功等于简单的事情重复做。 水滴石穿、冰冻三尺...

网友评论

    本文标题:kaggle案例重复:科比的投篮选择之一

    本文链接:https://www.haomeiwen.com/subject/tigowqtx.html