Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tdde01-ht24
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mehce338-felra653-sigjo290
tdde01-ht24
Commits
69430c3c
Commit
69430c3c
authored
4 months ago
by
Mehmet Celik Yildirim
Browse files
Options
Downloads
Patches
Plain Diff
code cleanup assignment 1
parent
aeaf1e1f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lab2/assignment1.R
+18
-21
18 additions, 21 deletions
lab2/assignment1.R
with
18 additions
and
21 deletions
lab2/assignment1.R
+
18
−
21
View file @
69430c3c
...
...
@@ -34,40 +34,37 @@ print(paste("MSE on the test data:", mse(test$Fat, test_pred)))
#----3.----
fit
<-
glmnet
(
as.matrix
(
X_train
),
y_train
,
alpha
=
1
)
plot
(
fit
,
xvar
=
"lambda"
,
label
=
TRUE
)
coef_matrix
<-
as.matrix
(
coef
(
fit
))[
-1
,
]
#ignore intercept
lambda_values
<-
fit
$
lambda
lambda_with_k_features
=
function
(
x_train
,
y_train
,
a
,
k
=
3
){
fit_train
<-
glmnet
(
as.matrix
(
X_train
),
y_train
,
alpha
=
a
)
plot
(
fit_train
,
xvar
=
"lambda"
,
label
=
TRUE
)
coef_matrix
<-
as.matrix
(
coef
(
fit_train
))[
-1
,
]
# Ignore intercept
lambda_values
<-
fit_train
$
lambda
num_non_zero
<-
apply
(
coef_matrix
!=
0
,
2
,
sum
)
# Sum of num of cols wit non zero coeff, i.e. number of features
lambda_values
[
num_non_zero
==
k
]
# lambda value wit 3 non zero coeff, i.e. number of features
num_non_zero
<-
apply
(
coef_matrix
!=
0
,
2
,
sum
)
}
lambda_with_3_features
<-
lambda_
values
[
num_non_zero
==
3
]
lambda_with_3_features
=
lambda_
with_k_features
(
x_train
,
y_train
,
1
,
3
)
print
(
lambda_with_3_features
)
#----4.----
fit
<-
glmnet
(
as.matrix
(
X_train
),
y_train
,
alpha
=
0
)
plot
(
fit
,
xvar
=
"lambda"
,
label
=
TRUE
)
coef_matrix
<-
as.matrix
(
coef
(
fit
))[
-1
,
]
#ignore intercept
lambda_values
<-
fit
$
lambda
num_non_zero
<-
apply
(
coef_matrix
!=
0
,
2
,
sum
)
lambda_with_3_features
<-
lambda_values
[
num_non_zero
==
3
]
lambda_with_3_features
=
lambda_with_k_features
(
x_train
,
y_train
,
0
,
3
)
print
(
lambda_with_3_features
)
#----5.----
fit
<-
cv.glmnet
(
as.matrix
(
X_train
),
y_train
,
alpha
=
1
)
fit
<-
cv.glmnet
(
as.matrix
(
X_train
),
y_train
,
alpha
=
1
)
# Cross-validation
plot
(
fit
,
xvar
=
"lambda"
,
label
=
TRUE
)
...
...
@@ -77,4 +74,4 @@ coef(fit, s="lambda.min")
y_hat
=
predict
(
fit
,
newx
=
as.matrix
(
X_train
),
s
=
"lambda.min"
)
plot
(
y_train
,
y_hat
)
plot
(
y_train
,
y_hat
,
xlab
=
"Observed test values"
,
ylab
=
"Predicted test values"
,
main
=
"Original versus predicted test values "
,
col
=
"blue"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment