五十四、绝地求生吃鸡数据分析
·
数据链接:https://pan.baidu.com/s/1sZ4tC8dk4jMTBgheXzbhcg
提取码:3qev
复制这段内容后打开百度网盘手机App,操作更方便哦

import numpy as np
import pandas as pd
# 展示
import matplotlib.pyplot as plt
import seaborn as sns
from pdpbox import pdp
# Sklearn
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from IPython.display import display
from sklearn import metrics
- dbnos-被击倒的敌方玩家数量。
- 助攻-被队友击伤的敌方球员数量。
- 助推器-使用的助推器项目数。
- 损坏全部-造成的全部损坏。注:减去自身造成的伤害。
- headshotkills-用headshots杀死的敌方玩家数量。
- 治疗-使用的治疗项目数。
- ID-玩家ID
- 杀戮地点-在杀死的敌方玩家数量的比赛中排名。
- 杀戮点-基于玩家外部排名的杀戮。(把这看作是一个只有杀戮才重要的ELO排名。)如果rankpoints中有-1以外的值,那么任何0中的杀戮都应被视为“无”。
- 杀戮条纹-在短时间内杀死的敌人玩家的最大数量。
- 杀戮-被杀的敌方玩家数量。
- longestkill-死亡时玩家与被杀玩家之间的最长距离。这可能会误导玩家,因为击倒一名玩家并将其驱走可能会导致一个巨大的longestkill属性。
- matchDuration-匹配持续时间(秒)。
- matchID—标识匹配的ID。训练和测试集中都没有匹配项。
- matchType—标识数据来源的游戏模式的字符串。标准模式为“单人”、“双人”、“Squad”、“单人fpp”、“双人fpp”和“Squad fpp”;其他模式来自活动或自定义比赛。
- rankpoints-类似于ELO的玩家排名。这个排名是不一致的,在API的下一个版本中被弃用,因此请谨慎使用。值-1替换为“无”。
- 复活-这个球员复活队友的次数。
- 行驶距离-以米为单位的车辆总行驶距离。
- 道路撞车-车辆内的撞车次数。
- 游泳距离-以米为单位的游泳总距离。
- TeamKills-该玩家杀死队友的次数。
- 车辆类别-被摧毁的车辆数量。
- 步行距离-步行总距离,以米为单位。
- 所需武器-所携带武器的数量。
- Winpoints-基于Win的玩家外部排名。(把这看作是一个ELO排名,只有赢才重要。)如果rankpoints中有-1以外的值,那么winpoints中的任何0都应被视为“无”。
- group id-标识匹配中的组的ID。如果同一组玩家在不同的比赛中玩,他们每次都会有不同的groupid。
- numgroups-我们在匹配中有数据的组数。
- MaxPlace-我们在匹配中有数据的最差位置。这可能与numgroups不匹配,因为有时数据跳过放置。
- winplaceperc-预测的目标。这是一个百分位数的获胜位置,1代表第一名,0代表比赛的最后一名。它是根据maxplace而不是numgroups计算的,因此在匹配中可能有丢失的块。
train = pd.read_csv('train_V2.csv')
test = pd.read_csv('test_V2.csv')
train.head()
| Id | groupId | matchId | assists | boosts | damageDealt | DBNOs | headshotKills | heals | killPlace | ... | revives | rideDistance | roadKills | swimDistance | teamKills | vehicleDestroys | walkDistance | weaponsAcquired | winPoints | winPlacePerc | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 7f96b2f878858a | 4d4b580de459be | a10357fd1a4a91 | 0 | 0 | 0.00 | 0 | 0 | 0 | 60 | ... | 0 | 0.0000 | 0 | 0.00 | 0 | 0 | 244.80 | 1 | 1466 | 0.4444 |
| 1 | eef90569b9d03c | 684d5656442f9e | aeb375fc57110c | 0 | 0 | 91.47 | 0 | 0 | 0 | 57 | ... | 0 | 0.0045 | 0 | 11.04 | 0 | 0 | 1434.00 | 5 | 0 | 0.6400 |
| 2 | 1eaf90ac73de72 | 6a4a42c3245a74 | 110163d8bb94ae | 1 | 0 | 68.00 | 0 | 0 | 0 | 47 | ... | 0 | 0.0000 | 0 | 0.00 | 0 | 0 | 161.80 | 2 | 0 | 0.7755 |
| 3 | 4616d365dd2853 | a930a9c79cd721 | f1f1f4ef412d7e | 0 | 0 | 32.90 | 0 | 0 | 0 | 75 | ... | 0 | 0.0000 | 0 | 0.00 | 0 | 0 | 202.70 | 3 | 0 | 0.1667 |
| 4 | 315c96c26c9aac | de04010b3458dd | 6dc8ff871e21e6 | 0 | 0 | 100.00 | 0 | 0 | 0 | 45 | ... | 0 | 0.0000 | 0 | 0.00 | 0 | 0 | 49.75 | 2 | 0 | 0.1875 |
5 rows × 29 columns
train.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4446966 entries, 0 to 4446965
Data columns (total 29 columns):
Id object
groupId object
matchId object
assists int64
boosts int64
damageDealt float64
DBNOs int64
headshotKills int64
heals int64
killPlace int64
killPoints int64
kills int64
killStreaks int64
longestKill float64
matchDuration int64
matchType object
maxPlace int64
numGroups int64
rankPoints int64
revives int64
rideDistance float64
roadKills int64
swimDistance float64
teamKills int64
vehicleDestroys int64
walkDistance float64
weaponsAcquired int64
winPoints int64
winPlacePerc float64
dtypes: float64(6), int64(19), object(4)
memory usage: 983.9+ MB
train[train['winPlacePerc'].isnull()]
| Id | groupId | matchId | assists | boosts | damageDealt | DBNOs | headshotKills | heals | killPlace | ... | revives | rideDistance | roadKills | swimDistance | teamKills | vehicleDestroys | walkDistance | weaponsAcquired | winPoints | winPlacePerc | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2744604 | f70c74418bb064 | 12dfbede33f92b | 224a123c53e008 | 0 | 0 | 0.0 | 0 | 0 | 0 | 1 | ... | 0 | 0.0 | 0 | 0.0 | 0 | 0 | 0.0 | 0 | 0 | NaN |
1 rows × 29 columns
train.drop(2744604,inplace=True)
train[train['winPlacePerc'].isnull()]
| Id | groupId | matchId | assists | boosts | damageDealt | DBNOs | headshotKills | heals | killPlace | ... | revives | rideDistance | roadKills | swimDistance | teamKills | vehicleDestroys | walkDistance | weaponsAcquired | winPoints | winPlacePerc |
|---|
0 rows × 29 columns
train['playersJoined'] = train.groupby('matchId')['matchId'].transform('count')
plt.figure(figsize=(15,10))
sns.countplot(train[train['playersJoined']>=75]['playersJoined'])
plt.show()

train.head()
| Id | groupId | matchId | assists | boosts | damageDealt | DBNOs | headshotKills | heals | killPlace | ... | rideDistance | roadKills | swimDistance | teamKills | vehicleDestroys | walkDistance | weaponsAcquired | winPoints | winPlacePerc | playersJoined | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 7f96b2f878858a | 4d4b580de459be | a10357fd1a4a91 | 0 | 0 | 0.00 | 0 | 0 | 0 | 60 | ... | 0.0000 | 0 | 0.00 | 0 | 0 | 244.80 | 1 | 1466 | 0.4444 | 96 |
| 1 | eef90569b9d03c | 684d5656442f9e | aeb375fc57110c | 0 | 0 | 91.47 | 0 | 0 | 0 | 57 | ... | 0.0045 | 0 | 11.04 | 0 | 0 | 1434.00 | 5 | 0 | 0.6400 | 91 |
| 2 | 1eaf90ac73de72 | 6a4a42c3245a74 | 110163d8bb94ae | 1 | 0 | 68.00 | 0 | 0 | 0 | 47 | ... | 0.0000 | 0 | 0.00 | 0 | 0 | 161.80 | 2 | 0 | 0.7755 | 98 |
| 3 | 4616d365dd2853 | a930a9c79cd721 | f1f1f4ef412d7e | 0 | 0 | 32.90 | 0 | 0 | 0 | 75 | ... | 0.0000 | 0 | 0.00 | 0 | 0 | 202.70 | 3 | 0 | 0.1667 | 91 |
| 4 | 315c96c26c9aac | de04010b3458dd | 6dc8ff871e21e6 | 0 | 0 | 100.00 | 0 | 0 | 0 | 45 | ... | 0.0000 | 0 | 0.00 | 0 | 0 | 49.75 | 2 | 0 | 0.1875 | 97 |
5 rows × 30 columns
train['killsNorm'] = train['kills']*((100-train['playersJoined'])/100+1)
train['damageDealtNorm'] = train['damageDealt']*((100-train['playersJoined'])/100+1)
train['matchDurationNorm'] = train['matchDuration']*((100-train['playersJoined'])/100+1)
to_show = ['Id', 'kills','killsNorm','damageDealt', 'damageDealtNorm', 'matchDuration', 'matchDurationNorm']
train[to_show][:11]
| Id | kills | killsNorm | damageDealt | damageDealtNorm | matchDuration | matchDurationNorm | |
|---|---|---|---|---|---|---|---|
| 0 | 7f96b2f878858a | 0 | 0.00 | 0.000 | 0.00000 | 1306 | 1358.24 |
| 1 | eef90569b9d03c | 0 | 0.00 | 91.470 | 99.70230 | 1777 | 1936.93 |
| 2 | 1eaf90ac73de72 | 0 | 0.00 | 68.000 | 69.36000 | 1318 | 1344.36 |
| 3 | 4616d365dd2853 | 0 | 0.00 | 32.900 | 35.86100 | 1436 | 1565.24 |
| 4 | 315c96c26c9aac | 1 | 1.03 | 100.000 | 103.00000 | 1424 | 1466.72 |
| 5 | ff79c12f326506 | 1 | 1.05 | 100.000 | 105.00000 | 1395 | 1464.75 |
| 6 | 95959be0e21ca3 | 0 | 0.00 | 0.000 | 0.00000 | 1316 | 1355.48 |
| 7 | 311b84c6ff4390 | 0 | 0.00 | 8.538 | 8.87952 | 1967 | 2045.68 |
| 8 | 1a68204ccf9891 | 0 | 0.00 | 51.600 | 53.14800 | 1375 | 1416.25 |
| 9 | e5bb5a43587253 | 0 | 0.00 | 37.270 | 38.38810 | 1930 | 1987.90 |
| 10 | 2b574d43972813 | 0 | 0.00 | 28.380 | 28.66380 | 1811 | 1829.11 |
对于开挂的,应当给他们剔除掉
train['totalDistance'] = train['rideDistance'] + train['walkDistance'] + train['swimDistance']
train['killWithoutMoving'] = ((train['kills']>0)&(train['totalDistance']==0))
train['headshot_rate'] = train['headshotKills']/train['kills']
train['headshot_rate'] = train['headshot_rate'].fillna(0)
train[train['killWithoutMoving']==True].shape
(1535, 36)
train.drop(train[train['killWithoutMoving']==True].index,inplace=True)
train[train['roadKills']>1].shape
(1749, 36)
train.drop(train[train['roadKills']>10].index,inplace=True)
plt.figure(figsize=(15,10))
sns.countplot(data=train,x=train['kills']).set_title('Kills')
plt.show()

train[train['kills']>10].shape
(8120, 36)
train.drop(train[train['kills']>30].index,inplace=True)
plt.figure(figsize=(15,10))
sns.distplot(train['headshot_rate'],bins=10)
plt.show()
<Figure size 1080x720 with 0 Axes>

categorical变量
train['matchType'].nunique()
16
train = pd.get_dummies(train,columns=['matchType'])
train.head()
| Id | groupId | matchId | assists | boosts | damageDealt | DBNOs | headshotKills | heals | killPlace | ... | matchType_normal-duo | matchType_normal-duo-fpp | matchType_normal-solo | matchType_normal-solo-fpp | matchType_normal-squad | matchType_normal-squad-fpp | matchType_solo | matchType_solo-fpp | matchType_squad | matchType_squad-fpp | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 7f96b2f878858a | 4d4b580de459be | a10357fd1a4a91 | 0 | 0 | 0.00 | 0 | 0 | 0 | 60 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 1 | eef90569b9d03c | 684d5656442f9e | aeb375fc57110c | 0 | 0 | 91.47 | 0 | 0 | 0 | 57 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 2 | 1eaf90ac73de72 | 6a4a42c3245a74 | 110163d8bb94ae | 1 | 0 | 68.00 | 0 | 0 | 0 | 47 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 3 | 4616d365dd2853 | a930a9c79cd721 | f1f1f4ef412d7e | 0 | 0 | 32.90 | 0 | 0 | 0 | 75 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 4 | 315c96c26c9aac | de04010b3458dd | 6dc8ff871e21e6 | 0 | 0 | 100.00 | 0 | 0 | 0 | 45 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
5 rows × 51 columns
train['matchId'] = train['matchId'].astype('category')
train['groupId'] = train['groupId'].astype('category')
train.drop(columns=['Id'],inplace=True)
单排,双排,四排
solos=train[train['numGroups']>50]
duos=train[(train['numGroups']>25)&(train['numGroups']<=50)]
squads=train[train['numGroups']<=25]
len(solos)/len(train)
0.15947923787902407
len(duos)/len(train)
0.7412845072729117
f,ax =plt.subplots(figsize=(20,10))
sns.pointplot(x='kills',y='winPlacePerc',data=solos,color='black',alpha=0.8)
sns.pointplot(x='kills',y='winPlacePerc',data=duos,color='red',alpha=0.8)
sns.pointplot(x='kills',y='winPlacePerc',data=squads,color='blue',alpha=0.8)
plt.text(25,0.5,'Solos',color='red')
plt.grid()
plt.show()


