This commit is contained in:
lda committed 2026-01-14 19:14:51 +07:00
1 parent 627e776534
commit 6b91d6efca
14 files changed
+155 -82

No files matched your search

+2 -1
View File
@@ -8,8 +8,9 @@
Redmond, Michael. 2002. Communities and Crime. UCI Machine Learning Repository. https://doi.org/10.24432/C53W3X.
fixed by copilot:
@misc{abalone_1,
author = {Nash, Warwick, Sellers, Tracy, Talbot, Simon, Cawthorn, Andrew, and Ford, Wes},
author = {Nash, Warwick and Sellers, Tracy and Talbot, Simon and Cawthorn, Andrew and Ford, Wes},
title = {{Abalone}},
year = {1994},
howpublished = {UCI Machine Learning Repository},
+109 -62
View File
@@ -5,10 +5,13 @@ import matplotlib.pyplot as plt
from sklearn.cluster import KMeans, MiniBatchKMeans
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import silhouette_score
from sklearn.metrics import (
silhouette_score,
davies_bouldin_score,
calinski_harabasz_score,
)
from pathlib import Path
```
@@ -75,30 +78,13 @@ for k in ks:
```python
silhouettes, inertias
f"{silhouettes = }, {inertias = }"
```
([0.4307184563069171,
0.3308881531841743,
0.34454881088271005,
0.3330973959780588,
0.37350567261284556,
0.39200932042425607,
0.3905976225423938,
0.38566484346143776,
0.3910649022379036],
[18066.009207127645,
13418.727667404704,
11157.455698492053,
9330.808564389003,
7660.230080453761,
6764.331897239782,
5939.9031003732325,
5315.323604570619,
4805.410264465898])
'silhouettes = [0.4307184563069171, 0.3308881531841743, 0.34454881088271005, 0.3330973959780588, 0.37350567261284556, 0.39200932042425607, 0.3905976225423938, 0.38566484346143776, 0.3910649022379036], inertias = [18066.00920712765, 13418.727667404704, 11157.455698492053, 9330.808564389003, 7660.230080453761, 6764.331897239782, 5939.903100373233, 5315.323604570618, 4805.410264465897]'
@@ -154,6 +140,23 @@ plt.show()
```python
# Abalone dataset stats
print(f"Abalone: n_samples={X.shape[0]}, n_features={X.shape[1]}")
print(f"Best k: {best_k}")
print(f"Cluster sizes: {np.bincount(best_labels)}")
print(f"Davies-Bouldin Index: {davies_bouldin_score(X, best_labels):.4f}")
print(f"Calinski-Harabasz Index: {calinski_harabasz_score(X, best_labels):.4f}")
```
Abalone: n_samples=4177, n_features=9
Best k: 2
Cluster sizes: [1940 2237]
Davies-Bouldin Index: 0.8836
Calinski-Harabasz Index: 4512.6320
## Covertype
@@ -190,26 +193,13 @@ for k in ks:
```python
silhouettes, inertias
f"{silhouettes = }, {inertias = }"
```
([-0.030689560868985374,
0.014267829228328097,
0.052170441001039715,
0.0763160133542805,
0.05712772597563029,
0.11281790580873496,
0.1241172489653034],
[1033855.8510931745,
983031.2462602488,
933199.3143504256,
914886.5211138967,
889334.3440412064,
868852.814198654,
848656.9204411206])
'silhouettes = [-0.030689560868985374, 0.014267829228328097, 0.052170441001039715, 0.0763160133542805, 0.05712772597563029, 0.11281790580873496, 0.1241172489653034], inertias = [1033855.8510931745, 983031.2462602488, 933199.3143504256, 914886.5211138967, 889334.3440412064, 868852.814198654, 848656.9204411206]'
@@ -224,7 +214,7 @@ plt.show()
![png](lw2_files/lw2_14_0.png)
![png](lw2_files/lw2_15_0.png)
@@ -239,7 +229,7 @@ plt.show()
![png](lw2_files/lw2_15_0.png)
![png](lw2_files/lw2_16_0.png)
@@ -249,6 +239,23 @@ best_k = ks[np.argmax(silhouettes)]
best_labels = labels_per_k[best_k]
```
```python
# Covertype dataset stats
print(f"Covertype: n_samples={X.shape[0]}, n_features={X.shape[1]}")
print(f"Best k: {best_k}")
print(f"Cluster sizes: {np.bincount(best_labels)}")
print(f"Davies-Bouldin Index: {davies_bouldin_score(X, best_labels):.4f}")
print(f"Calinski-Harabasz Index: {calinski_harabasz_score(X, best_labels):.4f}")
```
Covertype: n_samples=20000, n_features=54
Best k: 8
Cluster sizes: [1407 6092 3757 2258 1139 4226 88 1033]
Davies-Bouldin Index: 2.3425
Calinski-Harabasz Index: 714.6171
# community
@@ -286,10 +293,26 @@ plt.show()
![png](lw2_files/lw2_20_0.png)
![png](lw2_files/lw2_22_0.png)
```python
# Communities dataset stats + PCA 90% variance
print(f"Communities: n_samples={X.shape[0]}, n_features={X.shape[1]}")
n_90 = np.argmax(np.cumsum(pca.explained_variance_ratio_) >= 0.90) + 1
print(f"Components for 90% variance: {n_90}")
print(
f"First 2 components explain: {pca.explained_variance_ratio_[:2].sum() * 100:.1f}%"
)
```
Communities: n_samples=1994, n_features=122
Components for 90% variance: 30
First 2 components explain: 35.0%
## PCA to 2D (no clustering yet)
@@ -305,7 +328,7 @@ plt.show()
![png](lw2_files/lw2_22_0.png)
![png](lw2_files/lw2_25_0.png)
@@ -357,7 +380,7 @@ plt.show()
![png](lw2_files/lw2_28_0.png)
![png](lw2_files/lw2_31_0.png)
@@ -376,7 +399,7 @@ plt.show()
![png](lw2_files/lw2_30_0.png)
![png](lw2_files/lw2_33_0.png)
@@ -421,35 +444,59 @@ plt.show()
![png](lw2_files/lw2_35_0.png)
![png](lw2_files/lw2_38_0.png)
```python
sil_orig, sil_pca, sil_sub
f"{sil_orig = }, {sil_pca = }, {sil_sub = }"
```
([0.1447573886443752,
0.1513815804645033,
0.1622363759648628,
0.10473845344821518,
0.11854956392780605,
0.11689825990588008],
[0.3885783888959868,
0.43308922486937373,
0.3730220026311381,
0.38759443998496274,
0.35733354404743983,
0.36750626178240353],
[0.16609258945009867,
0.13111313797495522,
0.13898110095002836,
0.12515895253959083,
0.12109768667928446,
0.10798315908348614])
'sil_orig = [0.1447573886443752, 0.1513815804645033, 0.1622363759648628, 0.10473845344821518, 0.11854956392780605, 0.11689825990588008], sil_pca = [0.3885783888959868, 0.43308922486937373, 0.3730220026311381, 0.38759443998496274, 0.35733354404743983, 0.36750626178240353], sil_sub = [0.16609258945009867, 0.13111313797495522, 0.13898110095002836, 0.12515895253959083, 0.12109768667928446, 0.10798315908348614]'
```python
# Full metrics table for all k values - Communities
print("Communities & Crime - Full Metrics Table")
print("-" * 70)
print(f"{'k':<4} {'Orig Sil':<12} {'PCA Sil':<12} {'Rand Sil':<12}")
print("-" * 70)
for i, k in enumerate(ks):
print(f"{k:<4} {sil_orig[i]:<12.4f} {sil_pca[i]:<12.4f} {sil_sub[i]:<12.4f}")
```
Communities & Crime - Full Metrics Table
----------------------------------------------------------------------
k Orig Sil PCA Sil Rand Sil
----------------------------------------------------------------------
2 0.1448 0.3886 0.1661
3 0.1514 0.4331 0.1311
4 0.1622 0.3730 0.1390
5 0.1047 0.3876 0.1252
6 0.1185 0.3573 0.1211
7 0.1169 0.3675 0.1080
```python
# Best PCA clustering - additional metrics
best_pca_k = ks[np.argmax(sil_pca)]
best_pca_labels = labels_pca[best_pca_k]
print(f"Best PCA k: {best_pca_k}")
print(f"Cluster sizes (PCA): {np.bincount(best_pca_labels)}")
print(f"Davies-Bouldin (PCA): {davies_bouldin_score(Xp, best_pca_labels):.4f}")
print(f"Calinski-Harabasz (PCA): {calinski_harabasz_score(Xp, best_pca_labels):.4f}")
```
Best PCA k: 3
Cluster sizes (PCA): [786 253 955]
Davies-Bouldin (PCA): 0.7937
Calinski-Harabasz (PCA): 1704.2171
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+7 -7
View File
@@ -95,15 +95,15 @@ We tested k from 2 to 8, tracked inertia and silhouette scores, and recorded res
#### Silhouette Analysis
![Covtype Silhouette](lw2_files/lw2_15_0.png){latex-placement="H"}
![Covtype Silhouette](lw2_files/lw2_16_0.png){latex-placement="H"}
#### Elbow Plot
![Covtype Elbow](lw2_files/lw2_14_0.png){latex-placement="H"}
![Covtype Elbow](lw2_files/lw2_15_0.png){latex-placement="H"}
### Interpretation
The Covertype silhouette scores remained relatively low across k values, indicating weak separation in the original feature space. The elbow plot does not show a clear bend, further suggesting that classic k-means struggles with high dimensionality and feature noise in this dataset.
The Covertype silhouette scores remained relatively low across k values, indicating weak separation in the original feature space. The elbow plot does not show a clear bend, further suggesting that classic k-means struggles with high dimensionality and feature noise in this dataset. Additionally, the cluster size distribution is highly imbalanced (one cluster contains only 88 samples while another has 6,092), indicating potential outliers or that k-means is not finding natural groupings in this data.
### Dataset Statistics
@@ -166,13 +166,13 @@ Before clustering:
We applied PCA to analyze variance distribution across components and determine an appropriate dimensionality for visualization/clustering.
![PCA Variance Explained](lw2_files/lw2_20_0.png){latex-placement="H"}
![PCA Variance Explained](lw2_files/lw2_22_0.png){latex-placement="H"}
## 2.3 PCA Visualization (2D)
The data projected onto the first two principal components:
![PCA Projection](lw2_files/lw2_22_0.png){latex-placement="H"}
![PCA Projection](lw2_files/lw2_25_0.png){latex-placement="H"}
## 2.4 Clustering After PCA vs Original
@@ -183,13 +183,13 @@ We applied k-means clustering with varying k to:
The silhouette scores for both are shown below:
![Subspace Clustering Comparison](lw2_files/lw2_28_0.png){latex-placement="H"}
![Subspace Clustering Comparison](lw2_files/lw2_38_0.png){latex-placement="H"}
### Best PCA Clustering Visualization
For the PCA space, the best silhouette score occurred at _k = 3_. The clusters are visualized here:
![PCA Clusters (k=3)](lw2_files/lw2_30_0.png){latex-placement="H"}
![PCA Clusters (k=3)](lw2_files/lw2_33_0.png){latex-placement="H"}
## 2.5 Random Subspace Clustering
BIN
View File
Binary file not shown.
+25
View File
@@ -0,0 +1,25 @@
# Classification I
## K-nearest neighbor classification
The objective is to study the use of the method k-nn and
1. Select two datasets with labels.
2. Run k-nn on these two datasets. Calculate the classification error (by comparing the class
labels obtained with the prediction and the original labels of test data).
3. Vary the value of k, comment on the results
4. Try to normalize the input dataset, is the performance better?
5. Apply PCA and SVD on the dataset, then what is the performance of k-nn on the new
projected data? Justify the answer.
6. Propose an approach to improve the performance of k-nn with the use of k-cross validation.
7. Apply leave-one-out and calculate the error of classification.
## SVM classifier
Suppose that we use the SVM classifier from sklearn in Python.
- Select two datasets with labels.
- Analyze the dataset. How is the data distribution? linearly separable or non-linearly seperable?
- Set up the SVM parameters suitable to the selected datasets.
- What is the performance of SVMs?
- How can SVM handle multi-class datasets?