# Step 6: Unsupervised clustering (if no labels) if labels is None: # Elbow method inertias = [] K_range = range(2, min(10, data_scaled.shape[0])) for k in K_range: km = KMeans(n_clusters=k, random_state=42) km.fit(data_scaled) inertias.append(km.inertia_) plt.figure() plt.plot(K_range, inertias, 'bo-') plt.xlabel('k') plt.ylabel('Inertia') plt.title('Elbow for k-means') plt.savefig('elbow.png') best_k = K_range[np.argmin(np.diff(inertias))] # simple heuristic km_final = KMeans(n_clusters=best_k, random_state=42) clusters = km_final.fit_predict(data_scaled) print(f"Optimal clusters: {best_k}") return pca_scores, clusters
scores, clusters = run_mva(X, labels=y) We tested the script on a synthetic 100×10 dataset. The PCA scree plot (Fig. 1) showed that 3 components capture 82% of the variance. The LDA projection (Fig. 2) separated the two synthetic classes almost perfectly due to the constructed differences in means. Clustering on unlabeled data suggested an optimal k of 3. mva script
# Step 4: Plot scree plt.figure(figsize=(8,4)) plt.bar(range(1, len(pca.explained_variance_ratio_)+1), pca.explained_variance_ratio_) plt.step(range(1, len(cum_var)+1), cum_var, where='mid', color='red') plt.title('Scree Plot with Cumulative Variance') plt.xlabel('Principal Component') plt.ylabel('Variance Ratio') plt.savefig('scree_plot.png') # Step 6: Unsupervised clustering (if no labels)