文章目录

  • 一. 数据源介绍
  • 二. 数据预处理
    • 2.1 简单看看科比投篮的位置
    • 2.2 对数据做预处理
    • 2.3 查看特征值之间是否存在线性关系
    • 2.4 使用group by查看数据分布情况
    • 2.5 查看投篮区域和投篮范围的情况
    • 2.6 pandas独热编码
  • 三. 训练模型
  • 参考:

一. 数据源介绍

数据源是科比篮球比赛的一个数据集

我们先简单的看一下数据集

特征值简介:
action_type 进攻方式(更具体)
combined_shot_type 进攻方式
game_event_id 比赛时间id
game_id 比赛ID
lat 投篮点
loc_x 投篮点
loc_y 投篮点
lon 投篮点
minutes_remaining 单节剩余时间(分钟)
period 表示第几节
playoffs 是否是季后赛
season 赛季
seconds_remaining 剩余时间(秒)
shot_distance 投篮距离
shot_made_flag 是否进球
shot_type 两分球或三分球
shot_zone_area 投篮区域
shot_zone_basic 投篮区域(更具体)
shot_zone_range 投篮范围
team_id 球队ID
team_name 球队名称
game_date 比赛日期
matchup 比赛双方
opponent 对手
shot_id 投篮ID

二. 数据预处理

2.1 简单看看科比投篮的位置

代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import KFold# 读取数据源
filename = "E:/file/data.csv"
raw = pd.read_csv(filename)
#print(raw.head(10))# 选择标签值不为null的
kobe = raw[pd.notnull(raw['shot_made_flag'])]# 画图
alpha = 0.02
plt.figure(figsize=(10, 10))# x轴和y轴
plt.subplot(121)
plt.scatter(kobe.loc_x, kobe.loc_y, color='r', alpha=alpha)
plt.title('loc_x and loc_y')# 精度和维度
plt.subplot(122)
plt.scatter(kobe.lon, kobe.lat, color='b', alpha=alpha)
plt.title('lat and lon')plt.show()

测试记录:
从图中我们可以看到,loc_x和loc_y 以及 lat和lon 这些都代表了坐标,可看到kobe投篮位置的分布。

2.2 对数据做预处理

通过我们简单的分析可以看到数据集存在如下问题:

  1. shot_made_flag 这个标签值有缺失值
  2. loc_x 有为0的异常值
  3. season 特征值格式混乱
  4. 部分特征值我们需要看看唯一值

做数据预处理的时候,我们需要解决上述问题

代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import KFold# 读取数据源
filename = "E:/file/data.csv"
raw = pd.read_csv(filename)
#print(raw.head(10))# 选择标签值不为null的
kobe = raw[pd.notnull(raw['shot_made_flag'])]# 根据X轴和Y轴来计算距离,衍生一个特征
raw['dist'] = np.sqrt(raw['loc_x']**2 + raw['loc_y']**2)# 处理loc_x 为0的异常数据
loc_x_zero = raw['loc_x'] == 0
#print (loc_x_zero)
raw['angle'] = np.array([0]*len(raw))
raw['angle'][~loc_x_zero] = np.arctan(raw['loc_y'][~loc_x_zero] / raw['loc_x'][~loc_x_zero])
raw['angle'][loc_x_zero] = np.pi / 2# 将比赛剩余的分钟和秒  加起来,衍生一个特征
raw['remaining_time'] = raw['minutes_remaining'] * 60 + raw['seconds_remaining']# 查看特征的唯一值
print("查看特征的唯一值")
print(kobe.action_type.unique())
print(kobe.combined_shot_type.unique())
print(kobe.shot_type.unique())
print(kobe.shot_type.value_counts())print("处理season格式不统一的问题")
kobe['season'].unique()
raw['season'] = raw['season'].apply(lambda x: int(x.split('-')[1]))
print(raw['season'].unique())

测试记录:

查看特征的唯一值
['Jump Shot' 'Driving Dunk Shot' 'Layup Shot' 'Running Jump Shot''Reverse Dunk Shot' 'Slam Dunk Shot' 'Driving Layup Shot''Turnaround Jump Shot' 'Reverse Layup Shot' 'Tip Shot''Running Hook Shot' 'Alley Oop Dunk Shot' 'Dunk Shot''Alley Oop Layup shot' 'Running Dunk Shot' 'Driving Finger Roll Shot''Running Layup Shot' 'Finger Roll Shot' 'Fadeaway Jump Shot''Follow Up Dunk Shot' 'Hook Shot' 'Turnaround Hook Shot' 'Jump Hook Shot''Running Finger Roll Shot' 'Jump Bank Shot' 'Turnaround Finger Roll Shot''Hook Bank Shot' 'Driving Hook Shot' 'Running Tip Shot''Running Reverse Layup Shot' 'Driving Finger Roll Layup Shot''Fadeaway Bank shot' 'Pullup Jump shot' 'Finger Roll Layup Shot''Turnaround Fadeaway shot' 'Driving Reverse Layup Shot''Driving Slam Dunk Shot' 'Step Back Jump shot' 'Turnaround Bank shot''Reverse Slam Dunk Shot' 'Floating Jump shot' 'Putback Slam Dunk Shot''Running Bank shot' 'Driving Bank shot' 'Driving Jump shot''Putback Layup Shot' 'Putback Dunk Shot' 'Running Finger Roll Layup Shot''Pullup Bank shot' 'Running Slam Dunk Shot' 'Cutting Layup Shot''Driving Floating Jump Shot' 'Running Pull-Up Jump Shot' 'Tip Layup Shot''Driving Floating Bank Jump Shot']
['Jump Shot' 'Dunk' 'Layup' 'Tip Shot' 'Hook Shot' 'Bank Shot']
['2PT Field Goal' '3PT Field Goal']
2PT Field Goal    20285
3PT Field Goal     5412
Name: shot_type, dtype: int64
处理season格式不统一的问题
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 97 98 99  0]

2.3 查看特征值之间是否存在线性关系

如果特征值之间存在线性关系,此时我们只需要使用其中之一即可,无需使用多个特征值。

代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import KFold# 读取数据源
filename = "E:/file/data.csv"
raw = pd.read_csv(filename)
#print(raw.head(10))# 根据X轴和Y轴来计算距离,衍生一个特征
raw['dist'] = np.sqrt(raw['loc_x']**2 + raw['loc_y']**2)# 画散点图
# 这个两个特征值呈线性关系,用一个即可
plt.figure(figsize=(5, 5))
plt.scatter(raw['dist'], raw['shot_distance'], color='blue')
plt.title('dist and shot_distance')
plt.show()

测试记录:
从图中我们可以看到,这两个特征值完全呈线性关系,此时我们只需要使用一个特征值即可。

2.4 使用group by查看数据分布情况

代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import KFold# 读取数据源
filename = "E:/file/data.csv"
raw = pd.read_csv(filename)
#print(raw.head(10))# 选择标签值不为null的
kobe = raw[pd.notnull(raw['shot_made_flag'])]# 聚合查看数据
gs = kobe.groupby('shot_zone_area')
print(kobe['shot_zone_area'].value_counts())
print (len(gs))

测试记录:

Center(C)                11289
Right Side Center(RC)     3981
Right Side(R)             3859
Left Side Center(LC)      3364
Left Side(L)              3132
Back Court(BC)              72
Name: shot_zone_area, dtype: int64
6

2.5 查看投篮区域和投篮范围的情况

代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import KFold
import matplotlib.cm as cm# 读取数据源
filename = "E:/file/data.csv"
raw = pd.read_csv(filename)
#print(raw.head(10))# 处理loc_x 为0的异常数据
loc_x_zero = raw['loc_x'] == 0
#print (loc_x_zero)
raw['angle'] = np.array([0]*len(raw))
raw['angle'][~loc_x_zero] = np.arctan(raw['loc_y'][~loc_x_zero] / raw['loc_x'][~loc_x_zero])
raw['angle'][loc_x_zero] = np.pi / 2# 将比赛剩余的分钟和秒  加起来,衍生一个特征
raw['remaining_time'] = raw['minutes_remaining'] * 60 + raw['seconds_remaining']# 选择标签值不为null的
kobe = raw[pd.notnull(raw['shot_made_flag'])]# 画图
plt.figure(figsize=(20, 10))def scatter_plot_by_category(feat):alpha = 0.1gs = kobe.groupby(feat)cs = cm.rainbow(np.linspace(0, 1, len(gs)))for g, c in zip(gs, cs):plt.scatter(g[1].loc_x, g[1].loc_y, color=c, alpha=alpha)# shot_zone_area
plt.subplot(131)
scatter_plot_by_category('shot_zone_area')
plt.title('shot_zone_area')# shot_zone_basic
plt.subplot(132)
scatter_plot_by_category('shot_zone_basic')
plt.title('shot_zone_basic')# shot_zone_range
plt.subplot(133)
scatter_plot_by_category('shot_zone_range')
plt.title('shot_zone_range')plt.show()

测试记录:

2.6 pandas独热编码

在对变量进行独热编码时使用,例如:某一列类别型变量是季节,取值为春、夏、秋、冬,当我们对其进行建模时,需要将其进行独热编码,这时:pandas.get_dummies便派上了用场。

data : array-like, Series, or DataFrame 输入的数据
prefix : string, get_dummies转换后,列名的前缀,默认为None
columns : 指定需要实现类别转换的列名 否则转换所有类别性的列
dummy_na : bool, default False 增加一列表示空缺值,如果False就忽略空缺值
drop_first : bool, default False 获得k中的k-1个类别值,去除第一个,防止出现多重共线性

