# Overfitting polynomial. help

**URL:** https://discourse.julialang.org/t/overfitting-polynomial-help/105200
**Category:** New to Julia
**Tags:** question, homework
**Created:** [October 19, 2023, 7:51pm UTC](https://discourse.julialang.org/t/overfitting-polynomial-help/105200 "2023-10-19T19:51:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![condor98](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/condor98/32/202607_2.png) [@condor98](https://discourse.julialang.org/u/condor98)
#### Post date: [October 19, 2023, 7:51pm UTC](https://discourse.julialang.org/t/overfitting-polynomial-help/105200/1 "2023-10-19T19:51:33Z")

</div>

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 text`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)).

 ![5c18b6b6-5410-4987-84d6-57e834ca14a1](https://global.discourse-cdn.com/julialang/original/3X/d/5/d5fe06de2d47ce93e72cc54b16aa68f009f04c7f.jpeg)

---

<div class="post-metadata">

### Author: ![condor98](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/condor98/32/202607_2.png) [@condor98](https://discourse.julialang.org/u/condor98)
#### Post date: [October 19, 2023, 7:52pm UTC](https://discourse.julialang.org/t/overfitting-polynomial-help/105200/2 "2023-10-19T19:52:34Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [October 19, 2023, 9:36pm UTC](https://discourse.julialang.org/t/overfitting-polynomial-help/105200/3 "2023-10-19T21:36:54Z")

</div>

How is this different from your asks here? [Polynomial problem-help me please](https://discourse.julialang.org/t/polynomial-problem-help-me-please/105195)

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

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [October 19, 2023, 9:36pm UTC](https://discourse.julialang.org/t/overfitting-polynomial-help/105200/4 "2023-10-19T21:36:59Z")

</div>


