()
| 63 | } |
| 64 | |
| 65 | pub fn plot_knn_decision_boundary() { |
| 66 | |
| 67 | let seed = [2, 3, 5, 7]; |
| 68 | |
| 69 | let m = |
| 70 | mixture_builder() |
| 71 | .add(100, normal_builder(seed).add(1.0, 2.2).add(2.0, 1.2)) |
| 72 | .add(100, normal_builder(seed).add(5.0, 2.5).add(6.0, 2.5)) |
| 73 | .add(100, normal_builder(seed).add(6.0, 2.5).add(0.0, 2.5)) |
| 74 | .as_matrix(); |
| 75 | |
| 76 | let labels = m.column(0).unwrap().iter().map(|&x| x.clone() as usize).collect::<Vec<usize>>(); |
| 77 | let mx = m.rm_column(0).unwrap(); |
| 78 | |
| 79 | let mut mt = Matrix::<f64>::new(); |
| 80 | for y in (-49..100) { |
| 81 | for x in (-19..100) { |
| 82 | let xp = x as f64 / 10.0; |
| 83 | let yp = y as f64 / 10.0; |
| 84 | let l = classify(&mx, &labels, &[xp, yp], 5, |x, y| Euclid::compute(x, y).unwrap()); |
| 85 | mt.add_row(&[l as f64, xp, yp]); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | builder() |
| 90 | .add_matrix("X = $$", &mt) |
| 91 | .add("A = X(X(:,1) == 0, 2:end)") |
| 92 | .add("B = X(X(:,1) == 1, 2:end)") |
| 93 | .add("C = X(X(:,1) == 2, 2:end)") |
| 94 | .add("scatter(A(:,1), A(:,2), 5, [1, 1, 0.7], 'filled')") |
| 95 | .add("hold on") |
| 96 | .add("scatter(B(:,1), B(:,2), 5, [0.7, 0.7, 1], 'filled')") |
| 97 | .add("scatter(C(:,1), C(:,2), 5, [1, 0.7, 0.7], 'filled')") |
| 98 | .add_matrix("X = $$", &m) |
| 99 | .add("A = X(X(:,1) == 0, 2:end)") |
| 100 | .add("B = X(X(:,1) == 1, 2:end)") |
| 101 | .add("C = X(X(:,1) == 2, 2:end)") |
| 102 | .add("plot(A(:,1), A(:,2), 'o', 'markerfacecolor', 'yellow', 'color', 'black', 'markersize', 7)") |
| 103 | .add("plot(B(:,1), B(:,2), 's', 'markerfacecolor', 'blue', 'color', 'black', 'markersize', 6)") |
| 104 | .add("plot(C(:,1), C(:,2), 'd', 'markerfacecolor', 'red', 'color', 'black', 'markersize', 8)") |
| 105 | .add("grid on") |
| 106 | .add("axis([-2, 10, -5, 10])") |
| 107 | .add("axis('nolabel')") |
| 108 | .add("print -r50 -dpng /tmp/plot_knn_boundary.png") |
| 109 | .run("/tmp/plot_knn_boundary.m") |
| 110 | .unwrap(); |
| 111 | |
| 112 | Window::new() |
| 113 | .show_image(&RgbImage::from_file("/tmp/plot_knn_boundary.png").unwrap()) |
| 114 | .wait_key(); |
| 115 | } |
| 116 | |
| 117 | pub fn plot_nn() { |
| 118 |
no test coverage detected