Decomposition
best_score_excluding_atom(norm_reduction, combinations, atom)
For each sample, find the maximum norm_reduction value among all combinations that do NOT contain atom[i].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
norm_reduction
|
ndarray
|
(n_sample, n_comb) Score or reduction value for each sample–combination pair. |
required |
combinations
|
ndarray
|
(n_comb, n_atom_select) Atom indices used in each combination. |
required |
atom
|
ndarray
|
(n_sample,) Atom index to exclude for each sample. |
required |
Returns:
Name | Type | Description |
---|---|---|
best_score_excl |
ndarray
|
(n_sample,) Max norm_reduction for each sample excluding combinations that contain atom[i]. |
Source code in isca_tools/utils/decomposition.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
pca_on_xarray(data, n_modes=4, standardize=True, valid=None, feature_dim_name='lev', reference_mean=True)
Perform PCA (via SVD) on xarray dataset. The PCA is fit only on samples where valid
is True. The
components found are then fit to all samples in data
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataArray with dims (..., feature_dim_name) (e.g. (co2, lat, lon, lev) with |
required | |
n_modes
|
int
|
Number of PCA modes to keep. |
4
|
standardize
|
bool
|
If True, divide each feature by its std (computed from valid samples)
before SVD so that features with different variances are equalized.
If False, SVD is performed on raw deviations from |
True
|
valid
|
Optional[DataArray]
|
Boolean mask with the same non-feature dims as |
None
|
feature_dim_name
|
str
|
Name of the dimension containing features of interest in |
'lev'
|
reference_mean
|
Union[bool, DataArray]
|
1-D DataArray (dim |
True
|
Returns:
Name | Type | Description |
---|---|---|
components |
DataArray
|
EOFs (modes) with dims (mode, feature_dim_name). |
scores |
DataArray
|
PC coefficients with same dims as |
mean_profile |
DataArray
|
The reference_mean actually used (dim |
std_profile |
DataArray
|
Std used for scaling (dim |
Notes
- This function uses np.linalg.svd directly so there is NO automatic re-centering:
the
reference_mean
you supply (or zero) is the baseline from which deviations are computed. - If standardize=True, std_profile is computed from the valid set and used both for the SVD input and for projecting all profiles.
Source code in isca_tools/utils/decomposition.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
scaled_k_means(x, initial_cluster_mean, valid=None, n_atom_select=1, norm_thresh=0, score_thresh=0.5, score_diff_thresh=0.1, score_diff_thresh_test_converge=0.05, score_thresh_multi_atom=0.05, min_cluster_size=10, n_iter=100, remove_perm=None, atom_ind_no_update=None, use_norm=False)
Perform scaled k-means clustering with optional multi-atom combinations.
This algorithm generalizes k-means by allowing each data point to be represented as a scaled combination of a small subset of cluster "atoms" (mean vectors), optionally including a zero vector (to allow sparse fits). At each iteration, coefficients for all possible atom combinations are computed to minimize residual norm, and cluster means are updated as the dominant direction of assigned samples’ residuals.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray
|
Input data of shape (n_sample, n_feature). |
required |
initial_cluster_mean
|
ndarray
|
Initial cluster centroids of shape (n_cluster, n_feature). |
required |
valid
|
Optional[ndarray]
|
Boolean mask (n_sample,) specifying valid samples for updates. |
None
|
n_atom_select
|
int
|
Number of atoms combined to represent each sample. Defaults to 1. |
1
|
norm_thresh
|
float
|
Threshold for treating samples as small-norm (ignored in fitting). Defaults to 0. |
0
|
score_thresh
|
float
|
Minimum improvement (norm reduction) for a sample to influence cluster update. Defaults to 0.5. |
0.5
|
score_diff_thresh
|
float
|
Minimum difference in score between best and next-best atom to be considered distinct. Defaults to 0.1. |
0.1
|
score_diff_thresh_test_converge
|
float
|
Tolerance for convergence test (difference between old and new best scores). Defaults to 0.05. |
0.05
|
score_thresh_multi_atom
|
float
|
Threshold for assigning multi-atom fits when residual difference is small. Defaults to 0.05. |
0.05
|
min_cluster_size
|
int
|
Minimum number of samples required to update a cluster. Defaults to 10. |
10
|
n_iter
|
int
|
Maximum number of iterations. Defaults to 100. |
100
|
remove_perm
|
Optional[ndarray]
|
List of atom combinations (indices) to exclude. Defaults to None. |
None
|
atom_ind_no_update
|
Optional[ndarray]
|
Atom indices that should not be updated. Defaults to None. |
None
|
use_norm
|
bool
|
Whether to normalize each residual before updating atoms. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
norm_cluster_mean |
ndarray
|
Updated normalized cluster mean vectors (atoms). |
cluster_eig_val |
ndarray
|
Leading eigenvalues for each cluster. |
cluster_ind |
ndarray
|
Cluster/combination index assigned to each sample. |
top_score |
ndarray
|
Norm reduction score of the assigned combination for each sample. |
coef_best |
ndarray
|
Coefficients of best-fitting atom combination per sample. |
atom_perm |
ndarray
|
Array of atom index combinations considered. |
Notes
- The algorithm can handle multi-atom fits by enumerating all valid atom combinations.
- A zero vector is appended as an additional atom to allow sparse representations.
- Clusters with fewer than
min_cluster_size
assigned samples are deactivated.
Source code in isca_tools/utils/decomposition.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
|