Machine Learning for DFS: Unsupervised Learning -- K-Means Clustering

Опубликовано: 15 Август 2026
на канале: Nick's Niche
1,327
44

#DFS #DailyFantasySports

Machine Learning for DFS: Unsupervised Learning -- K-Means Clustering Tutorial

Check me out on Patreon!
  / nicksniche  

*******
If anyone is having any issues with their python code, feel free to shoot me an email with your .py or .ipynb file and a quick summary of what's going on and what you've tried to solve the issue thus far. If you are going to send me an email, please comment here or on a relevant video and let me know, and put the subject line of the email in the comment so I can keep track of who is sending what and when. Emails can be sent to [email protected] . If there is not a comment with the subject line inside and short explanation on a video here, I will likely not read the email.
********

0:00 Introduction
1:51 Import Statements
2:40 Shameless Plug for Blog
3:12 Reviewing Make Blob Parameters
8:00 Initializing KMeans Analysis
9:04 Discussing Variance
14:18 Elbow Method
16:48 KMeans Prediction Results
17:55 Cross-Checking Results
22:30 Outro -- Contact Methods

Hey everyone welcome back! Today we are going to be getting a crash course in K-Means Clustering, an Unsupervised Learning algorithm. In the bigger picture, this method will be used to identify archetypes/playstyles and group players by them based on NBA Advanced statistics. However, today we are going to be using a sample dataset just to go over the basics.

What is K-Means Clustering?
K-Means clustering is a grouping algorithm that seeks to minimize the sum of variance measurements across each cluster. For those not familiar with how to calculate the variance, the variance for each datapoint is as follows:
with
x = data point
m = mean value for entire cluster

variance = |x - m|^2
or in english, the absolute value of the datapoint minus the mean of the dataset, squared.

Then the variance for each data point in the cluster is summed to obtain a variance value for the cluster as a whole. This summed value is what the K-Means algorithm seeks to minimize for all clusters. This algorithm will continue to alter and reshape each cluster until the summed variance is minimized for ALL clusters involved.

On to the code.

First thing's first, we have our import statements:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_blobs
from sklearn.cluster import KMeans

I don't believe we have used anything from matplotlib or sklearn yet, so if you do not have those installed on your system, you can use the following pip install statements to install them:
pip install matplotlib
pip install sklearn

Next we will be generating our dummy dataset using the make blob function. This function will predefine our data into distinct clusters, using the parameters we define. This is how we will cross-check our clustering results to verify it is working correctly.

After this dataset is generated and we have reviewed the data we have to work with, we will begin the process of utilizing the KMeans algorithm. While in this specific instance, we already know how many distinct clusters are present in the data, normally we would not know this going in. So we are going to operate as if we do not know. This requires us to iterate over the KMeans function several times, and record the variance for each one for comparison. We will analyze how many clusters to create by utilizing what is called the Elbow method. We will go further into detail on these methods later on, but in short, the elbow method is basically just graphing the variance against the iteration number, and identifying the biggest slope change. This is going to be the cluster number that returns the greatest value of distinction between clusters. While it may seem at first that the more clusters the better, because they will be more specific/accurate, at some point you begin getting diminishing returns. This is evident on the elbow plot by the slope of the line remaining static, (Straight Line) between different iterations. You CAN use more clusters, but the level of information gained is essentially meaningless.

After we have decided how many clusters to use, we will then predict which cluster each point will fall into, and compare with the initial blob assignments to ensure a 1-1 relationship from blob to cluster.