Skip to content

Commit

Permalink
Updated ml plot
Browse files Browse the repository at this point in the history
  • Loading branch information
ulises-jeremias committed Nov 5, 2023
1 parent 7604e75 commit 7fa7171
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 25 deletions.
2 changes: 1 addition & 1 deletion examples/ml_kmeans_plot/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ fn main() {
model.train(epochs: 6)

// Plot the results using the new plot method
model.plot()!
model.get_plotter().show()!
}
2 changes: 1 addition & 1 deletion examples/ml_knn_plot/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ fn main() {
println('Prediction: ${prediction}')

// Plot the KNN model
knn.plot()!
knn.get_plotter().show()!
}
2 changes: 1 addition & 1 deletion examples/ml_linreg_plot/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ fn main() {

reg.train()

reg.plot()!
reg.get_plotter().show()!
}
6 changes: 3 additions & 3 deletions ml/kmeans.v
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ pub fn (o &Kmeans) str() string {
return res.join('\n')
}

// plot method for visualizing the clustering
pub fn (o &Kmeans) plot() ! {
// get_plotter returns a plot.Plot struct for plotting
pub fn (o &Kmeans) get_plotter() &plot.Plot {
mut plt := plot.Plot.new()
plt.layout(
title: 'K-means Clustering'
Expand Down Expand Up @@ -194,5 +194,5 @@ pub fn (o &Kmeans) plot() ! {
}
)

plt.show()!
return plt
}
7 changes: 4 additions & 3 deletions ml/knn.v
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ pub fn (o &KNN) str() string {
return res.join('\n')
}

// plot method for visualizing the KNN model
pub fn (o &KNN) plot() ! {
// get_plotter returns a plot.Plot struct with the data needed to plot
// the KNN model.
pub fn (o &KNN) get_plotter() &plot.Plot {
mut plt := plot.Plot.new()
plt.layout(
title: 'K-Nearest Neighbors'
Expand Down Expand Up @@ -237,5 +238,5 @@ pub fn (o &KNN) plot() ! {
)
}

plt.show()!
return plt
}
18 changes: 4 additions & 14 deletions ml/linreg.v
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ pub fn (o &LinReg) str() string {
return res.join('\n')
}

// plot plots the data and the linear regression model
pub fn (o &LinReg) plot() ! {
// get_plotter returns a plot.Plot struct for plotting the data and the linear regression model
pub fn (o &LinReg) get_plotter() &plot.Plot {
// Get the minimum and maximum values of the features
min_x := o.stat.min_x[0]
max_x := o.stat.max_x[0]
Expand All @@ -167,28 +167,18 @@ pub fn (o &LinReg) plot() ! {
plt.layout(
title: 'Linear Regression Example'
)

plt.scatter(
name: 'dataset'
x: o.data.x.get_col(0)
y: o.data.y
mode: 'markers'
colorscale: 'smoker'
marker: plot.Marker{
size: []f64{len: o.data.y.len, init: 10.0}
}
)

plt.scatter(
name: 'linear regression'
name: 'prediction'
x: x_values
y: y_values
mode: 'lines'
colorscale: 'smoker'
line: plot.Line{
color: 'red'
}
)

plt.show()!
return plt
}
4 changes: 2 additions & 2 deletions plot/plot.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub mut:
layout Layout
}

pub fn Plot.new() Plot {
return Plot{}
pub fn Plot.new() &Plot {
return &Plot{}
}

// add_trace adds a trace to the plot
Expand Down

0 comments on commit 7fa7171

Please sign in to comment.