Application and evaluation of unsupervised clustering (k-means, PCA-based preprocessing, hierarchical Ward linkage) across datasets with fundamentally different structure, testing both the recovery of known classes and the discovery of latent patterns in unlabeled data.
This is the codebase for a graduate coursework project (UFRGS, PPGEE — ELE0318 Inteligência Computacional I, June 2026) evaluating k-means clustering against datasets of different natures: one with ground-truth labels available for validation, one purely exploratory. The central question is whether a single algorithm, given appropriate preprocessing, is equally useful for confirming structure that is already known and for surfacing structure that isn't. K-means was chosen for its simplicity and interpretability, but its sensitivity to feature collinearity and scale required deliberate preprocessing decisions (PCA, Yeo-Johnson transforms, Hungarian-algorithm label alignment) rather than naive application. This repository holds the code for two of the three datasets analyzed in the accompanying paper — wheat grain geometry and weekly retail sales — both of which run entirely on CPU with no real-time or embedded constraints. Status: completed, written up as a paper (Project_paper.pdf).
- PCA-preceded k-means recovered three wheat species from seven correlated geometric features with 98.41% test accuracy and an Adjusted Rand Index of 0.952, despite Pearson correlations above 0.8 between several input variables that would otherwise distort centroid-based distances.
- Skewed input distributions were corrected with a Yeo-Johnson power transform before standardization and PCA — addressing asymmetry in the raw geometric measurements rather than feeding them directly into a Euclidean-distance algorithm.
- Cluster-to-class correspondence was resolved with the Kuhn-Munkres (Hungarian) assignment algorithm over the contingency matrix, instead of naive label matching — the standard pitfall when scoring unsupervised cluster accuracy against known labels.
- PC5, despite explaining a near-zero share of total variance, was the second-largest driver of classification accuracy gains (carrying kernel width/length signal) — evidence that "variance explained" alone is an unreliable feature-selection heuristic for downstream clustering.
- k-means was run with 1,000 random restarts and lowest-inertia selection in every experiment, trading compute for reproducibility and reduced sensitivity to centroid initialization.
- The same pipeline was applied unsupervised, with no train/test split, to 811 retail products represented as 52-week normalized sales-trend vectors, recovering six qualitatively distinct seasonal demand patterns via elbow-curve model-order selection (k = 6 from a sweep of k = 1 to 50).
- A preliminary, unconstrained decision-tree classifier was run on the same wheat dataset as an instructive contrast: it reached 100% training accuracy but only ~90.5% test accuracy, a textbook overfitting signature versus the PCA+k-means pipeline's tighter train/test gap.
The wheat-grain dataset (UCI Seeds, 210 samples, 3 balanced classes, 7 geometric attributes) is used to test whether k-means can recover known classes once redundant, collinear features are compressed by PCA. The retail dataset (UCI Sales Transactions Weekly, 811 products × 52 weeks) is used to test the complementary exploratory case, where no ground truth exists and the algorithm must reveal trend archetypes on its own. Evaluation metrics combine the Adjusted Rand Index (chance-corrected agreement with known labels) and classification accuracy after Hungarian alignment, swept across an increasing number of principal components fed into the model. A companion analysis in the paper — household electrical power consumption with hierarchical Ward clustering and silhouette/Davies-Bouldin/Calinski-Harabasz model selection — was contributed by a co-author and is not part of this codebase, but is summarized below for completeness.
- Language: Python 3.12
- ML/CV Libraries: scikit-learn (KMeans, PCA, PowerTransformer, DecisionTreeClassifier), SciPy (Kuhn-Munkres assignment)
- Embedded/Deployment: Not applicable — offline batch analysis, CPU only
- Tools: pandas, NumPy, matplotlib, seaborn, Jupyter, openpyxl
- Hardware: Standard CPU; no GPU or embedded targets used
| Metric | Training | Test |
|---|---|---|
| Accuracy (%) | 95.24 | 98.41 |
| Adjusted Rand Index | 0.8633 | 0.9520 |
PCA compresses the original 7 correlated attributes into components that explain >95% of variance with just 3 PCs:
Component loadings show PC1 is a general size factor (area, perimeter, kernel dimensions all load positively), while later components isolate shape (compactness, asymmetry) — including PC5, which carries little variance but meaningful discriminative signal:
Accuracy and ARI both climb as more principal components are added, with the largest jump occurring at PC5 and no further gain beyond it:
Preliminary results: an unconstrained decision tree on the same train/test split reached 100% training accuracy but only 90.5% test accuracy (57/63 correct), with kernel_groove_length (importance 0.47) and kernel_width (0.33) as the dominant features and compactness contributing nothing — included as an overfitting baseline, not as the paper's chosen model.
No ground-truth labels exist for this dataset, so there is no accuracy table — the elbow curve below drove model-order selection, with diminishing returns past k ≈ 6–7:
The six resulting centroids correspond to qualitatively distinct yearly demand shapes — e.g. high-and-falling, low-and-flat, low-and-rising, and mid-range-with-a-spike — recovered with no supervision:
pip install -r requirements.txt
# Wheat grain pipeline
python -m Seeds.src.prepare_dataset
python -m Seeds.src.split_dataset
python -m Seeds.src.pca_components_analysis
python -m Seeds.src.k_means_pca
python -m Seeds.src.decision_tree
# Retail sales pipeline (exploratory analysis lives in the notebook)
python -m Sales-Transactions-Weekly.src.prepare_dataset
jupyter notebook Sales-Transactions-Weekly/src/dataset_analysis.ipynbScripts expect to be run from the repository root so that datasets/ paths resolve correctly.
Project_paper.pdf— "Aplicação e avaliação de métodos de agrupamento em conjuntos de dados de naturezas distintas" (Caetani & Nunes, UFRGS PPGEE, June 2026). Covers all three datasets — wheat grains, retail sales, and household power consumption — including PCA, k-means, k-NN-based cluster validation, and Ward hierarchical clustering with inconsistency-coefficient-based cut selection; this repository implements the wheat-grain and retail-sales portions.