corr
f,ax =plt.subplots(figsize=(15,15))
sns.heatmap(train.corr(),annot=True,linewidths=0.5,fmt='.1f',ax=ax)
plt.show()


k = 5
f,ax =plt.subplots(figsize=(12,12))
cols = train.corr().nlargest(k,'winPlacePerc')['winPlacePerc'].index
cm = np.corrcoef(train[cols].values.T)
sns.heatmap(cm,annot=True,linewidths=0.5,fmt='.1f',ax=ax,yticklabels=cols.values,xticklabels=cols.values)
plt.show()

建模
df_sample.head()
| assists | boosts | damageDealt | DBNOs | headshotKills | heals | killPlace | killPoints | kills | killStreaks | ... | matchType_normal-duo | matchType_normal-duo-fpp | matchType_normal-solo | matchType_normal-solo-fpp | matchType_normal-squad | matchType_normal-squad-fpp | matchType_solo | matchType_solo-fpp | matchType_squad | matchType_squad-fpp | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1980592 | 0 | 0 | 0.00 | 0 | 0 | 0 | 60 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
| 2448157 | 0 | 1 | 40.13 | 0 | 0 | 1 | 45 | 1024 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
| 2128667 | 0 | 0 | 0.00 | 0 | 0 | 0 | 70 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
| 3913225 | 0 | 0 | 0.00 | 0 | 0 | 0 | 79 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2980818 | 0 | 0 | 46.92 | 0 | 0 | 1 | 52 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
5 rows × 48 columns
sample = 500000
df_sample = train.sample(sample)
df_sample.drop(columns = ['groupId','matchId'],inplace=True)
df = df_sample.drop(columns=['winPlacePerc'])
y = df_sample['winPlacePerc']
X_train,X_valid,y_train,y_valid = train_test_split(df,y,random_state=1)
def print_score(m):
res= ['mae train',mean_absolute_error(m.predict(X_train),y_train),
'mae val',mean_absolute_error(m.predict(X_valid),y_valid)]
print (res)
from sklearn.metrics import mean_absolute_error
m1 = RandomForestRegressor(n_estimators=50,n_jobs=-1)
m1.fit(X_train,y_train)
print_score(m1)
['mae train', 0.021762396328252453, 'mae val', 0.058309784584455536]
m1.feature_importances_
array([1.35089375e-03, 4.87875324e-03, 3.32787366e-03, 3.68641782e-03,
3.82363312e-04, 2.64745662e-03, 1.80408541e-01, 2.32876452e-03,
2.69482315e-03, 2.92096788e-03, 5.21641404e-03, 8.68030154e-03,
5.49998219e-03, 9.88430257e-03, 4.29501554e-03, 7.88414670e-04,
1.91100321e-03, 4.66435583e-05, 6.18390314e-04, 2.47636965e-04,
8.75905989e-05, 6.78730778e-01, 3.83837336e-03, 2.63527017e-03,
1.83260527e-02, 9.11648154e-03, 3.63214124e-03, 1.17871960e-02,
2.64370862e-02, 0.00000000e+00, 6.58062557e-04, 1.93873550e-05,
1.03040286e-06, 2.40822626e-04, 4.06550376e-04, 4.99149059e-06,
1.03070725e-05, 1.61284732e-06, 3.84765144e-05, 5.58591717e-06,
1.44720204e-05, 1.58473274e-05, 1.79677780e-04, 1.27493547e-04,
2.55436001e-04, 1.11125703e-03, 5.03059736e-04])
def rf_feat_importance(m,df):
return pd.DataFrame({'cols':df.columns,'imp':m.feature_importances_}).sort_values('imp',ascending=False)
rf_feat_importance(m1,df)
| cols | imp | |
|---|---|---|
| 21 | walkDistance | 0.678731 |
| 6 | killPlace | 0.180409 |
| 28 | totalDistance | 0.026437 |
| 24 | playersJoined | 0.018326 |
| 27 | matchDurationNorm | 0.011787 |
| 13 | numGroups | 0.009884 |
| 25 | killsNorm | 0.009116 |
| 11 | matchDuration | 0.008680 |
| 12 | maxPlace | 0.005500 |
| 10 | longestKill | 0.005216 |
| 1 | boosts | 0.004879 |
| 14 | rankPoints | 0.004295 |
| 22 | weaponsAcquired | 0.003838 |
| 3 | DBNOs | 0.003686 |
| 26 | damageDealtNorm | 0.003632 |
| 2 | damageDealt | 0.003328 |
| 9 | killStreaks | 0.002921 |
| 8 | kills | 0.002695 |
| 5 | heals | 0.002647 |
| 23 | winPoints | 0.002635 |
| 7 | killPoints | 0.002329 |
| 16 | rideDistance | 0.001911 |
| 0 | assists | 0.001351 |
| 45 | matchType_squad | 0.001111 |
| 15 | revives | 0.000788 |
| 30 | headshot_rate | 0.000658 |
| 18 | swimDistance | 0.000618 |
| 46 | matchType_squad-fpp | 0.000503 |
| 34 | matchType_duo-fpp | 0.000407 |
| 4 | headshotKills | 0.000382 |
| 44 | matchType_solo-fpp | 0.000255 |
| 19 | teamKills | 0.000248 |
| 33 | matchType_duo | 0.000241 |
| 42 | matchType_normal-squad-fpp | 0.000180 |
| 43 | matchType_solo | 0.000127 |
| 20 | vehicleDestroys | 0.000088 |
| 17 | roadKills | 0.000047 |
| 38 | matchType_normal-duo-fpp | 0.000038 |
| 31 | matchType_crashfpp | 0.000019 |
| 41 | matchType_normal-squad | 0.000016 |
| 40 | matchType_normal-solo-fpp | 0.000014 |
| 36 | matchType_flaretpp | 0.000010 |
| 39 | matchType_normal-solo | 0.000006 |
| 35 | matchType_flarefpp | 0.000005 |
| 37 | matchType_normal-duo | 0.000002 |
| 32 | matchType_crashtpp | 0.000001 |
| 29 | killWithoutMoving | 0.000000 |
rf_feat_importance(m1,df)[:10].plot('cols','imp',figsize=(14,6),kind='barh')
plt.show()

fi=rf_feat_importance(m1,df)
to_keep = fi[fi.imp>0.02].cols
to_keep
21 walkDistance
6 killPlace
29 totalDistance
1 boosts
22 weaponsAcquired
5 heals
26 damageDealtNorm
2 damageDealt
16 rideDistance
10 longestKill
Name: cols, dtype: object
X_train,X_valid = X_train[to_keep],X_valid[to_keep]
m2 = RandomForestRegressor(n_estimators=50,n_jobs=-1)
m2.fit(X_train,y_train)
print_score(m2)
['mae train', 0.029341054777938774, 'mae val', 0.07770080750538866]
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐



所有评论(0)