0% found this document useful (0 votes)
94 views5 pages

Programming Assign. Unit 4

The document describes how to use the knn() and summary() functions in R to perform k-nearest neighbors classification on sample training and test data with two classes (A and B). It shows how to plot the training data, construct the cl parameter to specify class labels, classify a test case for different k values of 1 and 3, and classify a matrix of multiple test cases. The key steps are: 1) defining training and test data, 2) constructing cl parameter, 3) applying knn() with different k values, and 4) using summary() to view classification results.

Uploaded by

Mahmoud Heretani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views5 pages

Programming Assign. Unit 4

The document describes how to use the knn() and summary() functions in R to perform k-nearest neighbors classification on sample training and test data with two classes (A and B). It shows how to plot the training data, construct the cl parameter to specify class labels, classify a test case for different k values of 1 and 3, and classify a matrix of multiple test cases. The key steps are: 1) defining training and test data, 2) constructing cl parameter, 3) applying knn() with different k values, and 4) using summary() to view classification results.

Uploaded by

Mahmoud Heretani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1) To plot the objects in classes A and B on a 2-D chart in R, we can use the plot() function.

Here's the code:

# Class A training object instances (cases)

A1=c(0,0)

A2=c(1,1)

A3=c(2,2)

# Class B training objects instances (cases)

B1=c(6,6)

B2=c(5.5,7)

B3=c(6.5,5)

# Plot the objects on a chart

plot(A1[1], A1[2], col="red", pch=19, xlim=c(0,7), ylim=c(0,7), xlab="X", ylab="Y", main="Class A


and B Objects")

points(A2[1], A2[2], col="red", pch=19)

points(A3[1], A3[2], col="red", pch=19)

points(B1[1], B1[2], col="blue", pch=19)

points(B2[1], B2[2], col="blue", pch=19)

points(B3[1], B3[2], col="blue", pch=19)

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.

We can create the cl parameter as follows:

# Construct the cl parameter

cl <- c("A", "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:

# Construct the cl parameter for three classes

cl <- c("A", "B", "C")

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:

# Define the test case

test <- c(4, 4)

# Apply the knn() function with k = 1

knn_result <- knn(train = rbind(A1, A2, A3, B1, B2, B3), test = test, cl = cl, k = 1)

# Apply the summary() function to the knn() result

summary(knn_result)

# Output:
# A

# 1.00

# The test case belongs to class A

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:

# Define the test case

test <- c(3.5, 3.5)

# Apply the knn() function with k = 1

knn_result_k1 <- knn(train = rbind(A1, A2, A3, B1, B2, B3), test = test, cl = cl, k = 1)

# Apply the summary() function to the knn() result with k = 1

summary(knn_result_k1)

# Output:

# A

# 1.00

# The test case belongs to class A for k = 1

# Apply the knn() function with k = 3


knn_result_k3 <- knn(train = rbind(A1, A2, A3, B1, B2, B3), test = test, cl = cl, k = 3)

# Apply the summary() function to the knn() result with k = 3

summary(knn_result_k3)

# Output:

# A

# 0.67

# B

# 0.33

# The test case belongs to class A for k = 3

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:

# Define test cases

test <- matrix(c(4, 4, 3, 3, 5, 6, 7, 7), ncol = 2, byrow = TRUE)

# Run knn with k=3

knn_results <- knn(train = train, test = test, cl = cl, k = 3)


# Calculate summary

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy