-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmain.v
155 lines (140 loc) · 3.02 KB
/
main.v
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
module main
import vsl.ml
import vsl.plot
fn main() {
// Example data: two features (X1 and X2) and a label (y)
mut data := ml.Data.from_raw_xy([
[1.0, 2.0, 0.0],
[2.0, 3.0, 0.0],
[3.0, 3.0, 0.0],
[2.0, 1.0, 0.0],
[6.0, 7.0, 1.0],
[8.0, 6.0, 1.0],
[7.0, 8.0, 1.0],
[8.0, 7.0, 1.0],
[4.0, 5.0, 0.0],
[5.0, 6.0, 0.0],
[6.0, 5.0, 0.0],
[5.0, 4.0, 0.0],
[9.0, 10.0, 1.0],
[10.0, 9.0, 1.0],
[11.0, 10.0, 1.0],
[10.0, 11.0, 1.0],
[12.0, 11.0, 1.0],
[11.0, 12.0, 1.0],
[13.0, 14.0, 1.0],
[14.0, 13.0, 1.0],
[15.0, 14.0, 1.0],
[14.0, 15.0, 1.0],
[16.0, 15.0, 1.0],
[15.0, 16.0, 1.0],
[17.0, 18.0, 1.0],
[18.0, 17.0, 1.0],
[19.0, 18.0, 1.0],
[18.0, 19.0, 1.0],
[3.0, 4.0, 0.0],
[4.0, 3.0, 0.0],
[7.0, 6.0, 1.0],
[6.0, 8.0, 1.0],
[9.0, 8.0, 1.0],
[8.0, 9.0, 1.0],
[11.0, 12.0, 1.0],
[12.0, 13.0, 1.0],
[15.0, 14.0, 1.0],
[14.0, 16.0, 1.0],
[18.0, 17.0, 1.0],
[17.0, 19.0, 1.0],
[5.0, 5.0, 0.0],
[10.0, 10.0, 1.0],
[15.0, 15.0, 1.0],
[4.0, 8.0, 1.0],
[8.0, 4.0, 0.0],
[12.0, 16.0, 1.0],
[16.0, 12.0, 1.0],
[20.0, 5.0, 0.0],
[5.0, 20.0, 0.0],
[12.0, 8.0, 1.0],
[8.0, 12.0, 1.0],
[18.0, 16.0, 1.0],
[16.0, 18.0, 1.0],
])!
// Visualize data in a 3D scatter plot
mut plt_3d := plot.Plot.new()
x1 := data.x.get_col(0)
x2 := data.x.get_col(1)
y := data.y
// Split data into two classes for visualization
mut x1_class0 := []f64{}
mut x2_class0 := []f64{}
mut x1_class1 := []f64{}
mut x2_class1 := []f64{}
for i in 0 .. data.nb_samples {
if y[i] == 0.0 {
x1_class0 << x1[i]
x2_class0 << x2[i]
} else {
x1_class1 << x1[i]
x2_class1 << x2[i]
}
}
// Add traces for each class in the 3D plot
plt_3d.scatter3d(
x: x1_class0
y: x2_class0
z: [][]f64{len: x1_class0.len, init: [0.0]}
mode: 'markers'
marker: plot.Marker{
size: []f64{len: x1_class0.len, init: 8.0}
color: []string{len: x1_class0.len, init: 'blue'}
}
name: 'Class 0'
)
plt_3d.scatter3d(
x: x1_class1
y: x2_class1
z: [][]f64{len: x1_class1.len, init: [0.0]}
mode: 'markers'
marker: plot.Marker{
size: []f64{len: x1_class1.len, init: 8.0}
color: []string{len: x1_class1.len, init: 'red'}
}
name: 'Class 1'
)
// Configure the layout of the 3D plot
plt_3d.layout(
title: 'Two-class Data'
xaxis: plot.Axis{
title: plot.AxisTitle{
text: 'X1'
}
}
yaxis: plot.Axis{
title: plot.AxisTitle{
text: 'X2'
}
}
)
// Show the 3D plot
plt_3d.show()!
// Basic statistics analysis
mut stat := ml.Stat.from_data(mut data, 'Example Data')
stat.update()
// Visualize statistics in a bar chart
mut plt_bars := plot.Plot.new()
plt_bars.bar(
x: []string{len: stat.mean_x.len, init: 'Class ${index}'}
y: stat.mean_x
name: 'Mean'
)
plt_bars.bar(
x: []string{len: stat.sig_x.len, init: 'Class ${index}'}
y: stat.sig_x
name: 'Standard Deviation'
)
// Configure the layout of the bar chart
plt_bars.layout(
title: 'Feature Statistics'
)
// Show the bar chart
plt_bars.show()!
}