diff --git a/lab2/assignment1.R b/lab2/assignment1.R index 4923521cdd767f38a6563705c3e58471bc1e633b..b706dd3ddd11555b28535a73fd93e24c35d868ed 100644 --- a/lab2/assignment1.R +++ b/lab2/assignment1.R @@ -44,9 +44,11 @@ lambda_with_k_features = function(x_train, y_train, alpha, k = 3) { 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 + # sum of num of cols wit non zero coeff, i.e. number of features + num_non_zero <- apply(coef_matrix != 0, 2, sum) - lambda_values[num_non_zero == k] # lambda value with k non zero coeff, i.e. number of features + # lambda value with k non zero coeff, i.e. number of features + lambda_values[num_non_zero == k] } diff --git a/lab2/assignment2.R b/lab2/assignment2.R index c9039778de9b4d5dfedc6c4b2fbccef7546b729b..f1c95f1dbfadb59a2a8845f04969aec8f9b600ed 100644 --- a/lab2/assignment2.R +++ b/lab2/assignment2.R @@ -149,7 +149,8 @@ accuracy <- function(x, x_pred) { } F1 <- function(x, x_pred) { - return(2 * precision(x, x_pred) * recall(x, x_pred) / (precision(x, x_pred) + recall(x, x_pred))) + return(2 * precision(x, x_pred) * recall(x, x_pred) / (precision(x, x_pred) + + recall(x, x_pred))) } y_hat4 <- predict(optimal_tree, newdata = test, type = "class") diff --git a/lab2/assignment3.R b/lab2/assignment3.R index 291fa3fbbdd68708dffb4dcea96f64996b663be9..6441e7a1d526b5ff96aee21aa57df5c26c993f8d 100644 --- a/lab2/assignment3.R +++ b/lab2/assignment3.R @@ -67,9 +67,8 @@ test = data[-id, ] scaler3 <- preProcess(train) -train_scaled<- predict(scaler3, train) +train_scaled <- predict(scaler3, train) -test_scaled <- test test_scaled <- predict(scaler3, test) X_train <- train_scaled[, feature_column_names] @@ -99,17 +98,45 @@ test_errors <- c() cost_function <- function(theta) { train_error <- MSE(y_train, as.matrix(X_train) %*% theta) + + + return(train_error) +} + +theta <- rep(0, ncol(X_train)) + +# res <- optim( +# par = theta, +# fn = cost_function, +# method = "BFGS", +# control = list(trace = 1, maxit = 200, REPORT = 1), +# ) + +for (i in 1:5000) { + + print(i) + res <- optim( + par = theta, + fn = cost_function, + method = "BFGS", + control = list(maxit = 1), + ) + + print(res$value) + + theta <- res$par + test_error <- MSE(y_test, as.matrix(X_test) %*% theta) - train_errors <<- c(train_errors, train_error) + train_errors <<- c(train_errors, res$value) test_errors <<- c(test_errors, test_error) - return(train_error) + if (res$convergence == 0) { + break + } } -res <- optim(par = rep(0, ncol(X_train)), - fn = cost_function, - method = "BFGS",) +res$counts range <- c(train_errors[501:length(train_errors)], test_errors[501:length(test_errors)]) @@ -122,7 +149,7 @@ plot( main = "Training and Test Errors over Iterations", lty = 1, ylim = c(min(range), max(range)), - xlim = c(501,length(train_errors)), + xlim = c(501, length(train_errors)), ) points(test_errors, diff --git a/lab2/figures/assignment3-4.eps b/lab2/figures/assignment3-4.eps index f156ce98a9a7f9e7654665b39c4bffc8d57ee9d5..484234983a2e80b0e0b3d9955ee694ed472fe41e 100644 --- a/lab2/figures/assignment3-4.eps +++ b/lab2/figures/assignment3-4.eps @@ -131,7 +131,6 @@ dup length dict begin end /Font5 exch definefont pop %%EndProlog -0.00 0.00 450.00 300.00 cl %%Page: 1 1 bp /bg { 1 1 1 srgb } def @@ -40511,25 +40510,25 @@ o 0 0 0 srgb 0.75 setlinewidth [] 0 setdash -313.92 240.96 105.84 -37.12 r p3 +290.94 240.96 128.82 -37.12 r p3 0 0 1 srgb np -323.14 228.59 m -18.43 0 l +302.15 228.59 m +22.44 0 l o 1 0 0 srgb 0.75 setlinewidth [ 2.25 3.75] 0 setdash np -323.14 216.21 m -18.43 0 l +302.15 216.21 m +22.44 0 l o /Font1 findfont 12 s 0 0 0 srgb -350.79 224.28 (T) 0 ta +335.81 224.28 (T) 0 ta -1.440 (r) tb -0.120 (aining Error) tb gr -350.79 211.90 (T) 0 ta +335.81 211.90 (T) 0 ta -1.440 (est Error) tb gr ep %%Trailer diff --git a/lab2/figures/assignment3-4.png b/lab2/figures/assignment3-4.png index cc6b1d311f37cafd53dadedd31f61a5223c3e9a6..0e747b8b028581d3385f2bdc04d9e874dc61f97a 100644 Binary files a/lab2/figures/assignment3-4.png and b/lab2/figures/assignment3-4.png differ