三. 训练模型

代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import StratifiedKFold
import matplotlib.cm as cm
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import confusion_matrix, log_loss
import time# 读取数据源
filename = "E:/file/data.csv"
raw = pd.read_csv(filename)
#print(raw.head(10))# 将不需要的特征值进行删除
drops = ['shot_id', 'team_id', 'team_name', 'shot_zone_area', 'shot_zone_range', 'shot_zone_basic', \'matchup', 'lon', 'lat', 'seconds_remaining', 'minutes_remaining', \'shot_distance', 'loc_x', 'loc_y', 'game_event_id', 'game_id', 'game_date']
for drop in drops:raw = raw.drop(drop, 1)#print(raw['combined_shot_type'].value_counts())
#pd.get_dummies(raw['combined_shot_type'], prefix='combined_shot_type')[0:2]categorical_vars = ['action_type', 'combined_shot_type', 'shot_type', 'opponent', 'period', 'season']
for var in categorical_vars:raw = pd.concat([raw, pd.get_dummies(raw[var], prefix=var)], 1)raw = raw.drop(var, 1)# 划分训练集和测试集
train_kobe = raw[pd.notnull(raw['shot_made_flag'])]
train_label = train_kobe['shot_made_flag'].astype(np.int64)
train_kobe = train_kobe.drop('shot_made_flag', 1)
test_kobe = raw[pd.isnull(raw['shot_made_flag'])]
test_kobe = test_kobe.drop('shot_made_flag', 1)print('Finding best n_estimators for RandomForestClassifier...')
min_score = 100000
best_n = 0
scores_n = []
range_n = np.logspace(0, 2, num=3, dtype=np.int64).astype(np.int64)
for n in range_n:print("the number of trees : {0}".format(n))t1 = time.time()rfc_score = 0.rfc = RandomForestClassifier(n_estimators=n)skfolds = StratifiedKFold(n_splits=3,random_state=42,  # 设置随机种子shuffle=True)for train_k, test_k in skfolds.split(train_kobe, train_label):rfc.fit(train_kobe.iloc[train_k], train_label.iloc[train_k])# rfc_score += rfc.score(train.iloc[test_k], train_y.iloc[test_k])/10pred = rfc.predict(train_kobe.iloc[test_k])rfc_score += log_loss(train_label.iloc[test_k], pred) / 10scores_n.append(rfc_score)if rfc_score < min_score:min_score = rfc_scorebest_n = nt2 = time.time()print('Done processing {0} trees ({1:.3f}sec)'.format(n, t2 - t1))
print(best_n, min_score)# find best max_depth for RandomForestClassifier
print('Finding best max_depth for RandomForestClassifier...')
min_score = 100000
best_m = 0
scores_m = []
range_m = np.logspace(0, 2, num=3, dtype=np.int64).astype(np.int64)
for m in range_m:print("the max depth : {0}".format(m))t1 = time.time()skfolds = StratifiedKFold(n_splits=3,random_state=42,  # 设置随机种子shuffle=True)rfc_score = 0.rfc = RandomForestClassifier(max_depth=m, n_estimators=best_n)for train_k, test_k in skfolds.split(train_kobe, train_label):rfc.fit(train_kobe.iloc[train_k], train_label.iloc[train_k])# rfc_score += rfc.score(train.iloc[test_k], train_y.iloc[test_k])/10pred = rfc.predict(train_kobe.iloc[test_k])rfc_score += log_loss(train_label.iloc[test_k], pred) / 10scores_m.append(rfc_score)if rfc_score < min_score:min_score = rfc_scorebest_m = mt2 = time.time()print('Done processing {0} trees ({1:.3f}sec)'.format(m, t2 - t1))
print(best_m, min_score)plt.figure(figsize=(10, 5))
plt.subplot(121)
plt.plot(range_n, scores_n)
plt.ylabel('score')
plt.xlabel('number of trees')plt.subplot(122)
plt.plot(range_m, scores_m)
plt.ylabel('score')
plt.xlabel('max depth')
plt.show()

测试记录:

Finding best n_estimators for RandomForestClassifier...
the number of trees : 1Done processing 1 trees (0.556sec)
the number of trees : 10
Done processing 10 trees (1.138sec)
the number of trees : 100Done processing 100 trees (10.486sec)
100 3.9020328477633024
Finding best max_depth for RandomForestClassifier...
the max depth : 1Done processing 1 trees (1.294sec)
the max depth : 10Done processing 10 trees (3.215sec)
the max depth : 100Done processing 100 trees (10.556sec)
10 3.316936324282448

参考:

  1. https://study.163.com/course/introduction.htm?courseId=1003590004#/courseDetail?tab=1

Python数据分析与机器学习42-Python库分析科比生涯数据相关推荐

  1. 01、python数据分析与机器学习实战——python数据分析处理库-Pandas

    pandas介绍 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的. Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具. panda ...

  2. 01、python数据分析与机器学习实战——Python科学计算库-Numpy

    深度学习--学习目录 NumPy介绍 NumPy系统是Python的一种开源的数值计算扩展. 这种工具可用来存储和处理大型矩阵, 比Python自身的嵌套列表(nested list structur ...

  3. 01、python数据分析与机器学习实战——Python数据可视化库-Matplotlib

    Matplotlib介绍 Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形. Matplotlib基础 1.折线图绘制 假设,我 ...

  4. 【A-003】python数据分析与机器学习实战 Python科学计算库 Pandas数据分析处理库(二)

    目录: 处理缺失数据制作透视图删除含空数据的行和列多行索引使用apply函数 本节要处理的数据来自于泰坦尼克号的生存者名单,它的数据如下: PassengerId Survived Pclass .. ...

  5. 01、python数据分析与机器学习实战——Python可视化库Seaborn

    seaborn简介 Seaborn其实是在matplotlib的基础上进行了更高级的API封装,从而使得作图更加容易,在大多数情况下使用seaborn就能做出很具有吸引力的图. seaborn基础 i ...

  6. Python数据分析项目实例4:使用seaborn分析泰坦尼克号生还者数据

    泰坦尼克号生还者数据集下载(免费):https://download.csdn.net/download/weixin_44940488/20814899 使用的分析软件:jupyter notebo ...

  7. Python数据分析实战:降雨量统计分析报告分析

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理. 以下文章来源于菜J学Python ,作者小小明 最近遇到一个有点烧脑的需求,其实也不算烧pytho ...

  8. Python数据分析初探项目 基于Python数据可视化的网易云音乐歌单分析系统 大学编程作业(TUST 天津科技大学 2022年)

    Python 数据分析初探项目 基于 Python 数据可视化的网易云音乐歌单分析系统 大学编程作业(TUST 天津科技大学 2022 年) Python 数据分析初探项目 基于 Python 数据可 ...

  9. python数据分析和机器学习入门,我有一些书单来推荐

    想要快速入门python数据分析与机器学习,书籍是一个很好的门路,可以帮助我们系统的快速入门! 下面是一些不错的书单,分享给大家,我也在拔草中,未来会把阅读笔记分享在我的公众号:python数据分析和 ...

  10. 23神经网络 :唐宇迪《python数据分析与机器学习实战》学习笔记

    唐宇迪<python数据分析与机器学习实战>学习笔记 23神经网络 1.初识神经网络 百度深度学习研究院的图,当数据规模较小时差异较小,但当数据规模较大时深度学习算法的效率明显增加,目前大 ...

最新文章

  1. 谷歌兄弟公司Wing将于10月开始试点无人机配送
  2. Java期末复习——ch02基本类型(进制转换,数据类型转换,汉字编码)
  3. mysql命令:为mysql命令指定字符集
  4. st(state-threads) coroutine和stack分析
  5. Java基础查漏补缺:(String篇)一个面试题问倒了我,原来String并不简单
  6. Centos中不从skel目录里向其中复制任何文件错误的解决方法
  7. Oracle 10g Audit(审计) --- 记录登录用户在Oracle中的所有操作(转)
  8. 华为p10 鸿蒙,全面上线!华为鸿蒙新消息传来,这是要彻底替换安卓
  9. 对象流java_Java中的对象流总结(必看篇)
  10. esx4克隆后的处理工作
  11. VS 15 预览 5 中 VB 15 新增的功能
  12. windows纯手工安装php和Apache以及连接mysql
  13. np.c_和np.r_
  14. E20180418-hm
  15. 最轻松mflac转flac、mp3方法
  16. 如何激活微信里沉睡的客户?
  17. 幼儿园案例经验迁移_幼儿园故事教学的实施策略
  18. java 毫秒转分钟和秒_将毫秒转换为分钟和秒的java程序
  19. 两周自制编程语言读书总结
  20. ODL Netconf 连接器 + Netopeer

热门文章

  1. Web CAD SDK 14.1.0 New Crack
  2. 计算机里面为什么只剩c盘,电脑只剩下C盘了,怎么处理
  3. adventureworks mysql_AdventureWorks2012
  4. DirectShow笔记
  5. c语言读bmp格式图片的步骤,C语言读取BMP格式的图片
  6. php在线拍照代码,PHP+Javascript实现在线拍照功能实例_php技巧
  7. W25QXX FLASH介绍
  8. 如何在古董级IPAD上修改植物大战僵尸游戏金币
  9. python 物理引擎_第十六章:物理引擎
  10. matlab编程弹簧,弹簧设计计算程序分享