TVP

在 FIFA 20 将技能相似球员进行分组（1）：K-均值聚类

了解 K-均值聚类算法

K-均值聚类算法是聚类算法中的一种。

• 指定 K-聚类并初始化随机质心。
• 进行迭代，直到聚类分配停止更改。该方法将每个观测值精确地分配到 K 个聚类中的一个。
• 对于每个 K 聚类，计算聚类平均值。
• 继续查看观测值列表，并将观测值分配给平均值最接近的聚类。

K-均值聚类算法使用平方欧几里得距离计算相似度。

特征工程

``````df = df[['short_name','age', 'height_cm', 'weight_kg', 'overall', 'potential',
'value_eur', 'wage_eur', 'international_reputation', 'weak_foot',
'skill_moves', 'release_clause_eur', 'team_jersey_number',
'contract_valid_until', 'nation_jersey_number', 'pace', 'shooting',
'passing', 'dribbling', 'defending', 'physic', 'gk_diving',
'gk_handling', 'gk_kicking', 'gk_reflexes', 'gk_speed',
'gk_positioning', 'attacking_crossing', 'attacking_finishing',
'attacking_volleys', 'skill_dribbling', 'skill_curve',
'skill_fk_accuracy', 'skill_long_passing', 'skill_ball_control',
'movement_acceleration', 'movement_sprint_speed', 'movement_agility',
'movement_reactions', 'movement_balance', 'power_shot_power',
'power_jumping', 'power_stamina', 'power_strength', 'power_long_shots',
'mentality_aggression', 'mentality_interceptions',
'mentality_positioning', 'mentality_vision', 'mentality_penalties',
'mentality_composure', 'defending_marking', 'defending_standing_tackle',
'defending_sliding_tackle', 'goalkeeping_diving',
'goalkeeping_handling', 'goalkeeping_kicking',
'goalkeeping_positioning', 'goalkeeping_reflexes']]``````

``df = df[df.overall > 86] # extracting players with overall above 86``

``df = df.fillna(df.mean())``

• 我们希望将数据进行归一化，因为变量是在不同尺度上测量的。
``````from sklearn import preprocessing
x = df.values # numpy array
scaler = preprocessing.MinMaxScaler()
x_scaled = scaler.fit_transform(x)
X_norm = pd.DataFrame(x_scaled)``````

``````from sklearn.decomposition import PCA
pca = PCA(n_components = 2) # 2D PCA for the plot
reduced = pd.DataFrame(pca.fit_transform(X_norm))``````

执行 K-均值聚类

``````from sklearn.cluster import KMeans
# specify the number of clusters
kmeans = KMeans(n_clusters=5)
# fit the input data
kmeans = kmeans.fit(reduced)
# get the cluster labels
labels = kmeans.predict(reduced)
# centroid values
centroid = kmeans.cluster_centers_
# cluster values
clusters = kmeans.labels_.tolist()``````

``````reduced['cluster'] = clusters
reduced['name'] = names
reduced.columns = ['x', 'y', 'cluster', 'name']

K-均值聚类图的可视化

``````import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set(style="white")
ax = sns.lmplot(x="x", y="y", hue='cluster', data = reduced, legend=False,
fit_reg=False, size = 15, scatter_kws={"s": 250})
texts = []
for x, y, s in zip(reduced.x, reduced.y, reduced.name):
texts.append(plt.text(x, y, s))
ax.set(ylim=(-2, 2))
plt.tick_params(labelsize=15)
plt.xlabel("PC 1", fontsize = 20)
plt.ylabel("PC 2", fontsize = 20)
plt.show()``````

K-均值聚类

Jaemin Lee，专攻数据分析与数据科学，数据科学应届毕业生。

https://towardsdatascience.com/grouping-soccer-players-with-similar-skillsets-in-fifa-20-part-1-k-means-clustering-c4a845db78bc

• 发表于:
• 本文为 InfoQ 中文站特供稿件
• 首发地址https://www.infoq.cn/article/b5THqc54suBNOTITcB6o
• 如有侵权，请联系 cloudcommunity@tencent.com 删除。

2020-09-21

2020-09-21

2020-09-21

2020-09-21

2020-09-21

2020-09-21

2023-06-09

2023-06-09

2023-06-09

2020-09-18

2020-09-22

10元无门槛代金券