Master K-Means Clustering in Python: A Step‑by‑Step Guide with Real Basketball Data

This tutorial walks you through installing Anaconda, introduces essential Python data‑science packages, explains clustering versus classification, details the K‑means algorithm, and demonstrates a complete K‑means analysis on a basketball player dataset using Python code and visualizations.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master K-Means Clustering in Python: A Step‑by‑Step Guide with Real Basketball Data

1. Anaconda Installation and Usage

We use Anaconda, which bundles many Python packages for data mining and analysis. Install Anaconda on the default C:\ drive (avoid Chinese paths), then finish the setup and launch Spyder for writing Python code.

After installation, open the Anaconda folder, locate the executable files, and start Spyder. The left pane is for code editing, while the Console on the right shows output.

Although Anaconda includes many packages, some third‑party packages may still need to be installed via pip install <package> (e.g., pip install selenium, pip install lda).

2. Common Machine Learning Packages

The following four packages are frequently used for Python data mining and big‑data analysis:

Scikit‑Learn : Provides tools for classification, regression, clustering, dimensionality reduction, model selection, and preprocessing.

NumPy : Offers advanced numerical programming tools, including matrix types and vector operations.

SciPy : Supplies modules for statistics, optimization, integration, linear algebra, Fourier transforms, signal and image processing, and ODE solving.

Matplotlib : A plotting library similar to MATLAB/R, useful for creating interactive visualizations and embedding plots in GUI applications.

3. Clustering and K‑means Introduction

Clustering groups similar data points without predefined labels (unsupervised learning), while classification assigns labels based on a trained model (supervised learning). An example of classification is Gmail’s spam filter, which learns from user‑marked emails.

K‑means is a popular clustering algorithm known for its simplicity and speed. The basic steps are:

Select the desired number of clusters k.

Randomly choose k initial centroids.

Assign each data point to the nearest centroid.

Recompute centroids as the mean of assigned points.

Repeat steps 3–4 until centroids stabilize (convergence).

Illustrative diagrams show how points are reassigned and centroids move until the clustering result no longer changes.

4. Case Study: K‑means Clustering of Athlete Data

The dataset contains basketball players' statistics: assists per minute, height, playing time, age, and points per minute (96 rows, 5 features). The goal is to infer player positions (e.g., shooting guard, center, point guard) based on these metrics.

For the demo we use only two features—assists and points per minute—forming a 20 × 2 matrix. We set n_clusters=3 so the algorithm outputs labels 0, 1, 2 for the 20 players.

The core clustering code (using sklearn.cluster.KMeans) and the plotting code (using matplotlib.pyplot) are shown in the following images:

The resulting plot shows three clusters: a red cluster with high assists and points (akin to a “Jordan‑type” player), a middle cluster, and a lower‑right cluster with high assists but low points (likely point guards). More data would improve clustering quality.

By adjusting markers (e.g., marker='o'), the plot highlights the red points as top performers, confirming the clustering aligns with intuitive player roles.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

machine learningPythonK-MeansBasketball
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.