# Polynomial overfitting and y3- Help

**URL:** <https://discourse.julialang.org/t/polynomial-overfitting-and-y3-help/105239>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 20, 2023, 1:28pm UTC](https://discourse.julialang.org/t/polynomial-overfitting-and-y3-help/105239 "2023-10-20T13:28:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anafranchi](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@anafranchi](https://discourse.julialang.org/u/anafranchi)\
**Post date:** [October 20, 2023, 1:28pm UTC](https://discourse.julialang.org/t/polynomial-overfitting-and-y3-help/105239/1 "2023-10-20T13:28:59Z")

</div>

Hi guys, I’m learning Julia. I would like to ask you a question. How do I insert y3 into the graph?  
and then why order 8 comes out with so much overfitting. A more compliant line should emerge as the result of the photo I attach.  
In the comments I will attach my result instead.  
Can you correct the code where the error is present, because I can’t understand.  
Thank you  
 ![5c18b6b6-5410-4987-84d6-57e834ca14a1](https://global.discourse-cdn.com/julialang/original/3X/d/5/d5fe06de2d47ce93e72cc54b16aa68f009f04c7f.jpeg)  
this is the code i used:

```julia
using Plots
using 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))

```

---

<div class="post-metadata">

**Author:** ![anafranchi](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@anafranchi](https://discourse.julialang.org/u/anafranchi)\
**Post date:** [October 20, 2023, 1:29pm UTC](https://discourse.julialang.org/t/polynomial-overfitting-and-y3-help/105239/2 "2023-10-20T13:29:31Z")

</div>

![Screenshot 2023-10-19 alle 21.54.30](https://global.discourse-cdn.com/julialang/original/3X/f/c/fc12cf0eb930252497a8b7f4642a0a89d0557e69.png)  
my result.
