R

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

2019-04-27  本文已影响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
上一篇 下一篇

猜你喜欢

热点阅读