To perform k-means clustering with MySQL query, we can follow these steps:
- Choose the number of clusters k and initialize k centroids. In this example, let's choose k=3 and initialize the centroids randomly.
CREATE TEMPORARY TABLE centroids (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
centroid_1 FLOAT,
centroid_2 FLOAT,
centroid_3 FLOAT
);
INSERT INTO centroids (centroid_1, centroid_2, centroid_3)
SELECT 1.0 + RAND(), 1.0 + RAND(), 1.0 + RAND() UNION ALL
SELECT 2.0 + RAND(), 2.0 + RAND(), 2.0 + RAND() UNION ALL
SELECT 3.0 + RAND(), 3.0 + RAND(), 3.0 + RAND();
In this example, we create a temporary table called "centroids" with an ID column and three centroid columns (centroid_1, centroid_2, and centroid_3). We populate the table with three random centroids for each column.
- Calculate the distance between each point and each centroid using the Euclidean distance formula.
SELECT MaSV, TenSV, MaDA, TenDA, ChuyenMon, TuKhoa, HeDT, GVHD,
POW(centroid_1 - tf_idf_1, 2) + POW(centroid_2 - tf_idf_2, 2) + POW(centroid_3 - tf_idf_3, 2) AS distance
FROM (
SELECT MaSV, TenSV, MaDA, TenDA, ChuyenMon, TuKhoa, HeDT, GVHD,
MAX(CASE WHEN TuKhoa = 'term_1' THEN tf_idf ELSE 0 END) AS tf_idf_1,
MAX(CASE WHEN TuKhoa = 'term_2' THEN tf_idf ELSE 0 END) AS tf_idf_2,
MAX(CASE WHEN TuKhoa = 'term_3' THEN tf_idf ELSE 0 END) AS tf_idf_3
FROM (
SELECT MaSV, TenSV, MaDA, TenDA, ChuyenMon, TuKhoa, HeDT, GVHD,
tf * idf AS tf_idf
FROM (
-- TF-IDF calculation query from previous example
) AS tf_idf_tbl
) AS tf_idf_pivot_tbl
GROUP BY MaSV, MaDA
) AS distance_tbl
CROSS JOIN centroids;
In this query, we join the table of TF-IDF scores with the table of centroids, and calculate the Euclidean distance between each point and each centroid. Replace "term_1", "term_2", and "term_3" with the actual terms that you want to use for clustering.
- Assign each point to the closest centroid.
CREATE TEMPORARY TABLE assignments (
MaSV INT NOT NULL PRIMARY KEY,
cluster_id INT NOT NULL
);
INSERT INTO assignments (MaSV, cluster_id)
SELECT MaSV,
CASE
WHEN distance_1 <= distance_2 AND distance_1 <= distance_3 THEN 1
WHEN distance_2 <= distance_1 AND distance_2 <= distance_3 THEN 2
ELSE 3
END AS cluster_id
FROM (
-- Distance calculation query from previous step
) as distance_tbl;
In this step, we create a temporary table called "assignments" with columns for the student ID and the assigned cluster ID. We calculate the distance between each point and each centroid, and assign each point to the closest centroid.
- Update the centroids based on the new cluster assignments.
UPDATE centroids AS c, (
SELECT cluster_id, AVG(tf_idf_1) AS centroid_1, AVG(tf_idf_2) AS centroid_2, AVG(tf_idf_3) AS centroid_3
FROM (
SELECT a.cluster_id, tf_idf_1, tf_idf_2, tf_idf_3
FROM assignments AS a
JOIN (
SELECT MaSV,
MAX(CASE WHEN TuKhoa = 'term_1' THEN tf_idf ELSE 0 END) AS tf_idf_1,
MAX(CASE WHEN TuKhoa = 'term_2' THEN tf_idf ELSE 0 END) AS tf_idf_2,
MAX(CASE WHEN TuKhoa = 'term_3' THEN tf_idf ELSE 0 END) AS tf_idf_3
FROM (
SELECT MaSV, TenSV, MaDA, TenDA, ChuyenMon, TuKhoa, HeDT, GVHD,
tf * idf AS tf_idf
FROM (
-- TF-IDF calculation query from previous example
) AS tf_idf_tbl
) AS tf_idf_pivot_tbl
GROUP BY MaSV, MaDA
) AS tf_idf_pivot_tbl ON a.MaSV = tf_idf_pivot_tbl.MaSV
) AS cluster_tfidf_tbl
GROUP BY cluster_id
) AS new_centroids
SET c.centroid_1 = new_centroids.centroid_1, c.centroid_2 = new_centroids.centroid_2, c.centroid_3 = new_centroids.centroid_3
WHERE c.id = new_centroids.cluster_id;
In this final step, we update the centroids by calculating the mean of the TF-IDF scores for each cluster. We join the assignments table with the TF-IDF scores table, group by the cluster ID, and calculate the average for each term. Then, we update the centroid table with the new values.
Repeat steps 2 to 4 until the centroids converge to stable values (i.e., the cluster assignments stop changing or reach a predetermined number of iterations). Finally, you can examine the assignments table to see which data points belong to which clusters.