Saturday, February 14, 2026

[perl][ML][Machine Learning]K-means in Perl

#!/usr/bin/perl -w

use strict;

use warnings;

use PDL::LiteF;

use PDL::Stats;


# 1. Define 4 separate arrays (Features)

my $f1 = pdl(1, 2, 1, 10, 11, 12); # Feature 1

my $f2 = pdl(1, 1, 2, 10, 12, 11); # Feature 2

my $f3 = pdl(0, 1, 0, 15, 14, 15); # Feature 3

my $f4 = pdl(2, 2, 1, 20, 21, 22); # Feature 4


# 2. Combine them into a 4-column matrix

# cat() joins them, transpose() makes each row a "person/object"

my $data = cat($f1, $f2, $f3, $f4)->transpose;


# 3. Run K-means for 2 groups

my $k = 2;

my %result = $data->kmeans({k => $k, NCLUS => 2});


# 4. Display which group each of the 6 items belongs to

print "Data Matrix (Rows = Items, Cols = Features):\n$data\n";

print "Cluster Assignments:\n", $result{cluster}, "\n";

print "Final Centroids (The average of each group):\n", $result{centroid}, "\n";


No comments:

Post a Comment