Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F102754001
plots.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Feb 23, 20:38
Size
1 KB
Mime Type
text/x-python
Expires
Tue, Feb 25, 20:38 (2 d)
Engine
blob
Format
Raw Data
Handle
24418416
Attached To
rTZUCT ML_Project1
plots.py
View Options
# -*- coding: utf-8 -*-
"""a function of ploting figures."""
import
numpy
as
np
from
build_polynomial
import
*
import
matplotlib.pyplot
as
plt
def
plot_fitted_curve
(
y
,
x
,
weights
,
degree
,
ax
):
"""plot the fitted curve."""
ax
.
scatter
(
x
,
y
,
color
=
'b'
,
s
=
12
,
facecolors
=
'none'
,
edgecolors
=
'r'
)
xvals
=
np
.
arange
(
min
(
x
)
-
0.1
,
max
(
x
)
+
0.1
,
0.1
)
tx
=
build_poly
(
xvals
,
degree
)
f
=
tx
.
dot
(
weights
)
ax
.
plot
(
xvals
,
f
)
ax
.
set_xlabel
(
"x"
)
ax
.
set_ylabel
(
"y"
)
ax
.
set_title
(
"Polynomial degree "
+
str
(
degree
))
def
plot_train_test
(
train_errors
,
test_errors
,
lambdas
,
degree
):
"""
train_errors, test_errors and lambas should be list (of the same size) the respective train error and test error for a given lambda,
* lambda[0] = 1
* train_errors[0] = RMSE of a ridge regression on the train set
* test_errors[0] = RMSE of the parameter found by ridge regression applied on the test set
degree is just used for the title of the plot.
"""
plt
.
semilogx
(
lambdas
,
train_errors
,
color
=
'b'
,
marker
=
'*'
,
label
=
"Train error"
)
plt
.
semilogx
(
lambdas
,
test_errors
,
color
=
'r'
,
marker
=
'*'
,
label
=
"Test error"
)
plt
.
xlabel
(
"lambda"
)
plt
.
ylabel
(
"RMSE"
)
plt
.
title
(
"Ridge regression for polynomial degree "
+
str
(
degree
))
leg
=
plt
.
legend
(
loc
=
1
,
shadow
=
True
)
leg
.
draw_frame
(
False
)
plt
.
savefig
(
"ridge_regression"
)
Event Timeline
Log In to Comment