Python大数据分析LOL游戏胜率
基于數據的LOL游戲勝利預測
目錄:
文章目錄
- 基于數據的LOL游戲勝利預測
- 目錄:
- 1.背景
- a.LOL簡介:
- 游戲規則簡介:
- b.對其進行數據分析的可行性和目的:
- 2.數據簡介和展示:
- 簡介:
- 數據展示:
- 數據結構:
- 數據名詞詳解:
- 數據清洗處理:
- 1.檢查數據是否規范和有缺失:
- **刪除有空值的行:**
- 查看數據是否規范:
- 2.對數據進行處理:
- **預處理:**
- **熱力圖分析處理:**
- 數據分析和建模(Logistic Regression):
- 1.數據分析處理:
- 1.初步處理后數據:
- 2.一般分析處理:
- 2.數據建模:
- 處理后數據:
- 邏輯回歸簡介:
- 分析:
- 標準化數據:
- 數據切片:
- 模型訓練分析:
- 數據測試:
- 結果分析:
- 源碼:
- 數據下載地址:
1.背景
a.LOL簡介:
英雄聯盟(LOL)是一個MOBA(多人在線戰斗競技場),其中2支隊伍(藍色和紅色)對峙。有3條車道,一個叢林和5個角色。目標是擊倒敵方水晶以贏得比賽.
- 名詞解釋:
- Warding totem:(視野眼) 玩家可以放置在地圖上以顯示附近區域的物品。對于地圖/目標控制非常有用。
- Minions: (小兵)屬于兩個團隊的NPC。當被玩家殺死時,他們給予金幣。
- Jungle minions: 叢林NPC。當被玩家殺死時,他們會給予金幣和增益。
- Elite monsters: 具有很高的血量和傷害的怪獸,在被團隊殺死時會給予巨額獎勵(金幣/ XP /屬性)。
- Dragons: 精英怪獸,被殺死后會給予團隊加成。被團隊殺死的第四條龍給予了巨大的屬性加值。第五龍(長者龍)為球隊提供了巨大的優勢
- Herald: 精英怪物,被玩家殺死后會給予屬性加成。它有助于推開車道并破壞建筑物
- Towers:(防御塔) 您必須摧毀的結構才能到達敵方水晶。他們給金幣。
- Level:等級,從1開始,最大為18。
游戲規則簡介:
玩家—>殺死小兵和野怪—>獲得金幣和buff–>摧毀敵方防御塔------>摧毀敵方水晶(獲得勝利)
? | | |
? | | 擊殺敵人
? 放置視野眼,獲得視野優勢 | |
? |__—>購買裝備,提升實力
?
b.對其進行數據分析的可行性和目的:
隨著網絡游戲在年輕人中的盛行,電子競技也變得越來越流行.LOL(英雄聯盟)作為電子競技的代表游戲之一,受到越來越多年輕人的關注和喜愛
-
可行性:在每一次比賽的過程中,最終的勝利受到許許多多因素的影響,不可否認,玩家的操作技術和意識是決定比賽輸贏的關鍵因素,但玩家的所有自身實力都會反映在游戲中的數據里面,而LOL又是一款團隊競技游戲,因此,一局比賽進行到后期時的數據能夠比較好的反映出操作者的水平和團隊間的配合.此時的數據就可以比較準確的用來對比賽的輸贏進行預測.
-
目的:通過對LOL數據的分析,不僅可以用來對比賽的輸贏進行預測,同時,也可以發現對一局比賽輸贏影響較大的因素,從而對現實生活中的比賽具有指導意義.
2.數據簡介和展示:
簡介:
-
數據來源:網絡
-
**數據集簡介:**此數據集包含前10分鐘大約統計 從高ELO(鉆石I到大師)的10k次排位游戲。玩家的水平大致相同。游戲開始10分鐘后,每支隊伍收集了19項數據(總共38項)。其中包括殺戮,死亡,金錢,經驗,等級……
數據展示:
data = pd.read_csv('high_diamond_ranked_10min.csv', index_col=0) print(data.head()) gameId blueWins blueWardsPlaced blueWardsDestroyed blueFirstBlood \ 0 4519157822 0 28 2 1 1 4523371949 0 12 1 0 2 4521474530 0 15 0 0 3 4524384067 0 43 1 0 4 4436033771 0 75 4 0 blueKills blueDeaths blueAssists blueEliteMonsters blueDragons \ 0 9 6 11 0 0 1 5 5 5 0 0 2 7 11 4 1 1 3 4 5 5 1 0 4 6 6 6 0 0 blueHeralds blueTowersDestroyed blueTotalGold blueAvgLevel \ 0 0 0 17210 6.6 1 0 0 14712 6.6 2 0 0 16113 6.4 3 1 0 15157 7.0 4 0 0 16400 7.0 blueTotalExperience blueTotalMinionsKilled blueTotalJungleMinionsKilled \ 0 17039 195 36 1 16265 174 43 2 16221 186 46 3 17954 201 55 4 18543 210 57 blueGoldDiff blueExperienceDiff blueCSPerMin blueGoldPerMin \ 0 643 -8 19.5 1721.0 1 -2908 -1173 17.4 1471.2 2 -1172 -1033 18.6 1611.3 3 -1321 -7 20.1 1515.7 4 -1004 230 21.0 1640.0 redWardsPlaced redWardsDestroyed redFirstBlood redKills redDeaths \ 0 15 6 0 6 9 1 12 1 1 5 5 2 15 3 1 11 7 3 15 2 1 5 4 4 17 2 1 6 6 redAssists redEliteMonsters redDragons redHeralds redTowersDestroyed \ 0 8 0 0 0 0 1 2 2 1 1 1 2 14 0 0 0 0 3 10 0 0 0 0 4 7 1 1 0 0 redTotalGold redAvgLevel redTotalExperience redTotalMinionsKilled \ 0 16567 6.8 17047 197 1 17620 6.8 17438 240 2 17285 6.8 17254 203 3 16478 7.0 17961 235 4 17404 7.0 18313 225 redTotalJungleMinionsKilled redGoldDiff redExperienceDiff redCSPerMin \ 0 55 -643 8 19.7 1 52 2908 1173 24.0 2 28 1172 1033 20.3 3 47 1321 7 23.5 4 67 1004 -230 22.5 redGoldPerMin 0 1656.7 1 1762.0 2 1728.5 3 1647.8 4 1740.4數據結構:
print(data.shape) 數據形狀: (9879, 40) #集合共包含9879個元數據,每個數據有40列 print(data.describe) 數據概覽: blueWins blueWardsPlaced blueWardsDestroyed blueFirstBlood \ count 9879.000000 9879.000000 9879.000000 9879.000000 mean 0.499038 22.288288 2.824881 0.504808 std 0.500024 18.019177 2.174998 0.500002 min 0.000000 5.000000 0.000000 0.000000 25% 0.000000 14.000000 1.000000 0.000000 50% 0.000000 16.000000 3.000000 1.000000 75% 1.000000 20.000000 4.000000 1.000000 max 1.000000 250.000000 27.000000 1.000000 blueKills blueDeaths blueAssists blueEliteMonsters blueDragons \ count 9879.000000 9879.000000 9879.000000 9879.000000 9879.000000 mean 6.183925 6.137666 6.645106 0.549954 0.361980 std 3.011028 2.933818 4.064520 0.625527 0.480597 min 0.000000 0.000000 0.000000 0.000000 0.000000 25% 4.000000 4.000000 4.000000 0.000000 0.000000 50% 6.000000 6.000000 6.000000 0.000000 0.000000 75% 8.000000 8.000000 9.000000 1.000000 1.000000 max 22.000000 22.000000 29.000000 2.000000 1.000000 blueHeralds blueTowersDestroyed blueTotalGold blueAvgLevel \ count 9879.000000 9879.000000 9879.000000 9879.000000 mean 0.187974 0.051422 16503.455512 6.916004 std 0.390712 0.244369 1535.446636 0.305146 min 0.000000 0.000000 10730.000000 4.600000 25% 0.000000 0.000000 15415.500000 6.800000 50% 0.000000 0.000000 16398.000000 7.000000 75% 0.000000 0.000000 17459.000000 7.200000 max 1.000000 4.000000 23701.000000 8.000000 blueTotalExperience blueTotalMinionsKilled \ count 9879.000000 9879.000000 mean 17928.110133 216.699565 std 1200.523764 21.858437 min 10098.000000 90.000000 25% 17168.000000 202.000000 50% 17951.000000 218.000000 75% 18724.000000 232.000000 max 22224.000000 283.000000 blueTotalJungleMinionsKilled blueGoldDiff blueExperienceDiff \ count 9879.000000 9879.000000 9879.000000 mean 50.509667 14.414111 -33.620306 std 9.898282 2453.349179 1920.370438 min 0.000000 -10830.000000 -9333.000000 25% 44.000000 -1585.500000 -1290.500000 50% 50.000000 14.000000 -28.000000 75% 56.000000 1596.000000 1212.000000 max 92.000000 11467.000000 8348.000000 blueCSPerMin blueGoldPerMin redWardsPlaced redWardsDestroyed \ count 9879.000000 9879.000000 9879.000000 9879.000000 mean 21.669956 1650.345551 22.367952 2.723150 std 2.185844 153.544664 18.457427 2.138356 min 9.000000 1073.000000 6.000000 0.000000 25% 20.200000 1541.550000 14.000000 1.000000 50% 21.800000 1639.800000 16.000000 2.000000 75% 23.200000 1745.900000 20.000000 4.000000 max 28.300000 2370.100000 276.000000 24.000000 redFirstBlood redKills redDeaths redAssists redEliteMonsters \ count 9879.000000 9879.000000 9879.000000 9879.000000 9879.000000 mean 0.495192 6.137666 6.183925 6.662112 0.573135 std 0.500002 2.933818 3.011028 4.060612 0.626482 min 0.000000 0.000000 0.000000 0.000000 0.000000 25% 0.000000 4.000000 4.000000 4.000000 0.000000 50% 0.000000 6.000000 6.000000 6.000000 0.000000 75% 1.000000 8.000000 8.000000 9.000000 1.000000 max 1.000000 22.000000 22.000000 28.000000 2.000000 redDragons redHeralds redTowersDestroyed redTotalGold \ count 9879.000000 9879.000000 9879.000000 9879.000000 mean 0.413098 0.160036 0.043021 16489.041401 std 0.492415 0.366658 0.216900 1490.888406 min 0.000000 0.000000 0.000000 11212.000000 25% 0.000000 0.000000 0.000000 15427.500000 50% 0.000000 0.000000 0.000000 16378.000000 75% 1.000000 0.000000 0.000000 17418.500000 max 1.000000 1.000000 2.000000 22732.000000 redAvgLevel redTotalExperience redTotalMinionsKilled \ count 9879.000000 9879.000000 9879.000000 mean 6.925316 17961.730438 217.349226 std 0.305311 1198.583912 21.911668 min 4.800000 10465.000000 107.000000 25% 6.800000 17209.500000 203.000000 50% 7.000000 17974.000000 218.000000 75% 7.200000 18764.500000 233.000000 max 8.200000 22269.000000 289.000000 redTotalJungleMinionsKilled redGoldDiff redExperienceDiff \ count 9879.000000 9879.000000 9879.000000 mean 51.313088 -14.414111 33.620306 std 10.027885 2453.349179 1920.370438 min 4.000000 -11467.000000 -8348.000000 25% 44.000000 -1596.000000 -1212.000000 50% 51.000000 -14.000000 28.000000 75% 57.000000 1585.500000 1290.500000 max 92.000000 10830.000000 9333.000000 redCSPerMin redGoldPerMin count 9879.000000 9879.000000 mean 21.734923 1648.904140 std 2.191167 149.088841 min 10.700000 1121.200000 25% 20.300000 1542.750000 50% 21.800000 1637.800000 75% 23.300000 1741.850000 max 28.900000 2273.200000數據名詞詳解:
pd.set_option('display.width', 10) #設置Console每一行展示的最大寬度,屏幕一行顯示滿之后才會進行換行 print("數據列名:",data.columns) Index(['gameId', #每局游戲的唯一ID。#--------------------------------------------------------------------'blueWins', #藍方是否獲得勝利 1:勝利 0:失敗 *****因變量****#--------------------------------------------------------------------19項'blueWardsPlaced', #藍色團隊在地圖上放置的視野眼數量'blueWardsDestroyed', #藍隊摧毀的敵方視野眼數量'blueFirstBlood', #藍方是否獲得一血(游戲的第一殺) 1:獲得 0:未獲得'blueKills', #藍隊殺死的敵人數量'blueDeaths', #死亡人數(藍隊)'blueAssists', #擊殺助攻數(藍隊)'blueEliteMonsters', #藍隊殺死的精銳怪物數量(龍與先驅隊)'blueDragons', #藍隊殺死的龍數量'blueHeralds', #藍隊殺死的精英怪物數量'blueTowersDestroyed', #藍隊摧毀防御塔數量'blueTotalGold', #藍隊總的金幣數量'blueAvgLevel', #藍隊平均等級'blueTotalExperience', #藍隊總的經驗'blueTotalMinionsKilled', #藍隊殺死的小兵總數'blueTotalJungleMinionsKilled', #藍隊殺死的野怪總數'blueGoldDiff', #藍隊金幣與紅隊差值'blueExperienceDiff', #藍隊經驗差值'blueCSPerMin', #藍隊每分鐘摧毀視野眼數量'blueGoldPerMin', #藍隊每分鐘獲得金幣數量#紅方與藍方相同--------------------------------------------------------19項'redWardsPlaced', 'redWardsDestroyed','redFirstBlood','redKills','redDeaths','redAssists','redEliteMonsters','redDragons','redHeralds','redTowersDestroyed','redTotalGold','redAvgLevel','redTotalExperience','redTotalMinionsKilled','redTotalJungleMinionsKilled','redGoldDiff','redExperienceDiff','redCSPerMin','redGoldPerMin'],數據清洗處理:
1.檢查數據是否規范和有缺失:
刪除有空值的行:
data.dropna(axis=0, how='any', inplace=True)查看數據是否規范:
print("數據概覽:",data.info()) <class 'pandas.core.frame.DataFrame'> Int64Index: 9879 entries, 0 to 9878 Data columns (total 40 columns):# Column Non-Null Count Dtype --- ------ -------------- ----- 0 gameId 9879 non-null int64 1 blueWins 9879 non-null int64 2 blueWardsPlaced 9879 non-null int64 3 blueWardsDestroyed 9879 non-null int64 4 blueFirstBlood 9879 non-null int64 5 blueKills 9879 non-null int64 6 blueDeaths 9879 non-null int64 7 blueAssists 9879 non-null int64 8 blueEliteMonsters 9879 non-null int64 9 blueDragons 9879 non-null int64 10 blueHeralds 9879 non-null int64 11 blueTowersDestroyed 9879 non-null int64 12 blueTotalGold 9879 non-null int64 13 blueAvgLevel 9879 non-null float6414 blueTotalExperience 9879 non-null int64 15 blueTotalMinionsKilled 9879 non-null int64 16 blueTotalJungleMinionsKilled 9879 non-null int64 17 blueGoldDiff 9879 non-null int64 18 blueExperienceDiff 9879 non-null int64 19 blueCSPerMin 9879 non-null float6420 blueGoldPerMin 9879 non-null float6421 redWardsPlaced 9879 non-null int64 22 redWardsDestroyed 9879 non-null int64 23 redFirstBlood 9879 non-null int64 24 redKills 9879 non-null int64 25 redDeaths 9879 non-null int64 26 redAssists 9879 non-null int64 27 redEliteMonsters 9879 non-null int64 28 redDragons 9879 non-null int64 29 redHeralds 9879 non-null int64 30 redTowersDestroyed 9879 non-null int64 31 redTotalGold 9879 non-null int64 32 redAvgLevel 9879 non-null float6433 redTotalExperience 9879 non-null int64 34 redTotalMinionsKilled 9879 non-null int64 35 redTotalJungleMinionsKilled 9879 non-null int64 36 redGoldDiff 9879 non-null int64 37 redExperienceDiff 9879 non-null int64 38 redCSPerMin 9879 non-null float6439 redGoldPerMin 9879 non-null float64 dtypes: float64(6), int64(34)2.對數據進行處理:
預處理:
由于gameId與游戲勝利無關,因此刪去
data=data.drop(['gameId'], axis=1)熱力圖分析處理:
在相關性矩陣的熱力圖中可以發現存在高度相關的變量,這些變量解釋了相同的事物。因此,如果它們顯示的數據與另一列相同,則它們對分類沒有幫助。例如在列 RedKills(紅色團隊擊殺的次數)和BlueDeaths(藍隊被擊殺的人數)中。紅隊的擊殺人數就是藍隊的死亡人數。因此,正確的做法是刪除一個列。注:(有時在游戲redkills和bluedeaths不一定相等,因為玩家可能會被野怪和防御塔殺死,但我們的數據來自于高段位玩家,這種情況可以忽略不記)
plt.figure(figsize=(20,15)) sns.heatmap(round(data.corr(),1), cmap="coolwarm", annot=True, linewidths=.5) plt.savefig('熱力圖相關性分析.jpg', bbox_inches='tight') # data.corr():計算列與列之間的相關系數,返回相關系數矩陣 # sns.heatmap():利用seaborn繪制變量之間相關性的熱力圖將數據中相關性較高的數據刪去,降低分析難度
#定義一個函數 作用:找出相關系數矩陣中相關性大的一組數據,同時返回其中一列數據 def remove_redundancy(r):to_remove = []for i in range(len(r.columns)):for j in range(i):if (abs(r.iloc[i,j]) >= 1 and (r.columns[j] not in to_remove)):print("相關性:",r.iloc[i,j], r.columns[j], r.columns[i])to_remove.append(r.columns[i])return to_removeclean_data = data.drop(remove_redundancy(data.corr()), axis=1) #刪去相關性較高項這幾組數據的本質是一樣的,故刪去
相關性: 1.000000000000002 blueTotalMinionsKilled blueCSPerMin 相關性: 1.0000000000000013 blueTotalGold blueGoldPerMin 相關性: -1.0 blueFirstBlood redFirstBlood 相關性: 1.0 blueDeaths redKills 相關性: 1.0 blueKills redDeaths 相關性: -1.0 blueGoldDiff redGoldDiff 相關性: -1.0 blueExperienceDiff redExperienceDiff 相關性: 1.0000000000000042 redTotalMinionsKilled redCSPerMin 相關性: 1.0000000000000049 redTotalGold redGoldPerMin數據分析和建模(Logistic Regression):
1.數據分析處理:
1.初步處理后數據:
print("初步處理后的數據:",clean_data.columns)blueEliteMonsters redEliteMonsters 初步處理后的數據: Index(['blueWins','blueWardsPlaced','blueWardsDestroyed','blueFirstBlood','blueKills','blueDeaths','blueAssists','blueEliteMonsters','blueDragons','blueHeralds','blueTowersDestroyed','blueTotalGold','blueAvgLevel','blueTotalExperience','blueTotalMinionsKilled','blueTotalJungleMinionsKilled','blueGoldDiff','blueExperienceDiff',#---------------------------------------------------------- 'redWardsPlaced','redWardsDestroyed','redAssists','redEliteMonsters','redDragons','redHeralds','redTowersDestroyed','redTotalGold','redAvgLevel','redTotalExperience','redTotalMinionsKilled','redTotalJungleMinionsKilled'],dtype='object')2.一般分析處理:
- 由于在游戲中擊殺野怪和小兵都是獲得經驗和金幣,因此將野怪和小兵的擊殺數合并起來
-
由熱力圖分析可知,等級和經驗的相關性較高,故進行分析:
#等級和經驗分析: plt.figure(figsize=(12,12)) plt.subplot(121) sns.scatterplot(x='blueAvgLevel', y='blueTotalExperience', hue='blueWins', data=clean_data) plt.title('blue') plt.xlabel('blueAvgLevel') plt.ylabel('blueTotalExperience') plt.grid(True) plt.subplot(122) sns.scatterplot(x='redAvgLevel', y='redTotalExperience', hue='blueWins', data=clean_data) plt.title('red') plt.xlabel('redAvgLevel') plt.ylabel('redTotalExperience') plt.grid(True) plt.savefig('等級和經驗分析.jpg', bbox_inches='tight')
可看出等級和經驗呈線性關系,并且具有很強的相關性(見熱力圖),同時由于等級的差異不明顯,故刪去等級
#刪去等級列 clean_data=clean_data.drop(['blueAvgLevel'], axis=1) clean_data=clean_data.drop(['redAvgLevel'], axis=1)-
數據可視化分析:
sns.set(font_scale=1.5) plt.figure(figsize=(20,20)) sns.set_style("whitegrid")# 擊殺和被擊殺數繪制散點圖 plt.subplot(321) sns.scatterplot(x='blueKills', y='blueDeaths', hue='blueWins', data=clean_data) plt.title('blueKills&&blueDeaths') plt.xlabel('blueKills') plt.ylabel('blueDeaths') plt.grid(True)# 助攻數繪制散點圖 plt.subplot(322) sns.scatterplot(x='blueAssists', y='redAssists', hue='blueWins', data=clean_data) plt.title('Assists') plt.xlabel('blueAssists') plt.ylabel('redAssists') plt.tight_layout(pad=1.5) plt.grid(True)#雙方金幣數繪制散點圖 plt.subplot(323) sns.scatterplot(x='blueTotalGold', y='redTotalGold', hue='blueWins', data=clean_data) plt.title('TotalGold') plt.xlabel('blueTotalGold') plt.ylabel('redTotalGold') plt.tight_layout(pad=1.5) plt.grid(True)#雙方經驗繪制散點圖 plt.subplot(324) sns.scatterplot(x='blueTotalExperience', y='redTotalExperience', hue='blueWins', data=clean_data) plt.title('Experience') plt.xlabel('blueTotalExperience') plt.ylabel('redTotalExperience') plt.tight_layout(pad=1.5) plt.grid(True)# 雙方插眼數量繪制散點圖 plt.subplot(325) sns.scatterplot(x='blueWardsPlaced', y='redWardsPlaced', hue='blueWins', data=clean_data) plt.title('WardsPlaced') plt.xlabel('blueWardsPlaced') plt.ylabel('redWardsPlaced') plt.tight_layout(pad=1.5) plt.grid(True)# 擊殺的小兵和野怪總數繪制散點圖 plt.subplot(326) sns.scatterplot(x='blueMinionsTotales', y='redMinionsTotales', hue='blueWins', data=clean_data) plt.title('MinionsTotales') plt.xlabel('Equipo Azul') plt.ylabel('Equipo Rojo') plt.tight_layout(pad=1.5) plt.grid(True) plt.savefig('數據分析.jpg', bbox_inches='tight')
?
- 由于在游戲中blueWardsPlaced 和redWardsPlaced, blueWardsDestroyed和redWardsDestroyed, blueEliteMonsters和redEliteMonsters等數據的值不大,并且比賽的勝利多與其之間的差值有關,同時數據與比賽雙方息息相關,因此,將兩個值用他們之間的差值來展示(減小數據量).
-
blueFirstBlood,blueDragonsDiff ,EliteMonstersDiff分析:
#一血,龍與精英怪物分析 sns.catplot(x="blueWins", y="blueGoldDiff", hue="blueFirstBlood", data=clean_data) plt.savefig('一血.jpg', bbox_inches='tight') sns.catplot(x="blueWins", y="blueGoldDiff", hue="blueDragonsDiff", data=clean_data) plt.savefig('龍.jpg', bbox_inches='tight') sns.catplot(x="blueWins", y="blueGoldDiff", hue="EliteMonstersDiff", data=clean_data) plt.savefig('精英怪物.jpg', bbox_inches='tight')一血:
擊殺龍的差值:
擊殺精英怪物的差值:
2.數據建模:
處理后數據:
最終數據: Index(['blueWins', #藍方是否獲得勝利 1:勝利 0:失敗 *****因變量****'blueFirstBlood', #藍方是否獲得一血(游戲的第一殺) 1:獲得 0:未獲得'blueKills', #藍隊殺死的敵人數量'blueDeaths', #死亡人數(藍隊)'blueTotalGold', #藍隊總的金幣數量'blueTotalExperience', #藍隊總的經驗'blueGoldDiff', #藍隊與紅隊金幣差值'blueExperienceDiff', #藍隊與紅隊經驗差值'blueMinionsTotales', #藍隊殺死的野怪和小兵總數量'redMinionsTotales', #紅隊殺死的野怪和小兵總數量'WardsPlacedDiff', #兩隊在地圖上放置的視野眼數量差異'WardsDestroyedDiff', #兩隊在地圖上摧毀的視野眼數量差異'AssistsDiff', #兩隊助攻差異'blueHeraldsDiff', #兩隊殺死的精英怪物數量差異'blueDragonsDiff', #兩隊殺死的龍數量差異'blueTowersDestroyedDiff', #兩隊摧毀防御塔數量差異'EliteMonstersDiff'], #兩隊殺死的精銳怪物數量(龍與先驅隊)差異dtype='object') blueWins blueFirstBlood blueKills blueDeaths blueTotalGold \ 0 0 1 9 6 17210 1 0 0 5 5 14712 2 0 0 7 11 16113 3 0 0 4 5 15157 4 0 0 6 6 16400 ... ... ... ... ... 9874 1 1 7 4 17765 9875 1 0 6 4 16238 9876 0 0 6 7 15903 9877 0 1 2 3 14459 9878 1 1 6 6 16266 blueTotalExperience blueGoldDiff blueExperienceDiff \ 0 17039 643 -8 1 16265 -2908 -1173 2 16221 -1172 -1033 3 17954 -1321 -7 4 18543 -1004 230 ... ... ... 9874 18967 2519 2469 9875 19255 782 888 9876 18032 -2416 -1877 9877 17229 -839 -1085 9878 17321 927 -58 blueMinionsTotales redMinionsTotales WardsPlacedDiff \ 0 231 252 13 1 217 292 0 2 232 231 0 3 256 282 28 4 267 292 58 ... ... ... 9874 280 263 -29 9875 281 262 42 9876 255 321 9 9877 272 287 -52 9878 251 247 9 WardsDestroyedDiff AssistsDiff blueHeraldsDiff blueDragonsDiff \ 0 -4 3 0 0 1 0 3 -1 -1 2 -3 -10 0 1 3 -1 -5 1 0 4 2 -1 0 -1 ... ... ... ... 9874 -1 -2 0 1 9875 -21 5 0 1 9876 1 -6 0 -1 9877 0 2 0 1 9878 -2 1 0 -1 blueTowersDestroyedDiff EliteMonstersDiff 0 0 0 1 -1 -2 2 0 1 3 0 1 4 0 -1 ... ... 9874 0 1 9875 0 1 9876 0 -1 9877 0 1 9878 0 -1邏輯回歸簡介:
logistic回歸又稱logistic回歸分析,是一種廣義的線性回歸分析模型,常用于數據挖掘,疾病自動診斷,經濟預測等領域。例如,探討引發疾病的危險因素,并根據危險因素預測疾病發生的概率等。以胃癌病情分析為例,選擇兩組人群,一組是胃癌組,一組是非胃癌組,兩組人群必定具有不同的體征與生活方式等。因此因變量就為是否胃癌,值為“是”或“否”,自變量就可以包括很多了,如年齡、性別、飲食習慣、幽門螺桿菌感染等。自變量既可以是連續的,也可以是分類的。然后通過logistic回歸分析,可以得到自變量的權重,從而可以大致了解到底哪些因素是胃癌的危險因素。同時根據該權值可以根據危險因素預測一個人患癌癥的可能性。
分析:
在對LOL游戲勝利預測的分析中,有非常多的自變量,而應變量只有blueWins,即游戲是否取得勝利這一個應變量,值為“是”或“否”,因此,宜采用Logistic Regression模型進行分析.
標準化數據:
簡介:數據的標準化(normalization)是將數據按比例縮放,使之落入一個小的特定區間。在某些比較和評價的指標處理中經常會用到,去除數據的單位限制,將其轉化為無量綱的純數值,便于不同單位或量級的指標能夠進行比較和加權。其中最典型的就是數據的歸一化處理,即將數據統一映射到[0,1]區間上。數據集的標準化對于眾多機器學習評估器來說是必須的;如果各獨立特征不進行標準化,結果標準正態分布數據差距很大:比如使用均值為0、方差為1的高斯分布.
標準化的流程簡單來說可以表達為:將數據按其屬性(按列進行)減去其均值,然后除以其方差。最后得到的結果是,對每個屬性/每列來說所有數據都聚集在0附近,方差值為1
# 創建自定義縮放器類(標準化) class CustomScaler(BaseEstimator, TransformerMixin):# 聲明一些基本內容和信息def __init__(self, columns, copy=True, with_mean=True, with_std=True):# scaler是Standard Scaler對象self.scaler = StandardScaler(copy, with_mean, with_std)self.columns = columnsself.mean_ = Noneself.var_ = None# 基于StandardScale的擬合方法def fit(self, X, y=None):self.scaler.fit(X[self.columns], y)self.mean_ = np.mean(X[self.columns])self.var_ = np.var(X[self.columns])return self# 進行實際縮放的變換方法def transform(self, X, y=None, copy=None):# 記錄列的初始順序init_col_order = X.columns# 縮放創建類實例時選擇的所有功能X_scaled = pd.DataFrame(self.scaler.transform(X[self.columns]), columns=self.columns)# 聲明一個包含所有未縮放信息的變量X_not_scaled = X.loc[:, ~X.columns.isin(self.columns)]# 返回包含所有已縮放要素和所有未縮放要素的數據框return pd.concat([X_not_scaled, X_scaled], axis=1)[init_col_order]# 數據縮放要忽略的列 columns_to_omit = ['blueFirstBlood'] # 忽略一血,因為它是分類變量# 根據要縮放的列創建列表 columns_to_scale = [x for x in unscaled_inputs.columns.values if x not in columns_to_omit] blue_scaler = CustomScaler(columns_to_scale) blue_scaler.fit(unscaled_inputs) scaled_inputs = blue_scaler.transform(unscaled_inputs) pd.set_option('display.width', 80) # 設置Console每一行展示的最大寬度,屏幕一行顯示滿之后才會進行換行 print("標準化處理后的數據:", scaled_inputs) 標準化處理后的數據:blueFirstBlood blueKills blueDeaths blueTotalGold \ 0 1 0.935301 -0.046926 0.460179 1 0 -0.393216 -0.387796 -1.166792 2 0 0.271042 1.657424 -0.254307 3 0 -0.725346 -0.387796 -0.876959 4 0 -0.061087 -0.046926 -0.067382 ... ... ... ... 9874 1 0.271042 -0.728666 0.821656 9875 0 -0.061087 -0.728666 -0.172894 9876 0 -0.061087 0.293944 -0.391082 9877 1 -1.389604 -1.069536 -1.331573 9878 1 -0.061087 -0.046926 -0.154657 blueTotalExperience blueGoldDiff blueExperienceDiff \ 0 -0.740639 0.256228 0.013342 1 -1.385391 -1.191254 -0.593342 2 -1.422043 -0.483614 -0.520436 3 0.021567 -0.544350 0.013863 4 0.512211 -0.415133 0.137283 ... ... ... 9874 0.865408 1.020936 1.303263 9875 1.105315 0.312888 0.479942 9876 0.086541 -0.990702 -0.959957 9877 -0.582367 -0.347874 -0.547516 9878 -0.505730 0.371994 -0.012696 blueMinionsTotales redMinionsTotales WardsPlacedDiff \ 0 -1.419968 -0.651842 0.503853 1 -1.968987 0.912988 0.003069 2 -1.380753 -1.473378 0.003069 3 -0.439577 0.521780 1.081682 4 -0.008205 0.912988 2.237338 ... ... ... 9874 0.501598 -0.221514 -1.114066 9875 0.540814 -0.260635 1.620988 9876 -0.478793 2.047489 0.349766 9877 0.187873 0.717384 -2.000069 9878 -0.635655 -0.847446 0.349766 WardsDestroyedDiff AssistsDiff blueHeraldsDiff blueDragonsDiff \ 0 -1.436801 0.523196 -0.047412 0.058162 1 -0.035635 0.523196 -1.744448 -1.079624 2 -1.086510 -1.731206 -0.047412 1.195948 3 -0.385927 -0.864129 1.649624 0.058162 4 0.664947 -0.170466 -0.047412 -1.079624 ... ... ... ... 9874 -0.385927 -0.343882 -0.047412 1.195948 9875 -7.391756 0.870027 -0.047412 1.195948 9876 0.314656 -1.037544 -0.047412 -1.079624 9877 -0.035635 0.349780 -0.047412 1.195948 9878 -0.736218 0.176365 -0.047412 -1.079624 blueTowersDestroyedDiff EliteMonstersDiff 0 -0.025866 0.021707 1 -3.104510 -1.851163 2 -0.025866 0.958142 3 -0.025866 0.958142 4 -0.025866 -0.914728 ... ... 9874 -0.025866 0.958142 9875 -0.025866 0.958142 9876 -0.025866 -0.914728 9877 -0.025866 0.958142 9878 -0.025866 -0.914728FutureWarning警告不影響代碼運行,可忽略
數據切片:
#數據切片 x_train, x_test, y_train, y_test = train_test_split(scaled_inputs, target, train_size=0.8, random_state=2) print("訓練數據:",x_train.shape,y_train.shape,"測試數據:",x_test.shape,y_test.shape) 訓練數據: (7903, 16) (7903, 1) 測試數據 (1976, 16) (1976, 1)模型訓練分析:
#模型訓練 reg = LogisticRegression() reg.fit(x_train, y_train) #創建一個匯總表以可視化變量以及各自的系數和幾率 variables = unscaled_inputs.columns.values summary_table = pd.DataFrame(columns=['Variables'], data = variables) summary_table['Coef'] = np.transpose(reg.coef_) # add the intercept at index 0 summary_table.index = summary_table.index + 1 summary_table.loc[0] = ['Intercept', reg.intercept_[0]] # calculate the Odds Ratio and add to the table summary_table['Odds Ratio'] = np.exp(summary_table.Coef) summary_table.sort_values(by=['Odds Ratio'], ascending=False)可視化變量:
模型變量評價: Variables Coef Odds Ratio 6 blueGoldDiff 1.211278 3.357772 7 blueExperienceDiff 0.473859 1.606180 14 blueDragonsDiff 0.181283 1.198754 9 redMinionsTotales 0.156352 1.169237 16 EliteMonstersDiff 0.143389 1.154178 3 blueDeaths 0.071080 1.073667 4 blueTotalGold 0.064979 1.067136 1 blueFirstBlood 0.062815 1.064830 11 WardsDestroyedDiff 0.031517 1.032019 10 WardsPlacedDiff 0.002818 1.002822 5 blueTotalExperience -0.002409 0.997594 0 Intercept -0.029890 0.970552 13 blueHeraldsDiff -0.045495 0.955524 8 blueMinionsTotales -0.079767 0.923332 12 AssistsDiff -0.092587 0.911570 15 blueTowersDestroyedDiff -0.110353 0.895518 2 blueKills -0.114022 0.892239數據測試:
# 模型測試 print("訓練數據評分:", reg.score(x_train, y_train)) print("訓練數據評分:", reg.score(x_test, y_test)) #將測試結果寫入到原始數據集中 predicted_prob = reg.predict_proba(x_test) data['predicted'] = reg.predict_proba(scaled_inputs)[:, 1] print("經過預測后的包含預測結果的完整數據集:", data) #原始數據和勝率分析對比 col_n = ['blueWins','predicted'] a = pd.DataFrame(data,columns = col_n) print("原始數據和勝率分析對比:", a) 訓練數據評分: 0.7327597115019613 訓練數據評分: 0.7358299595141701可見兩個數據的模型評分都非常相似,說明模型擬合得還不錯
結果分析:
在LOL等MOBA(多人在線戰斗競技場)中,一局游戲的勝利受到非常多因素的影響,在這類團隊競技游戲中,游戲的勝利與否非常考驗玩家的操作,意識和相互之間的配合,由于游戲的參與者是人,因此會存在許多未知因素并會受到許多不確定因素的影響,如玩家的心情,狀態甚至是網絡情況等.
因此,對此類游戲的勝利預測不可能達到100%的準確度,可見以上模型的擬合結果還過得去
結論及規律分析:
由以上的分析可看出,一血,龍,小兵野怪等都對經濟有一定的貢獻率.在一局比賽的第10分鐘時,影響雙方勝率最大的因素是blueGoldDiff(經濟差),并且當雙方的經濟差增加一個標準化單位的時候,勝率增加235%,同時,雙方的經驗差blueExperienceDiff對數據也有很大影響,當雙方經驗差增加一個標準化單位的時候,勝率增加60.6%
在比賽中擊殺一條龍的時候,可以為你的勝率增加20%左右,同時擊殺EliteMonstersDiff(精英怪物)也可以為比賽帶來比較大的助力.但在分析結果中也出現了一些反常現象,在一局比賽中,如果對方擊殺的野怪和小兵數redMinionsTotales越高,我方擊殺的野怪和小兵數blueMinionsTotales越少,則我方的勝率越高,我猜測可能是由于勝率高的一方在前期更喜歡進行團戰取得經濟優勢的原因,在比賽中blueKills擊殺數反而與勝率呈反比,我猜測可能是由于這是一個推塔游戲,而擊殺數比較高的一些隊伍可能更關注于擊殺而忽略了推塔
因此,在游戲時要注意與對方拉開經濟差,經濟差越大,則勝率越高.
原始數據集及勝率預測結果:
經過預測后的包含預測結果的完整數據集: blueWins blueWardsPlaced blueWardsDestroyed blueFirstBlood \ 0 0 28 2 1 1 0 12 1 0 2 0 15 0 0 3 0 43 1 0 4 0 75 4 0 ... ... ... ... 9874 1 17 2 1 9875 1 54 0 0 9876 0 23 1 0 9877 0 14 4 1 9878 1 18 0 1 blueKills blueDeaths blueAssists blueEliteMonsters blueDragons \ 0 9 6 11 0 0 1 5 5 5 0 0 2 7 11 4 1 1 3 4 5 5 1 0 4 6 6 6 0 0 ... ... ... ... ... 9874 7 4 5 1 1 9875 6 4 8 1 1 9876 6 7 5 0 0 9877 2 3 3 1 1 9878 6 6 5 0 0 blueHeralds blueTowersDestroyed blueTotalGold blueAvgLevel \ 0 0 0 17210 6.6 1 0 0 14712 6.6 2 0 0 16113 6.4 3 1 0 15157 7.0 4 0 0 16400 7.0 ... ... ... ... 9874 0 0 17765 7.2 9875 0 0 16238 7.2 9876 0 0 15903 7.0 9877 0 0 14459 6.6 9878 0 0 16266 7.0 blueTotalExperience blueTotalMinionsKilled \ 0 17039 195 1 16265 174 2 16221 186 3 17954 201 4 18543 210 ... ... 9874 18967 211 9875 19255 233 9876 18032 210 9877 17229 224 9878 17321 207 blueTotalJungleMinionsKilled blueGoldDiff blueExperienceDiff \ 0 36 643 -8 1 43 -2908 -1173 2 46 -1172 -1033 3 55 -1321 -7 4 57 -1004 230 ... ... ... 9874 69 2519 2469 9875 48 782 888 9876 45 -2416 -1877 9877 48 -839 -1085 9878 44 927 -58 blueCSPerMin blueGoldPerMin redWardsPlaced redWardsDestroyed \ 0 19.5 1721.0 15 6 1 17.4 1471.2 12 1 2 18.6 1611.3 15 3 3 20.1 1515.7 15 2 4 21.0 1640.0 17 2 ... ... ... ... 9874 21.1 1776.5 46 3 9875 23.3 1623.8 12 21 9876 21.0 1590.3 14 0 9877 22.4 1445.9 66 4 9878 20.7 1626.6 9 2 redFirstBlood redKills redDeaths redAssists redEliteMonsters \ 0 0 6 9 8 0 1 1 5 5 2 2 2 1 11 7 14 0 3 1 5 4 10 0 4 1 6 6 7 1 ... ... ... ... ... 9874 0 4 7 7 0 9875 1 4 6 3 0 9876 1 7 6 11 1 9877 0 3 2 1 0 9878 0 6 6 4 1 redDragons redHeralds redTowersDestroyed redTotalGold redAvgLevel \ 0 0 0 0 16567 6.8 1 1 1 1 17620 6.8 2 0 0 0 17285 6.8 3 0 0 0 16478 7.0 4 1 0 0 17404 7.0 ... ... ... ... ... 9874 0 0 0 15246 6.8 9875 0 0 0 15456 7.0 9876 1 0 0 18319 7.4 9877 0 0 0 15298 7.2 9878 1 0 0 15339 6.8 redTotalExperience redTotalMinionsKilled redTotalJungleMinionsKilled \ 0 17047 197 55 1 17438 240 52 2 17254 203 28 3 17961 235 47 4 18313 225 67 ... ... ... 9874 16498 229 34 9875 18367 206 56 9876 19909 261 60 9877 18314 247 40 9878 17379 201 46 redGoldDiff redExperienceDiff redCSPerMin redGoldPerMin predicted 0 -643 8 19.7 1656.7 0.549459 1 2908 1173 24.0 1762.0 0.170271 2 1172 1033 20.3 1728.5 0.387228 3 1321 7 23.5 1647.8 0.393685 4 1004 -230 22.5 1740.4 0.356486 ... ... ... ... ... 9874 -2519 -2469 22.9 1524.6 0.892957 9875 -782 -888 20.6 1545.6 0.610317 9876 2416 1877 26.1 1831.9 0.178771 9877 839 1085 24.7 1529.8 0.433183 9878 -927 58 20.1 1533.9 0.511128原始數據和勝率分析對比:
blueWins predicted 0 0 0.549459 1 0 0.170271 2 0 0.387228 3 0 0.393685 4 0 0.356486... ... 9874 1 0.892957 9875 1 0.610317 9876 0 0.178771 9877 0 0.433183 9878 1 0.5111281762.0 0.170271
2 1172 1033 20.3 1728.5 0.387228
3 1321 7 23.5 1647.8 0.393685
4 1004 -230 22.5 1740.4 0.356486
… … … … …
9874 -2519 -2469 22.9 1524.6 0.892957
9875 -782 -888 20.6 1545.6 0.610317
9876 2416 1877 26.1 1831.9 0.178771
9877 839 1085 24.7 1529.8 0.433183
9878 -927 58 20.1 1533.9 0.511128
源碼:
import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn import metrics from sklearn.preprocessing import StandardScaler from sklearn.base import BaseEstimator, TransformerMixinplt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = Falsepd.set_option('display.max_columns', None) # 顯示所有列 pd.set_option('mode.chained_assignment', None) # 關閉警告 # pd.set_option('display.width', 100) #設置Console每一行展示的最大寬度,屏幕一行顯示滿之后才會進行換行data = pd.read_csv('high_diamond_ranked_10min.csv')print("前五行數據:", data.head()) print("數據形狀:", data.shape) pd.set_option('display.width', 10) # 設置Console每一行展示的最大寬度,屏幕一行顯示滿之后才會進行換行 print("數據列名:", data.columns) pd.set_option('display.width', 80) # 設置Console每一行展示的最大寬度,屏幕一行顯示滿之后才會進行換行 print("數據概覽:", data.describe()) print("數據概覽:", data.info())data.dropna(axis=0, how='any', inplace=True) # 刪除有空值的行 data = data.drop(['gameId'], axis=1) # 刪除gameIdplt.figure(figsize=(18, 15)) sns.heatmap(round(data.corr(), 1), cmap="coolwarm", annot=True, linewidths=.5) # 相關性(-1,1) plt.savefig('熱力圖相關性分析.jpg', bbox_inches='tight')# data.corr():計算列與列之間的相關系數,返回相關系數矩陣 # sns.heatmap():利用seaborn繪制個變量之間相關性的熱力圖# 定義一個函數 作用:找出相關系數矩陣中相關性大的一組數據,同時返回其中一列數據 def remove_redundancy(r):to_remove = []for i in range(len(r.columns)):for j in range(i):if (abs(r.iloc[i, j]) >= 1 and (r.columns[j] not in to_remove)):print("相關性:", r.iloc[i, j], r.columns[j], r.columns[i])to_remove.append(r.columns[i])return to_removeclean_data = data.drop(remove_redundancy(data.corr()), axis=1) # 刪去相關性較高項pd.set_option('display.width', 10) # 設置Console每一行展示的最大寬度,屏幕一行顯示滿之后才會進行換行 print("初步處理后的數據:", clean_data.columns)# 將擊殺野怪和小兵數合并: clean_data['blueMinionsTotales'] = clean_data['blueTotalMinionsKilled'] + clean_data['blueTotalJungleMinionsKilled'] clean_data['redMinionsTotales'] = clean_data['redTotalMinionsKilled'] + clean_data['redTotalJungleMinionsKilled'] clean_data = clean_data.drop(['blueTotalMinionsKilled'], axis=1) clean_data = clean_data.drop(['blueTotalJungleMinionsKilled'], axis=1) clean_data = clean_data.drop(['redTotalMinionsKilled'], axis=1) clean_data = clean_data.drop(['redTotalJungleMinionsKilled'], axis=1)# 等級和經驗分析: plt.figure(figsize=(12, 12)) plt.subplot(121) sns.scatterplot(x='blueAvgLevel', y='blueTotalExperience', hue='blueWins', data=clean_data) plt.title('blue') plt.xlabel('blueAvgLevel') plt.ylabel('blueTotalExperience') plt.grid(True) plt.subplot(122) sns.scatterplot(x='redAvgLevel', y='redTotalExperience', hue='blueWins', data=clean_data) plt.title('red') plt.xlabel('redAvgLevel') plt.ylabel('redTotalExperience') plt.grid(True) plt.savefig('等級和經驗分析.jpg', bbox_inches='tight')# 刪去等級列 clean_data = clean_data.drop(['blueAvgLevel'], axis=1) clean_data = clean_data.drop(['redAvgLevel'], axis=1)sns.set(font_scale=1.5) plt.figure(figsize=(20, 20)) sns.set_style("whitegrid")# 擊殺和被擊殺數繪制散點圖 plt.subplot(321) sns.scatterplot(x='blueKills', y='blueDeaths', hue='blueWins', data=clean_data) plt.title('blueKills&&blueDeaths') plt.xlabel('blueKills') plt.ylabel('blueDeaths') plt.grid(True)# 助攻數繪制散點圖 plt.subplot(322) sns.scatterplot(x='blueAssists', y='redAssists', hue='blueWins', data=clean_data) plt.title('Assists') plt.xlabel('blueAssists') plt.ylabel('redAssists') plt.tight_layout(pad=1.5) plt.grid(True)# 雙方金幣數繪制散點圖 plt.subplot(323) sns.scatterplot(x='blueTotalGold', y='redTotalGold', hue='blueWins', data=clean_data) plt.title('TotalGold') plt.xlabel('blueTotalGold') plt.ylabel('redTotalGold') plt.tight_layout(pad=1.5) plt.grid(True)# 雙方經驗繪制散點圖 plt.subplot(324) sns.scatterplot(x='blueTotalExperience', y='redTotalExperience', hue='blueWins', data=clean_data) plt.title('Experience') plt.xlabel('blueTotalExperience') plt.ylabel('redTotalExperience') plt.tight_layout(pad=1.5) plt.grid(True)# 雙方插眼數量繪制散點圖 plt.subplot(325) sns.scatterplot(x='blueWardsPlaced', y='redWardsPlaced', hue='blueWins', data=clean_data) plt.title('WardsPlaced') plt.xlabel('blueWardsPlaced') plt.ylabel('redWardsPlaced') plt.tight_layout(pad=1.5) plt.grid(True)# 擊殺的小兵和野怪總數繪制散點圖 plt.subplot(326) sns.scatterplot(x='blueMinionsTotales', y='redMinionsTotales', hue='blueWins', data=clean_data) plt.title('MinionsTotales') plt.xlabel('blueMinionsTotales') plt.ylabel('redMinionsTotales') plt.tight_layout(pad=1.5) plt.grid(True) plt.savefig('數據分析.jpg', bbox_inches='tight')# 將一些數據轉換為它們的差值: clean_data['WardsPlacedDiff'] = clean_data['blueWardsPlaced'] - clean_data['redWardsPlaced'] clean_data['WardsDestroyedDiff'] = clean_data['blueWardsDestroyed'] - clean_data['redWardsDestroyed'] clean_data['AssistsDiff'] = clean_data['blueAssists'] - clean_data['redAssists'] clean_data['blueHeraldsDiff'] = clean_data['blueHeralds'] - clean_data['redHeralds'] clean_data['blueDragonsDiff'] = clean_data['blueDragons'] - clean_data['redDragons'] clean_data['blueTowersDestroyedDiff'] = clean_data['blueTowersDestroyed'] - clean_data['redTowersDestroyed'] clean_data['EliteMonstersDiff'] = clean_data['blueEliteMonsters'] - clean_data['redEliteMonsters'] clean_data = clean_data.drop(['blueWardsPlaced'], axis=1) clean_data = clean_data.drop(['redWardsPlaced'], axis=1) clean_data = clean_data.drop(['blueWardsDestroyed'], axis=1) clean_data = clean_data.drop(['redWardsDestroyed'], axis=1) clean_data = clean_data.drop(['blueAssists'], axis=1) clean_data = clean_data.drop(['redAssists'], axis=1) clean_data = clean_data.drop(['blueHeralds'], axis=1) clean_data = clean_data.drop(['redHeralds'], axis=1) clean_data = clean_data.drop(['blueTowersDestroyed'], axis=1) clean_data = clean_data.drop(['redTowersDestroyed'], axis=1) clean_data = clean_data.drop(['blueDragons'], axis=1) clean_data = clean_data.drop(['redDragons'], axis=1) clean_data = clean_data.drop(['blueEliteMonsters'], axis=1) clean_data = clean_data.drop(['redEliteMonsters'], axis=1) clean_data = clean_data.drop(['redTotalGold'], axis=1) # 紅隊金幣數可由藍隊金幣數與差值一起得到,故刪去 clean_data = clean_data.drop(['redTotalExperience'], axis=1) # 紅隊經驗可由藍隊經驗與差值一起得到,故刪去# 一血,龍與精英怪物分析 sns.catplot(x="blueWins", y="blueGoldDiff", hue="blueFirstBlood", data=clean_data) plt.savefig('一血.jpg', bbox_inches='tight') sns.catplot(x="blueWins", y="blueGoldDiff", hue="blueDragonsDiff", data=clean_data) plt.savefig('龍.jpg', bbox_inches='tight') sns.catplot(x="blueWins", y="blueGoldDiff", hue="EliteMonstersDiff", data=clean_data) plt.savefig('精英怪物.jpg', bbox_inches='tight')print("最終數據:", clean_data.columns)# 數據標準化處理 標準化非分類數據 unscaled_inputs = clean_data.filter(['blueFirstBlood','blueKills','blueDeaths','blueTotalGold','blueTotalExperience','blueGoldDiff','blueExperienceDiff','blueMinionsTotales','redMinionsTotales','WardsPlacedDiff','WardsDestroyedDiff','AssistsDiff','blueHeraldsDiff','blueDragonsDiff','blueTowersDestroyedDiff','EliteMonstersDiff'], axis=1) target = clean_data.filter(['blueWins'])# 創建自定義縮放器類 class CustomScaler(BaseEstimator, TransformerMixin):# 聲明一些基本內容和信息def __init__(self, columns, copy=True, with_mean=True, with_std=True):# scaler是Standard Scaler對象self.scaler = StandardScaler(copy, with_mean, with_std)self.columns = columnsself.mean_ = Noneself.var_ = None# 基于StandardScale的擬合方法def fit(self, X, y=None):self.scaler.fit(X[self.columns], y)self.mean_ = np.mean(X[self.columns])self.var_ = np.var(X[self.columns])return self# 進行實際縮放的變換方法def transform(self, X, y=None, copy=None):# 記錄列的初始順序init_col_order = X.columns# 縮放創建類實例時選擇的所有功能X_scaled = pd.DataFrame(self.scaler.transform(X[self.columns]), columns=self.columns)# 聲明一個包含所有未縮放信息的變量X_not_scaled = X.loc[:, ~X.columns.isin(self.columns)]# 返回包含所有已縮放要素和所有未縮放要素的數據框return pd.concat([X_not_scaled, X_scaled], axis=1)[init_col_order]# 數據縮放要忽略的列 columns_to_omit = ['blueFirstBlood', 'blueDragonsDiff'] # 忽略一血,因為它是分類變量# 根據要縮放的列創建列表 columns_to_scale = [x for x in unscaled_inputs.columns.values if x not in columns_to_omit] blue_scaler = CustomScaler(columns_to_scale) blue_scaler.fit(unscaled_inputs) scaled_inputs = blue_scaler.transform(unscaled_inputs) pd.set_option('display.width', 80) # 設置Console每一行展示的最大寬度,屏幕一行顯示滿之后才會進行換行 print("標準化處理后的數據:", scaled_inputs)# 數據切片 x_train, x_test, y_train, y_test = train_test_split(scaled_inputs, target, train_size=0.8, random_state=2) print("訓練數據:", x_train.shape, y_train.shape, "測試數據:", x_test.shape, y_test.shape)# 模型訓練 reg = LogisticRegression() reg.fit(x_train, y_train) # 創建一個匯總表以可視化變量以及各自的系數和幾率 variables = unscaled_inputs.columns.values intercept = reg.intercept_ # 截距 summary_table = pd.DataFrame(columns=['Variables'], data=variables) summary_table['Coef'] = np.transpose(reg.coef_) summary_table.index = summary_table.index + 1 summary_table.loc[0] = ['Intercept', reg.intercept_[0]] summary_table['Odds Ratio'] = np.exp(summary_table.Coef) summary_table.sort_values(by=['Odds Ratio'], ascending=False) print("模型變量評價:", summary_table.sort_values(by=['Odds Ratio'], ascending=False))# 模型測試 print("訓練數據評分:", reg.score(x_train, y_train)) print("訓練數據評分:", reg.score(x_test, y_test)) # 將測試結果寫入到原始數據集中 predicted_prob = reg.predict_proba(x_test) data['predicted'] = reg.predict_proba(scaled_inputs)[:, 1] print("經過預測后的包含預測結果的完整數據集:", data)# 原始數據和勝率分析對比 col_n = ['blueWins', 'predicted'] a = pd.DataFrame(data, columns=col_n) print("原始數據和勝率分析對比:", a)數據下載地址:
鏈接:https://pan.baidu.com/s/1PDG8DruKsZWex8xoGROZ2Q
提取碼:h3qa
總結
以上是生活随笔為你收集整理的Python大数据分析LOL游戏胜率的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenMP与C++ 事半功倍地获得多线
- 下一篇: 央视推荐的护眼台灯是什么牌子?教育照明灯