MCPcopy Create free account
hub / github.com/daniel-e/rustml / plot_knn_decision_boundary

Function plot_knn_decision_boundary

examples/plots.rs:65–115  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

63}
64
65pub 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
117pub fn plot_nn() {
118

Callers 1

mainFunction · 0.85

Calls 15

mixture_builderFunction · 0.85
normal_builderFunction · 0.85
classifyFunction · 0.85
from_fileFunction · 0.85
as_matrixMethod · 0.80
mapMethod · 0.80
columnMethod · 0.80
rm_columnMethod · 0.80
add_rowMethod · 0.80
runMethod · 0.80
add_matrixMethod · 0.80
wait_keyMethod · 0.80

Tested by

no test coverage detected