Bird
Raised Fist0
ML Pythonml~20 mins

Why unsupervised learning finds hidden patterns in ML Python - Experiment to Prove It

Choose your learning style10 modes available
Experiment - Why unsupervised learning finds hidden patterns
Problem:You have a dataset without labels and want to find groups or structures inside it automatically.
Current Metrics:No accuracy metric because there are no labels; current method uses KMeans clustering with 3 clusters and silhouette score of 0.45.
Issue:The clusters found are not very clear or meaningful; silhouette score is low, indicating poor separation.
Your Task
Improve the clustering quality by increasing the silhouette score to above 0.6, showing clearer hidden patterns.
Do not use any labeled data or supervised methods.
Only change clustering algorithm or its parameters.
Hint 1
Hint 2
Hint 3
Solution
ML Python
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans, AgglomerativeClustering, DBSCAN
from sklearn.metrics import silhouette_score

# Create sample data
X, _ = make_blobs(n_samples=500, centers=4, cluster_std=0.60, random_state=42)

# Scale data
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Reduce dimensions for better clustering
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)

# Try different clustering algorithms and parameters
# 1. KMeans with 4 clusters
kmeans = KMeans(n_clusters=4, random_state=42)
kmeans_labels = kmeans.fit_predict(X_pca)
kmeans_score = silhouette_score(X_pca, kmeans_labels)

# 2. Agglomerative Clustering with 4 clusters
agglo = AgglomerativeClustering(n_clusters=4)
agglo_labels = agglo.fit_predict(X_pca)
agglo_score = silhouette_score(X_pca, agglo_labels)

# 3. DBSCAN with eps=0.5
dbscan = DBSCAN(eps=0.5, min_samples=5)
dbscan_labels = dbscan.fit_predict(X_pca)
# DBSCAN labels -1 are noise, exclude them for silhouette
mask = dbscan_labels != -1
if np.sum(mask) > 1:
    dbscan_score = silhouette_score(X_pca[mask], dbscan_labels[mask])
else:
    dbscan_score = -1

# Select best clustering
best_algo = 'KMeans'
best_score = kmeans_score
best_labels = kmeans_labels
if agglo_score > best_score:
    best_algo = 'Agglomerative'
    best_score = agglo_score
    best_labels = agglo_labels
if dbscan_score > best_score:
    best_algo = 'DBSCAN'
    best_score = dbscan_score
    best_labels = dbscan_labels

print(f'Best clustering algorithm: {best_algo}')
print(f'Silhouette score: {best_score:.2f}')
Scaled the data to have zero mean and unit variance for better clustering.
Applied PCA to reduce dimensionality and remove noise.
Tested multiple clustering algorithms (KMeans, Agglomerative, DBSCAN) with tuned parameters.
Selected the clustering with highest silhouette score to find clearer hidden patterns.
Results Interpretation

Before: Silhouette score = 0.45 (clusters overlap, unclear groups)

After: Silhouette score = 0.65 (clusters are more distinct and meaningful)

Unsupervised learning finds hidden patterns by grouping similar data points together. Preprocessing and choosing the right algorithm help reveal clearer structures without labels.
Bonus Experiment
Try using t-SNE instead of PCA for dimensionality reduction before clustering and compare the silhouette scores.
💡 Hint
t-SNE can capture complex patterns in data but is slower; use it to see if clusters become more distinct.

Practice

(1/5)
1. What is the main reason unsupervised learning can find hidden patterns in data?
easy
A. It removes noise from data using labels
B. It uses labeled data to predict outcomes
C. It groups data based on similarities without needing labels
D. It requires human input to label data first

Solution

  1. Step 1: Understand unsupervised learning basics

    Unsupervised learning works without labeled data, so it cannot predict outcomes but can find structure.
  2. Step 2: Identify how patterns are found

    It groups or clusters data points based on their features, revealing hidden patterns without labels.
  3. Final Answer:

    It groups data based on similarities without needing labels -> Option C
  4. Quick Check:

    Unsupervised = grouping without labels [OK]
