Untitled document (7)
pdf
keyboard_arrow_up
School
University of Houston *
*We aren’t endorsed by this school
Course
1311
Subject
Industrial Engineering
Date
Apr 3, 2024
Type
Pages
10
Uploaded by rashusha505
HW 3
#1(a)
plot(NA, xlim = c(-3, 3), ylim = c(-3, 3), xlab = "X1", ylab = "X2")
# X1 - 2 * X2 = 0
abline(h = 0, col = "blue")
# X1 - 2 * X2 > 0 (above the line) red dots
points(0, 2, pch = 19, col = "red")
# Point (0, 2)
points(2, 1, pch = 19, col = "red")
# Point (2, 1)
# X1 - 2 * X2 < 0 (below the line) green triangles
points(0, -2, pch = 17, col = "green")
# Point (0, -2)
#points
text(0, 2, "X1 = 0, X2 = 2", pos = 2)
text(2, 1, "X1 = 2, X2 = 1", pos = 2)
text(0, -2, "X1 = 0, X2 = -2", pos = 2)
# legend
legend("topright", legend = c("Hyperplane", "X1 - 2*X2 > 0", "X1 - 2*X2 < 0"),
col = c("blue", "red", "green"), pch = c(-1, 19, 17))
#1(b)
plot(NA, xlim = c(-3, 3), ylim = c(-3, 3), xlab = "X1", ylab = "X2")
# X1 + X2 - 2 = 0
abline(a = 2, b = -1, col = "blue")
# X1 + X2 - 2 > 0 (above the line) red dots
points(1, 1, pch = 19, col = "red")
# Point (1, 1)
# X1 + X2 - 2 < 0 (below the line) green triangles
points(-1, -1, pch = 17, col = "green")
# Point (-1, -1)
points(1, 1, pch = 17, col = "green")
# Point (1, 1)
# points
text(1, 1, "X1 = 1, X2 = 1", pos = 2)
text(-1, -1, "X1 = -1, X2 = -1", pos = 2)
#legend
legend("topright", legend = c("Hyperplane", "X1 + X2 - 2 > 0", "X1 + X2 - 2 < 0"),
col = c("blue", "red", "green"), pch = c(-1, 19, 17))
#2(a/b)
plot(NA, NA, xlim = c(-10, 10), ylim = c(-10, 10), xlab = "X1", ylab = "X2")
curve(sqrt(9 - (1 + x)^2) + 2, from = -10, to = 10, col = "blue", lwd = 2, add = TRUE)
curve(-sqrt(9 - (1 + x)^2) + 2, from = -10, to = 10, col = "blue", lwd = 2, add = TRUE)
curve(sqrt(16 - (1 + x)^2) + 2, from = -10, to = 10, col = "red", lwd = 2, add = TRUE)
curve(-sqrt(16 - (1 + x)^2) + 2, from = -10, to = 10, col = "red", lwd = 2, add = TRUE)
# labels for the curves
text(2, 7, "Circle with r = 3", col = "blue", pos = 4)
text(2, -5, "Circle with r = 4", col = "red", pos = 4)
# Create points
X1_points <- seq(-10, 10, by = 0.1)
X2_points <- seq(-10, 10, by = 0.1)
# values of the expressions for the points
expression_values <- outer(X1_points, X2_points,
FUN = function(x1, x2) (1 + x1)^2 + (2 - x2)^2)
points(X1_points[expression_values > 16], X2_points[expression_values > 16], pch = 19, col
= "blue")
points(X1_points[expression_values >= 9 & expression_values <= 16],
X2_points[expression_values >= 9 & expression_values <= 16], pch = 19, col = "red")
points(X1_points[expression_values < 9], X2_points[expression_values < 9], pch = 19, col =
"white")
# legend
legend("topright", legend = c("Expression > 16", "9 <= Expression <= 16", "Expression < 9"),
col = c("blue", "red", "white"), pch = 19)
#2(c)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
(1+X1)^2+(2-X2)^2
For (0,0)
(1+0)^2+(2-0)^2=5
9<=5<=16(false)
5 is not greater than 9 therefore, it is classified as RED
For (-1,1)
(1+(-1))^2+(2-1)^2=1
9<=1<=16(false)
1 is not greater than 9 therefore, it is classified as RED
For (2,2)
(1+2)^2+(2-2)^2=9
9<=9<=16(true)
9 is = to 9 and greater than 16 therefore, it is classified as BLUE
For (3,4)
(1+3)^2+(2-4)^2=20
9<=20<=16(false)
20 is not less than 16, therefore it is classified as RED
#2(d)
(1+X1)^2+(2-X2)^2=16
X1^2+2X1+X2^2-4X^2=11 (expanded)
We can see that each coefficient of the variables X1 and X2 have a constant multiplier of
one of the terms involving them therefore, X1,X2,X1^2,X2^2 are linear combination of these
variables.
#3(a/b)
X1 = c(1, 3, 4, 2, 4, 4, 1, 1)
X2 = c(4, 4, 3, 2, 2, 4, 2, 3)
Y = factor(c("Blue", "Red", "Red", "Blue", "Red", "Red", "Blue", "Blue"))
plot(X1, X2, col = Y, xlim = c(0, 5), ylim = c(0, 5),main = "Optimal Separating Hyperplane")
slope=1/3
abline(0.5,1)
#3(C)
The classification rule is classify to Red if X1+X2>0
, and classify to Blue otherwise.
abline(0, 1, lty = 2)
abline(1, 1, lty = 2)
#3(d)
#3(e)
Supporting vectors : (1,2) , (2,1) , (3,4) , (4,3)
#3(f)
In a maximal margin classifier, only the support vectors influence the hyperplane's position.
#3(g)
plot(X1, X2, col = Y, xlim = c(0, 5), ylim = c(0, 5))
abline(-0.2, 1)
X1-X2-0.2=0 is not optimal separating hyperplane
#3(h)
plot(X1, X2, col = Y, xlim = c(0, 5), ylim = c(0, 5))
points(c(3), c(2), col = c("red"))
#4(a)
library(MASS)
data(Boston)
median_medv <- median(Boston$medv)
Boston$medv01 <- ifelse(Boston$medv > median_medv, 1, 0)
Boston$medv <- NULL
(b)
KNN
K=5
K=3
K=7
(C)
d) No, not all the variables in the Boston dataset are on the same scale
To deal with variables that are not on the same scale, you can perform feature scaling or
standardization.
5)a)
library(ISLR)
data("Auto")
median_mpg <- median(Auto$mpg)
Auto$above_median <- ifelse(Auto$mpg > median_mpg, 1, 0)
Auto$mpg <- NULL
head(Auto)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
head(Auto)
cylinders displacement horsepower weight acceleration year origin
name
above_median
1
8
307
130
3504
12.0
70
1 chevrolet chevelle malibu
0
2
8
350
165
3693
11.5
70
1
buick skylark 320
0
3
8
318
150
3436
11.0
70
1
plymouth satellite
0
4
8
304
150
3433
12.0
70
1
amc rebel sst
0
5
8
302
140
3449
10.5
70
1
ford torino
0
6
8
429
198
4341
10.0
70
1
ford galaxie 500
0
b)
library(e1071)
set.seed(1)
cost_values <- 10^seq(-2, 2, by = 0.5)
cv_errors <- numeric(length(cost_values))
for (i in 1:length(cost_values)) {
svm_model <- svm(above_median ~ ., data = Auto, kernel = "linear", cost = cost_values[i],
cross = 10)
cv_errors[i] <- 1 - sum(predict(svm_model, Auto) == Auto$above_median) / nrow(Auto)
}
cv_results <- data.frame(Cost = cost_values, CV_Error = cv_errors)
Cost CV_Error
1
0.01000000
1
2
0.03162278
1
3
0.10000000
1
4
0.31622777
1
5
1.00000000
1
6
3.16227766
1
7 10.00000000
1
8 31.62277660
1
9 100.00000000
1
c)
optimal.cost <- 1
svm.fit <- svm(above_median ~ ., data = Auto, kernel = "linear", cost = optimal.cost)
svm.pred <- predict(svm.fit, Auto, type = "response")
table(svm.pred, Auto$above_median)
misclassification_error <- mean(svm.pred != Auto$above_median)
misclassification_error
[1] 1
d)plot(svm_fit, Auto, 1 ~ 2)
#6(a)
data('OJ')
inTrain <- sample(nrow(OJ), 800, replace = FALSE)
training <- OJ[inTrain,]
testing <- OJ[-inTrain,]
(b)
svm_linear <- svm(Purchase ~ ., data = training,
kernel = 'linear',
cost = 0.01)
summary(svm_linear)
> summary(svm_linear)
Call:
svm(formula = Purchase ~ ., data = training, kernel = "linear", cost = 0.01)
Parameters:
SVM-Type: C-classification
SVM-Kernel: linear
cost: 0.01
Number of Support Vectors: 436
( 218 218 )
Number of Classes: 2
Levels:
CH MM
©
Training error:
postResample(predict(svm_linear, training), training$Purchase)
Accuracy
Kappa
0.8337500 0.6438756
Testing error:
postResample(predict(svm_linear, testing), testing$Purchase)
Accuracy
Kappa
0.8148148 0.6047547
(d)
svm_linear_tune <- train(Purchase ~ ., data = training,
method = 'svmLinear2',
trControl = trainControl(method = 'cv', number = 10),
preProcess = c('center', 'scale'),
tuneGrid = expand.grid(cost = seq(0.01, 10, length.out = 20)))
svm_linear_tune
Support Vector Machines with Linear Kernel
800 samples
17 predictor
2 classes: 'CH', 'MM'
Pre-processing: centered (18), scaled (18)
Resampling: Cross-Validated (10 fold)
Summary of sample sizes: 720, 721, 720, 721, 719, 719, ...
Resampling results across tuning parameters:
cost
Accuracy
Kappa
0.0100000 0.8225820 0.6200799
0.5357895 0.8337861 0.6472752
1.0615789 0.8350361 0.6497972
1.5873684 0.8362861 0.6525803
2.1131579 0.8350828 0.6497845
2.6389474 0.8363328 0.6522645
3.1647368 0.8375674 0.6551204
3.6905263 0.8375674 0.6551204
4.2163158 0.8375674 0.6551204
4.7421053 0.8363174 0.6522544
5.2678947 0.8350828 0.6498325
5.7936842 0.8350828 0.6498325
6.3194737 0.8350828 0.6498325
6.8452632 0.8350828 0.6498325
7.3710526 0.8350828 0.6498325
7.8968421 0.8350828 0.6498325
8.4226316 0.8350828 0.6498325
8.9484211 0.8350828 0.6498325
9.4742105 0.8350828 0.6498325
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
10.0000000 0.8350828 0.6498325
Accuracy was used to select the optimal model using the largest value.
The final value used for the model was cost = 3.164737.
(e)
Training error:
> postResample(predict(svm_linear_tune, training), training$Purchase)
Accuracy
Kappa
0.8400000 0.6598391
Testing error:
> postResample(predict(svm_linear_tune, testing), testing$Purchase)
Accuracy
Kappa
0.8185185 0.6147574