科比数据集的处理和预测(机器学习)

练习:科比数据集的处理和预测

  • 数据导入

     import pandas as pd
     import matplotlib.pyplot as plt
    
    # 注意:pandas 通常不会完全显示
    pd.set_option('display.max_columns', None)          # 显示所有列
    pd.set_option('display.max_rows', None)             # 显示所有行
    pd.set_option('max_colwidth', 100)                  # 设置 value 的显示长度为100,默认为50
    pd.set_option('display.width',1000)                 # 当 console 中输出的列数超过1000的时候才会换行
    
    # import data
    filename= "data.csv"
    raw = pd.read_csv(filename)
    
    print (raw.shape)
    print(raw.head())
    

输出结果为:
  • 散点图,通过用图表的形式观察数据,可以看出有两列内容不同的数据,但实际表达的意思相同,所以数据处理时可以只保留其中一个数据

    # 测试数据集
    kobe = raw[pd.notnull(raw['shot_made_flag'])]
    print(kobe.shape)
    
    # 画图操作-投篮位置信息
    alpha = 0.02                          # 指透明度,设置时要注意它的值
    plt.figure(figsize=(10, 10))
    
    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()
    
散点图
  • 特征提取

    import numpy as np
    import pandas as pd
    
    file_name = 'data.csv'
    raw = pd.read_csv(file_name)
    
    # 特征提取,将坐标转换为极坐标
    kobe = raw[pd.notnull(raw['shot_made_flag'])]
    raw['dist'] = np.sqrt(raw['loc_x']**2 + raw['loc_y']**2)
    
    loc_x_zero = raw['loc_x'] == 0
    
    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']
    
    # unique 显示一列里所有不重复的值的集合
    print(kobe.action_type.unique())
    print(kobe.combined_shot_type.unique())
    
    print(kobe.shot_type.unique())
    print(kobe.shot_type.value_counts())
    
    print(kobe.season.unique())
    print(kobe.season.value_counts())
    

运行结果为:
# 画图
plt.figure(figsize=(5,5))

plt.scatter(raw.dist, raw.shot_distance, color='blue')
plt.title('dist and shot_distance')

plt.show()

# 查看科比的投篮区域次数
gs = kobe.groupby('shot_zone_area')
print(kobe['shot_zone_area'].value_counts())
print(len(gs))

运行结果为:
  • 画图-对科比投篮的区域进行统计

    # 画图-对科比投篮的区域进行统计
    plt.figure(figsize=(20,10))
    
    def scatter_plot_by_category(feat):
        alpha = 0.1
        gs = 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_area
    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()
    

运行结果如下:可以看出,以看出有三列内容不同的数据,但实际表达的意思相同,所以数据处理时可以只保留其中一个数据
  • 删除不重要的列,同时对一些字符进行one-hot处理

    # 删除不重要的列,同时对一些字符进行one-hot处理
    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())
     x = pd.get_dummies(raw['combined_shot_type'], prefix='combined_shot_type')[0:2]
     print(x)
    # 制定前缀为 combined_shot_type, 查看前面两项数据
    

运行结果为:


独热编码可以参考:https://www.cnblogs.com/lianyingteng/p/7755545.html

  • 开始训练模型,判断科比能否进球

    import numpy as np
    import pandas as pd
    import time
    
    from sklearn.ensemble import  RandomForestRegressor, RandomForestClassifier
    from sklearn.metrics import confusion_matrix, log_loss
    from sklearn.model_selection import KFold
    
    import  matplotlib.pyplot as plt
    
    file_name = 'data.csv'
    raw = pd.read_csv(file_name)
    
    # 删除不重要的列,同时对一些字符进行one-hot处理
    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)
    
    # one-hot(独热编码)
    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)       # concat 为拼接操作
        raw = raw.drop(var, 1)
    
    # 至此数据的整理已经完成,下面开始训练模型,目的是判断科比是否可以进球
    # 这里把'shot_made_flag'里的5000个有缺失值得数据当做测试集
    train_kobe = raw[pd.notnull(raw['shot_made_flag'])]
    train_label = train_kobe['shot_made_flag']
    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)
    
    # 用随机森林训练模型,为了方便,森林的宽度和深度用了3个值(1,10,100)
    print('Finding best n_estimators for RandomForestClassifier...')
    min_score = 100000
    best_n = 0
    scores_n = []
    range_n = np.logspace(0,2,num=3).astype(int)                              # 建造一个从1~100的等比数列
    kf = KFold(n_splits=10, shuffle=True)
    
    for n in range_n:
        print('the number of trees : {0}'.format(n))
        t1 = time.time()
    
        rfc_score = 0.
        rfc = RandomForestRegressor(n_estimators=n)                           # 随机森林分类器建立一个模型
        for train_k, test_k in kf.split(train_kobe):
            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])/10
            pred = rfc.predict(train_kobe.iloc[test_k])                       # 对模型进行预测
            rfc_score += log_loss(train_label.iloc[test_k], pred) / 10        # 对模型进行评估
        scores_n.append(rfc_score)
        if rfc_score < min_score:
            min_score = rfc_score
            best_n = n
        t2 = time.time()
        print('Done processing {0} trees ({1:.3f}sec)'.format(n, t2 - t1))
    print(best_n, min_score)
    
    # find the 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).astype(int)
    kf = KFold(n_splits=10, shuffle=True)
    
    for m in range_m:
        print("the max depth : {0}".format(m))
        t1 = time.time()
    
        rfc_score = 0.
        rfc = RandomForestClassifier(max_depth=m, n_estimators=best_n)
        for train_k, test_k in kf.split(train_kobe):
            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])/10
            pred = rfc.predict(train_kobe.iloc[test_k])
            rfc_score += log_loss(train_label.iloc[test_k], pred) / 10
        scores_m.append(rfc_score)
        if rfc_score < min_score:
            min_score = rfc_score
            best_m = m
    
        t2 = 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()
    
    model = RandomForestClassifier(n_estimators=best_n, max_depth=best_m)
    model.fit(train_kobe, train_label)
    # 已经拿到模型了,可以对其进行预测
    

输出结果为:
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,591评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,448评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,823评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,204评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,228评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,190评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,078评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,923评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,334评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,550评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,727评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,428评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,022评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,672评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,826评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,734评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,619评论 2 354