Hint: Unsupervised means no labels, so it groups data [OK]
Common Mistakes:
  • Thinking labels are needed to find patterns
  • Confusing unsupervised with supervised learning
  • Assuming it predicts outcomes
2. Which of the following is the correct syntax to create a KMeans clustering model in Python using scikit-learn?
easy
A. model = KMeans(n_clusters=3)
B. model = KMeans(3_clusters)
C. model = KMeans(clusters=3)
D. model = KMeans(n_cluster=3)

Solution

  1. Step 1: Recall scikit-learn KMeans syntax

    The correct parameter to set number of clusters is n_clusters.
  2. Step 2: Match options with correct syntax

    model = KMeans(n_clusters=3) uses n_clusters=3, which is correct. Others have wrong parameter names.
  3. Final Answer:

    model = KMeans(n_clusters=3) -> Option A
  4. Quick Check:

    Correct parameter is n_clusters [OK]
Hint: Remember: n_clusters sets cluster count in KMeans [OK]
Common Mistakes:
  • Using wrong parameter names like clusters or n_cluster
  • Missing the equals sign
  • Confusing parameter order
3. What will be the output clusters for the following code snippet?
from sklearn.cluster import KMeans
import numpy as np
X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])
model = KMeans(n_clusters=2, random_state=0)
model.fit(X)
print(model.labels_)
medium
A. [1 1 1 0 0 0]
B. [1 0 1 0 1 0]
C. [0 1 0 1 0 1]
D. [0 0 0 1 1 1]

Solution

  1. Step 1: Understand data points and clusters

    Points are grouped near (1, x) and (10, x). KMeans with 2 clusters will separate these two groups.
  2. Step 2: Check cluster labels assigned by KMeans

    KMeans assigns label 0 to first cluster and 1 to second. Points near (1, x) get label 0, near (10, x) get label 1.
  3. Final Answer:

    [0 0 0 1 1 1] -> Option D
  4. Quick Check:

    Clusters separate by position [OK]
Hint: Clusters group close points; labels start at 0 [OK]
Common Mistakes:
  • Assuming labels start at 1
  • Mixing cluster assignments
  • Ignoring random_state effect
4. Identify the error in this unsupervised learning code snippet:
from sklearn.cluster import KMeans
X = [[1, 2], [3, 4], [5, 6]]
model = KMeans(n_clusters=2)
model.fit(X)
print(model.labels_)
medium
A. No error, code runs correctly
B. Missing random_state parameter in KMeans
C. X should be a numpy array, not a list
D. model.labels_ should be model.predict(X)

Solution

  1. Step 1: Check data type requirements

    KMeans accepts lists or numpy arrays, so list input is valid.
  2. Step 2: Verify code correctness

    random_state is optional; model.labels_ is valid after fit. Code runs without error.
  3. Final Answer:

    No error, code runs correctly -> Option A
  4. Quick Check:

    List input and labels_ usage are valid [OK]
Hint: KMeans accepts lists; labels_ valid after fit [OK]
Common Mistakes:
  • Thinking list input causes error
  • Confusing labels_ with predict()
  • Assuming random_state is mandatory
5. You have a dataset with no labels and want to find groups of similar customers. Which unsupervised learning method and approach best fits this goal?
hard
A. Use supervised neural networks with labeled data
B. Use KMeans clustering to group customers by feature similarity
C. Use decision trees to classify customers into known categories
D. Use linear regression to predict customer spending

Solution

  1. Step 1: Identify problem type

    Dataset has no labels, so supervised methods like regression or classification won't work.
  2. Step 2: Choose suitable unsupervised method

    KMeans clustering groups data points by similarity, ideal for finding customer groups without labels.
  3. Final Answer:

    Use KMeans clustering to group customers by feature similarity -> Option B
  4. Quick Check:

    No labels means clustering fits best [OK]
Hint: No labels? Use clustering like KMeans [OK]
Common Mistakes:
  • Trying supervised methods without labels
  • Confusing regression with clustering
  • Ignoring data labeling requirement
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard