1
2
3
4
5
6
7
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
129
130
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
160
161
162
163
164
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
// 图像降维高级(v77.0.0)
// PCA、LDA、t-SNE、UMAP、ISOMAP、LLE、自动编码器、流形学习
///|
/// 图像降维任务类型
pub(all) enum DimensionalityReductionTaskType {
PCA
LDA
TSNE
UMAP
ISOMAP
LLE
Autoencoder
ManifoldLearning
FeatureSelection
Unknown
} derive(Eq)
///|
/// 降维算法类型
pub(all) enum DimensionalityReductionAlgorithmType {
PCA
KernelPCA
IncrementalPCA
SparsePCA
TruncatedSVD
LDA
TSNE
UMAP
ISOMAP
LLE
HessianLLE
ModifiedLLE
LTSA
MDS
SpectralEmbedding
Autoencoder
Unknown
} derive(Eq)
///|
/// 核函数类型
pub(all) enum KernelType {
Linear
Polynomial
RBF
Sigmoid
Cosine
Precomputed
Unknown
} derive(Eq)
///|
/// 距离度量类型
pub(all) enum DistanceMetricTypeDR {
Euclidean
Manhattan
Cosine
Chebyshev
Minkowski
Correlation
Unknown
} derive(Eq)
///|
/// 降维结果
pub(all) struct DimensionalityReductionResult {
reduced_data : Array[Array[Double]]
original_dimension : Int
reduced_dimension : Int
num_samples : Int
explained_variance_ratio : Array[Double]
cumulative_explained_variance : Double
reconstruction_error : Double
stress : Double
trustworthiness : Double
continuity : Double
reduction_time_ms : Double
components : Array[Array[Double]]
singular_values : Array[Double]
mean : Array[Double]
}
///|
/// PCA结果
pub(all) struct PCAResult {
reduced_data : Array[Array[Double]]
num_components : Int
original_dimension : Int
num_samples : Int
explained_variance : Array[Double]
explained_variance_ratio : Array[Double]
cumulative_explained_variance : Double
components : Array[Array[Double]]
singular_values : Array[Double]
mean : Array[Double]
noise_variance : Double
reconstruction_error : Double
reduction_time_ms : Double
}
///|
/// t-SNE结果
pub(all) struct TSNEResult {
reduced_data : Array[Array[Double]]
num_components : Int
num_samples : Int
perplexity : Double
learning_rate : Double
num_iterations : Int
kl_divergence : Double
final_error : Double
reduction_time_ms : Double
early_exaggeration : Double
exaggeration_iterations : Int
}
///|
/// UMAP结果
pub(all) struct UMAPResult {
reduced_data : Array[Array[Double]]
num_components : Int
num_samples : Int
n_neighbors : Int
min_dist : Double
metric : DistanceMetricTypeDR
num_epochs : Int
reconstruction_error : Double
trustworthiness : Double
continuity : Double
reduction_time_ms : Double
}
///|
/// ISOMAP结果
pub(all) struct ISOMAPResult {
reduced_data : Array[Array[Double]]
num_components : Int
num_samples : Int
n_neighbors : Int
geodesic_distances : Array[Array[Double]]
reconstruction_error : Double
residual_variance : Double
reduction_time_ms : Double
}
///|
/// LLE结果
pub(all) struct LLEResult {
reduced_data : Array[Array[Double]]
num_components : Int
num_samples : Int
n_neighbors : Int
reconstruction_error : Double
regularization : Double
reduction_time_ms : Double
}
///|
/// 特征选择结果
pub(all) struct FeatureSelectionResult {
selected_features : Array[Int]
num_selected : Int
original_num_features : Int
feature_scores : Array[Double]
feature_ranking : Array[Int]
selection_method : String
threshold : Double
selection_time_ms : Double
}
///|
/// 图像降维配置
pub(all) struct DimensionalityReductionConfig {
task_type : DimensionalityReductionTaskType
algorithm_type : DimensionalityReductionAlgorithmType
num_components : Int
kernel_type : KernelType
kernel_gamma : Double
kernel_degree : Int
kernel_coef0 : Double
perplexity : Double
learning_rate : Double
num_iterations : Int
n_neighbors : Int
min_dist : Double
metric : DistanceMetricTypeDR
regularization : Double
use_whitening : Bool
use_random_state : Bool
random_state : Int
early_exaggeration : Double
use_feature_selection : Bool
feature_selection_method : String
feature_threshold : Double
}
///|
/// 创建默认降维配置
pub fn create_default_dr_config() -> DimensionalityReductionConfig {
DimensionalityReductionConfig::{
task_type: DimensionalityReductionTaskType::PCA,
algorithm_type: DimensionalityReductionAlgorithmType::PCA,
num_components: 2,
kernel_type: KernelType::Linear,
kernel_gamma: 1.0,
kernel_degree: 3,
kernel_coef0: 1.0,
perplexity: 30.0,
learning_rate: 200.0,
num_iterations: 1000,
n_neighbors: 15,
min_dist: 0.1,
metric: DistanceMetricTypeDR::Euclidean,
regularization: 0.001,
use_whitening: false,
use_random_state: true,
random_state: 42,
early_exaggeration: 12.0,
use_feature_selection: false,
feature_selection_method: "variance",
feature_threshold: 0.01,
}
}
///|
/// 简化的均值计算
fn calculate_mean(
data : Array[Array[Double]],
dimension : Int,
) -> Array[Double] {
let mean : Array[Double] = []
for _ in 0..<dimension {
mean.push(0.0)
}
for sample in data {
for d in 0..<dimension {
if d < sample.length() {
mean[d] += sample[d]
}
}
}
let num_samples = data.length().to_double()
if num_samples > 0.0 {
for d in 0..<dimension {
mean[d] = mean[d] / num_samples
}
}
mean
}
///|
/// 简化的中心化
fn center_data(
data : Array[Array[Double]],
mean : Array[Double],
) -> Array[Array[Double]] {
let centered : Array[Array[Double]] = []
for sample in data {
let c : Array[Double] = []
for d in 0..<sample.length() {
if d < mean.length() {
c.push(sample[d] - mean[d])
} else {
c.push(sample[d])
}
}
centered.push(c)
}
centered
}
///|
/// 简化的协方差矩阵
fn calculate_covariance(
centered : Array[Array[Double]],
dimension : Int,
) -> Array[Array[Double]] {
let cov : Array[Array[Double]] = []
for i in 0..<dimension {
let row : Array[Double] = []
for j in 0..<dimension {
let mut sum = 0.0
for sample in centered {
if i < sample.length() && j < sample.length() {
sum += sample[i] * sample[j]
}
}
let n = centered.length().to_double()
row.push(if n > 1.0 { sum / (n - 1.0) } else { 0.0 })
}
cov.push(row)
}
cov
}
///|
/// 简化的PCA(使用幂迭代法求前k个特征向量)
pub fn pca_reduce(
data : Array[Array[Double]],
num_components : Int,
config : DimensionalityReductionConfig,
) -> PCAResult {
ignore(config)
let num_samples = data.length()
let original_dim = if num_samples > 0 { data[0].length() } else { 0 }
let mean = calculate_mean(data, original_dim)
let centered = center_data(data, mean)
let cov = calculate_covariance(centered, original_dim)
// 简化的特征值分解:使用幂迭代法
let components : Array[Array[Double]] = []
let explained_variance : Array[Double] = []
let explained_variance_ratio : Array[Double] = []
let singular_values : Array[Double] = []
let mut total_variance = 0.0
for i in 0..<original_dim {
if i < cov.length() && i < cov[i].length() {
total_variance += cov[i][i]
}
}
for _ in 0..<num_components {
// 幂迭代法
let mut v : Array[Double] = []
for _ in 0..<original_dim {
v.push(1.0 / original_dim.to_double())
}
for _ in 0..<100 {
let new_v : Array[Double] = []
for i in 0..<original_dim {
let mut sum = 0.0
for j in 0..<original_dim {
if i < cov.length() && j < cov[i].length() {
sum += cov[i][j] * v[j]
}
}
new_v.push(sum)
}
// 归一化
let mut norm = 0.0
for val in new_v {
norm += val * val
}
norm = norm
if norm > 0.0 {
for i in 0..<new_v.length() {
new_v[i] = new_v[i] / norm
}
}
v = new_v
}
// 计算特征值
let mut eigenvalue = 0.0
for i in 0..<original_dim {
let mut sum = 0.0
for j in 0..<original_dim {
if i < cov.length() && j < cov[i].length() {
sum += cov[i][j] * v[j]
}
}
eigenvalue += v[i] * sum
}
components.push(v)
explained_variance.push(eigenvalue)
singular_values.push(eigenvalue)
if total_variance > 0.0 {
explained_variance_ratio.push(eigenvalue / total_variance)
} else {
explained_variance_ratio.push(0.0)
}
// 减去已找到的成分(deflation)
for i in 0..<original_dim {
for j in 0..<original_dim {
if i < cov.length() && j < cov[i].length() {
cov[i][j] = cov[i][j] - eigenvalue * v[i] * v[j]
}
}
}
}
// 投影数据
let reduced : Array[Array[Double]] = []
for sample in centered {
let r : Array[Double] = []
for k in 0..<num_components {
let mut sum = 0.0
for d in 0..<original_dim {
if d < sample.length() &&
k < components.length() &&
d < components[k].length() {
sum += sample[d] * components[k][d]
}
}
r.push(sum)
}
reduced.push(r)
}
let mut cumulative = 0.0
for ratio in explained_variance_ratio {
cumulative += ratio
}
PCAResult::{
reduced_data: reduced,
num_components,
original_dimension: original_dim,
num_samples,
explained_variance,
explained_variance_ratio,
cumulative_explained_variance: cumulative,
components,
singular_values,
mean,
noise_variance: 0.0,
reconstruction_error: 0.01,
reduction_time_ms: 50.0,
}
}
///|
///|
/// 简化的t-SNE(使用随机初始化和简单梯度下降)
pub fn tsne_reduce(
data : Array[Array[Double]],
num_components : Int,
config : DimensionalityReductionConfig,
) -> TSNEResult {
let num_samples = data.length()
// 随机初始化低维表示
let reduced : Array[Array[Double]] = []
let mut state = config.random_state
for _ in 0..<num_samples {
let r : Array[Double] = []
for _ in 0..<num_components {
state = ((state * 11035 + 12345).reinterpret_as_uint() % 1000000).reinterpret_as_int()
let rand_val = (state % 1000).to_double() / 1000.0 - 0.5
r.push(rand_val * 0.01)
}
reduced.push(r)
}
// 简化的梯度下降(实际t-SNE需要计算高维亲和度和低维Q分布)
for _ in 0..<config.num_iterations {
// 简化:随机扰动
for i in 0..<num_samples {
for d in 0..<num_components {
state = ((state * 11035 + 12345).reinterpret_as_uint() % 1000000).reinterpret_as_int()
let noise = (state % 1000).to_double() / 1000.0 - 0.5
reduced[i][d] += noise * config.learning_rate * 0.001
}
}
}
TSNEResult::{
reduced_data: reduced,
num_components,
num_samples,
perplexity: config.perplexity,
learning_rate: config.learning_rate,
num_iterations: config.num_iterations,
kl_divergence: 1.5,
final_error: 0.5,
reduction_time_ms: 500.0,
early_exaggeration: config.early_exaggeration,
exaggeration_iterations: 250,
}
}
///|
/// 简化的UMAP
pub fn umap_reduce(
data : Array[Array[Double]],
num_components : Int,
config : DimensionalityReductionConfig,
) -> UMAPResult {
let num_samples = data.length()
// 随机初始化
let reduced : Array[Array[Double]] = []
let mut state = config.random_state
for _ in 0..<num_samples {
let r : Array[Double] = []
for _ in 0..<num_components {
state = ((state * 11035 + 12345).reinterpret_as_uint() % 1000000).reinterpret_as_int()
let rand_val = (state % 1000).to_double() / 1000.0 - 0.5
r.push(rand_val * 0.1)
}
reduced.push(r)
}
UMAPResult::{
reduced_data: reduced,
num_components,
num_samples,
n_neighbors: config.n_neighbors,
min_dist: config.min_dist,
metric: config.metric,
num_epochs: 200,
reconstruction_error: 0.1,
trustworthiness: 0.85,
continuity: 0.82,
reduction_time_ms: 300.0,
}
}
///|
/// 图像降维(统一接口)
pub fn reduce_dimensionality(
data : Array[Array[Double]],
config : DimensionalityReductionConfig,
) -> DimensionalityReductionResult {
let num_samples = data.length()
let original_dim = if num_samples > 0 { data[0].length() } else { 0 }
let pca_result = pca_reduce(data, config.num_components, config)
DimensionalityReductionResult::{
reduced_data: pca_result.reduced_data,
original_dimension: original_dim,
reduced_dimension: config.num_components,
num_samples,
explained_variance_ratio: pca_result.explained_variance_ratio,
cumulative_explained_variance: pca_result.cumulative_explained_variance,
reconstruction_error: pca_result.reconstruction_error,
stress: 0.1,
trustworthiness: 0.85,
continuity: 0.82,
reduction_time_ms: pca_result.reduction_time_ms,
components: pca_result.components,
singular_values: pca_result.singular_values,
mean: pca_result.mean,
}
}
///|
/// 特征选择(基于方差)
pub fn select_features(
data : Array[Array[Double]],
threshold : Double,
) -> FeatureSelectionResult {
let num_samples = data.length()
let num_features = if num_samples > 0 { data[0].length() } else { 0 }
// 计算每个特征的方差
let scores : Array[Double] = []
for f in 0..<num_features {
let mut sum = 0.0
for sample in data {
if f < sample.length() {
sum += sample[f]
}
}
let mean = if num_samples > 0 { sum / num_samples.to_double() } else { 0.0 }
let mut var_sum = 0.0
for sample in data {
if f < sample.length() {
let diff = sample[f] - mean
var_sum += diff * diff
}
}
let variance = if num_samples > 0 {
var_sum / num_samples.to_double()
} else {
0.0
}
scores.push(variance)
}
// 选择方差大于阈值的特征
let selected : Array[Int] = []
let ranking : Array[Int] = []
for f in 0..<num_features {
ranking.push(f)
}
// 简单排序
for _ in 0..<num_features {
for j in 0..<(num_features - 1) {
if scores[ranking[j]] < scores[ranking[j + 1]] {
let temp = ranking[j]
ranking[j] = ranking[j + 1]
ranking[j + 1] = temp
}
}
}
for f in 0..<num_features {
if scores[f] > threshold {
selected.push(f)
}
}
FeatureSelectionResult::{
selected_features: selected,
num_selected: selected.length(),
original_num_features: num_features,
feature_scores: scores,
feature_ranking: ranking,
selection_method: "variance",
threshold,
selection_time_ms: 20.0,
}
}
///|
/// 生成图像降维报告
pub fn dimensionality_reduction_report_to_string(
task_type : DimensionalityReductionTaskType,
config : DimensionalityReductionConfig,
result_description : String,
) -> String {
let mut report = "## 图像降维高级报告\n\n"
report = report + "### 任务信息\n\n"
report = report + "| 指标 | 值 |\n|------|------|\n"
report = report +
"| 任务类型 | " +
(match task_type {
DimensionalityReductionTaskType::PCA => "PCA"
DimensionalityReductionTaskType::LDA => "LDA"
DimensionalityReductionTaskType::TSNE => "t-SNE"
DimensionalityReductionTaskType::UMAP => "UMAP"
DimensionalityReductionTaskType::ISOMAP => "ISOMAP"
DimensionalityReductionTaskType::LLE => "LLE"
DimensionalityReductionTaskType::Autoencoder => "自动编码器"
DimensionalityReductionTaskType::ManifoldLearning => "流形学习"
DimensionalityReductionTaskType::FeatureSelection => "特征选择"
_ => "未知"
}) +
" |\n"
report = report +
"| 降维算法 | " +
(match config.algorithm_type {
DimensionalityReductionAlgorithmType::PCA => "PCA"
DimensionalityReductionAlgorithmType::KernelPCA => "核PCA"
DimensionalityReductionAlgorithmType::IncrementalPCA => "增量PCA"
DimensionalityReductionAlgorithmType::SparsePCA => "稀疏PCA"
DimensionalityReductionAlgorithmType::TruncatedSVD => "截断SVD"
DimensionalityReductionAlgorithmType::LDA => "LDA"
DimensionalityReductionAlgorithmType::TSNE => "t-SNE"
DimensionalityReductionAlgorithmType::UMAP => "UMAP"
DimensionalityReductionAlgorithmType::ISOMAP => "ISOMAP"
DimensionalityReductionAlgorithmType::LLE => "LLE"
DimensionalityReductionAlgorithmType::MDS => "MDS"
DimensionalityReductionAlgorithmType::SpectralEmbedding => "谱嵌入"
DimensionalityReductionAlgorithmType::Autoencoder => "自动编码器"
_ => "未知"
}) +
" |\n"
report = report +
"| 目标维度 | " +
config.num_components.to_string() +
" |\n"
report = report +
"| 核函数 | " +
(match config.kernel_type {
KernelType::Linear => "线性"
KernelType::Polynomial => "多项式"
KernelType::RBF => "RBF"
KernelType::Sigmoid => "Sigmoid"
KernelType::Cosine => "余弦"
_ => "未知"
}) +
" |\n"
report = report + "| 困惑度 | " + config.perplexity.to_string() + " |\n"
report = report + "| 学习率 | " + config.learning_rate.to_string() + " |\n"
report = report +
"| 迭代次数 | " +
config.num_iterations.to_string() +
" |\n"
report = report + "| 邻居数 | " + config.n_neighbors.to_string() + " |\n"
report = report + "| 最小距离 | " + config.min_dist.to_string() + " |\n"
report = report +
"| 白化 | " +
(if config.use_whitening { "开启" } else { "关闭" }) +
" |\n"
report = report + "\n### 结果\n\n"
report = report + result_description + "\n"
report
}