Overfitting polynomial. help

Hi guys, I’m having trouble with this exercise. Order 18 is too overfitting for me. how can I solve it? a graph like the one on the orange line should come out. the one with the red line is mine. where I’m wrong in the code, can you correct it please…
![Screenshot 2023-10-19 alle 21.54.30|628x441]

this is my Assignment

You should use the function from above - the Φ feature generator - to approximate the points.

Below you solve for different polynomial orders and plot the result.

Use sub-plots within the Plots ecosystem, and prepare a plot with subplots:

col1 col2 col3
(2) (4) (6)
(8) (12) (18)

|Each subplot should contain|||

  • the points,
  • the order of polynomial and the error in the title string,
  • the plot aligned such that all points are well visible and the quality of the approximation is visible too.

When submitting the solution, make sure that your solution

this is my code:
Preformatted textusing CSV, DataFrames
using LinearAlgebra

Lettura dei dati come mostrato in precedenza

train_data = Matrix(CSV.read(“hw_data.csv”, DataFrame, header = false))
x_height = train_data[:,1]
y_weight = train_data[:,2]

Funzione lineare per il modello

linear_fun(height, a) = a[2] * height + a[1]

Funzione generatrice di caratteristiche

function Φ(x::Real, k::Integer; μ=165, σ=50)::Vector{Real}
z = (x-μ) / σ
return [z.^i for i=0:k]
end
function approximate_and_error(k)
Φ_matrix = hcat([Φ(x, k) for x in x_height]…)
Φ_matrix = transpose(Φ_matrix) # Trasponiamo per avere le dimensioni corrette
a = Φ_matrix \ y_weight
y_pred = Φ_matrix * a
error = norm(y_weight - y_pred)
return a, error
end
function plot_approximation(k)
a, error = approximate_and_error(k)

x_dense = range(minimum(x_height), maximum(x_height), length=1000)
y_preds_dense = [dot(a, Φ(x, k)) for x in x_dense]

p = scatter(x_height, y_weight, label="y2", markersize=5, color=:blue, legend=:topleft, xlabel="X Axis Label", ylabel="Y Axis Label", size=(600,400))

plot!(p, x_dense, y_preds_dense, label="y3 - Order $k", width=2, linecolor=:orange, linestyle=:solid)

title!("Grado $k - Errore: $(round(error; digits=2))")
return p

end

Creazione delle sottotrame

plots_array = [plot_approximation(k) for k in [2,4,6,8,12,18]]
plot(plots_array…, layout=(2,3), size=(900,600)).
5c18b6b6-5410-4987-84d6-57e834ca14a1

Screenshot 2023-10-19 alle 21.54.30

How is this different from your asks here? Polynomial problem-help me please

This looks to be the same problem, let’s keep things contained there.