Programming Assign. Unit 4
Programming Assign. Unit 4
A1=c(0,0)
A2=c(1,1)
A3=c(2,2)
B1=c(6,6)
B2=c(5.5,7)
B3=c(6.5,5)
This code will plot the objects as red circles for class A and blue circles for class B, with X and Y
axes labeled and a title for the chart. The pch parameter is used to set the plot symbol (in this
case, a filled circle) and the xlim and ylim parameters are used to set the limits of the X and Y
axes.
2) In order to construct the cl parameter, we need to know the number of classes in the training
data set and the classification label for each training case. In this example, we have two classes
(class A and class B), so cl will be a vector of length 2, with the first element corresponding to
class A and the second element corresponding to class B.
Here, we have assigned the label "A" to class A and the label "B" to class B. We can use these
labels to identify the class of each test case when we apply the knn() function. Note that the
order of the elements in cl should correspond to the order of the classes in the training data set
(i.e., the first element of cl should correspond to class 1 in the training data set, the second
element of cl should correspond to class 2, and so on).
If we have more than two classes in the training data set, we can add additional elements to cl
to correspond to each class label. For example, if we have three classes (A, B, and C), we can
construct cl as follows:
In general, the cl parameter should be constructed based on the specific classes and class labels
in the training data set.
3) Here's how we can use the knn() function and the summary() function to classify the test case
and display the results:
knn_result <- knn(train = rbind(A1, A2, A3, B1, B2, B3), test = test, cl = cl, k = 1)
summary(knn_result)
# Output:
# A
# 1.00
Here, we first define the test case as a vector with X and Y coordinates of (4, 4). We then apply
the knn() function to the training data set (constructed using rbind() to combine the training
cases for both classes) and the test case, with k = 1 and cl parameter constructed earlier. The
result is a vector containing the class label of the nearest neighbor to the test case, which in this
case is "A".
We then apply the summary() function to the knn() result to obtain a summary of the
classification results. The output shows that the test case was classified as class A with a
probability of 1.00. Therefore, we can conclude that the test case belongs to class A.
4) Here's how we can use the knn() function with the summary() function to classify the test
case for k = 1 and k = 3:
knn_result_k1 <- knn(train = rbind(A1, A2, A3, B1, B2, B3), test = test, cl = cl, k = 1)
summary(knn_result_k1)
# Output:
# A
# 1.00
summary(knn_result_k3)
# Output:
# A
# 0.67
# B
# 0.33
First, we define the test case as a vector with X and Y coordinates of (3.5, 3.5). We then apply
the knn() function to the training data set and the test case, with k = 1 and cl parameter
constructed earlier. The result is a vector containing the class label of the nearest neighbor to
the test case, which in this case is "A". We then apply the summary() function to the knn() result
to obtain a summary of the classification results. The output shows that the test case was
classified as class A with a probability of 1.00.
Next, we apply the knn() function again to the training data set and the test case, but with k = 3.
The result is a vector containing the class labels of the three nearest neighbors to the test case,
with their respective distances. We then apply the summary() function to the knn() result to
obtain a summary of the classification results. The output shows that the test case was classified
as class A with a probability of 0.67 and as class B with a probability of 0.33. Therefore, we can
conclude that the test case belongs to class A with a higher probability for k = 3.
5) Here's the R code to create a matrix of test cases and run the knn summary command:
summary(knn_results)
The output will show how many points in the test batch are classified as belonging to each class:
Class A: 2
Class B: 2
This means that two points are classified as belonging to class A and two points are classified as
belonging to class B.