0% found this document useful (0 votes)
126 views6 pages

K-nearest Neighbors with Iris Dataset

The K-nearest neighbors algorithm (KNN) is an simple supervised machine learning algorithm that can be used for both classification and regression problems. During training, KNN does not actually learn from the training data - all computation is done during prediction to find the K closest training examples to the new data point. The label or value for the new data is determined based on the labels of its K nearest neighbors, for example by majority vote. Weighing the contributions of neighbors differently based on distance can improve accuracy compared to treating all neighbors equally. On the Iris flower dataset, a KNN classifier with distance weighting achieved 100% accuracy on test data.

Uploaded by

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

K-nearest Neighbors with Iris Dataset

The K-nearest neighbors algorithm (KNN) is an simple supervised machine learning algorithm that can be used for both classification and regression problems. During training, KNN does not actually learn from the training data - all computation is done during prediction to find the K closest training examples to the new data point. The label or value for the new data is determined based on the labels of its K nearest neighbors, for example by majority vote. Weighing the contributions of neighbors differently based on distance can improve accuracy compared to treating all neighbors equally. On the Iris flower dataset, a KNN classifier with distance weighting achieved 100% accuracy on test data.

Uploaded by

Bao Trung Thai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Thuật toán K-nearest neighbor

K-nearest neighbor là một trong những thuật toán supervised-learning đơn giản nhất (mà hiệu
quả trong một vài trường hợp) trong Machine Learning. Khi training, thuật toán này không
học một điều gì từ dữ liệu training (đây cũng là lý do thuật toán này được xếp vào loại lazy
learning), mọi tính toán được thực hiện khi nó cần dự đoán kết quả của dữ liệu mới. K-nearest
neighbor có thể áp dụng được vào cả hai loại của bài toán Supervised learning
là Classification và Regression. KNN còn được gọi là một thuật toán Instance-based hay
Memory-based learning.
Với KNN, trong bài toán Classification, label của một điểm dữ liệu mới (hay kết quả của câu hỏi
trong bài thi) được suy ra trực tiếp từ K điểm dữ liệu gần nhất trong training set. Label của một
test data có thể được quyết định bằng major voting (bầu chọn theo số phiếu) giữa các điểm gần
nhất, hoặc nó có thể được suy ra bằng cách đánh trọng số khác nhau cho mỗi trong các điểm
gần nhất đó rồi suy ra label.
Một cách ngắn gọn, KNN là thuật toán đi tìm đầu ra của một điểm dữ liệu mới bằng
cách chỉ dựa trên thông tin của K điểm dữ liệu trong training set gần nó nhất (K-lân cận), không
quan tâm đến việc có một vài điểm dữ liệu trong những điểm gần nhất này là nhiễu.

Ví dụ trên Python

Bộ cơ sở dữ liệu Iris (Iris flower dataset).


Iris flower dataset là một bộ dữ liệu nhỏ (nhỏ hơn rất nhiều so với MNIST. Bộ dữ liệu
này bao gồm thông tin của ba loại hoa Iris (một loài hoa lan) khác nhau: Iris setosa, Iris
virginica và Iris versicolor. Mỗi loại có 50 bông hoa được đo với dữ liệu là 4 thông tin:
chiều dài, chiều rộng đài hoa (sepal), và chiều dài, chiều rộng cánh hoa (petal). Dưới
đây là ví dụ về hình ảnh của ba loại hoa. (Chú ý, đây không phải là bộ cơ sở dữ liệu
ảnh như MNIST, mỗi điểm dữ liệu trong tập này chỉ là một vector 4 chiều).
Thí nghiệm
Trong phần này, chúng ta sẽ tách 150 dữ liệu trong Iris flower dataset ra thành 2 phần,
gọi là training set và test set. Thuật toán KNN sẽ dựa vào trông tin ở training set để dự
đoán xem mỗi dữ liệu trong test set tương ứng với loại hoa nào. Dữ liệu được dự đoán
này sẽ được đối chiếu với loại hoa thật của mỗi dữ liệu trong test set để đánh giá hiệu
quả của KNN.

import numpy as np

import [Link] as plt

from sklearn import neighbors, datasets

Tiếp theo, chúng ta load dữ liệu và hiện thị vài dữ liệu mẫu. Các class được gán nhãn là 0,
1, và 2.

iris = datasets.load_iris()

iris_X = [Link]

iris_y = [Link]

print 'Number of classes: %d' %len([Link](iris_y))

print 'Number of data points: %d' %len(iris_y)

X0 = iris_X[iris_y == 0,:]

print '\nSamples from class 0:\n', X0[:5,:]

X1 = iris_X[iris_y == 1,:]

print '\nSamples from class 1:\n', X1[:5,:]

X2 = iris_X[iris_y == 2,:]

print '\nSamples from class 2:\n', X2[:5,:]

Number of classes: 3

Number of data points: 150

Samples from class 0:


[[ 5.1 3.5 1.4 0.2]

[ 4.9 3. 1.4 0.2]

[ 4.7 3.2 1.3 0.2]

[ 4.6 3.1 1.5 0.2]

[ 5. 3.6 1.4 0.2]]

Samples from class 1:

[[ 7. 3.2 4.7 1.4]

[ 6.4 3.2 4.5 1.5]

[ 6.9 3.1 4.9 1.5]

[ 5.5 2.3 4. 1.3]

[ 6.5 2.8 4.6 1.5]]

Samples from class 2:

[[ 6.3 3.3 6. 2.5]

[ 5.8 2.7 5.1 1.9]

[ 7.1 3. 5.9 2.1]

[ 6.3 2.9 5.6 1.8]

[ 6.5 3. 5.8 2.2]]

Nếu nhìn vào vài dữ liệu mẫu, chúng ta thấy rằng hai cột cuối mang khá nhiều thông tin giúp
chúng ta có thể phân biệt được chúng. Chúng ta dự đoán rằng kết quả classification cho cơ sở
dữ liệu này sẽ tương đối cao.

Tách training và test sets


Giả sử chúng ta muốn dùng 50 điểm dữ liệu cho test set, 100 điểm còn lại cho training
set. Scikit-learn có một hàm số cho phép chúng ta ngẫu nhiên lựa chọn các điểm này,
như sau:

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(

iris_X, iris_y, test_size=50)


print "Training size: %d" %len(y_train)

print "Test size : %d" %len(y_test)

Training size: 100

Test size : 50

Sau đây, tôi trước hết xét trường hợp đơn giản K = 1, tức là với mỗi điểm test data, ta chỉ xét 1
điểm training data gần nhất và lấy label của điểm đó để dự đoán cho điểm test này.

clf = [Link](n_neighbors = 1, p = 2)

[Link](X_train, y_train)

y_pred = [Link](X_test)

print "Print results for 20 test data points:"

print "Predicted labels: ", y_pred[20:40]

print "Ground truth : ", y_test[20:40]

Print results for first 20 test data points:

Predicted labels: [2 1 2 2 1 2 2 0 2 0 2 0 1 0 0 2 2 0 2 0]

Ground truth : [2 1 2 2 1 2 2 0 2 0 1 0 1 0 0 2 1 0 2 0]

Kết quả cho thấy label dự đoán gần giống với label thật của test data, chỉ có 2 điểm trong số 20
điểm được hiển thị có kết quả sai lệch. Ở đây chúng ta làm quen với khái niệm mới: ground
truth. Một cách đơn giản, ground truth chính là nhãn/label/đầu ra thực sự của các điểm trong
test data.

Phương pháp đánh giá (evaluation method)


Để đánh giá độ chính xác của thuật toán KNN classifier này, chúng ta xem xem có bao nhiêu
điểm trong test data được dự đoán đúng. Lấy số lượng này chia cho tổng số lượng trong tập
test data sẽ ra độ chính xác. Scikit-learn cung cấp hàm số accuracy_score để thực hiện công
việc này.

from [Link] import accuracy_score

print "Accuracy of 1NN: %.2f %%" %(100*accuracy_score(y_test, y_pred))


Accuracy of 1NN: 94.00 %

Với khoảng cách ở được tính là khoảng cách theo norm 2.

clf = [Link](n_neighbors = 10, p = 2)

[Link](X_train, y_train)

y_pred = [Link](X_test)

print "Accuracy of 10NN with major voting: %.2f %%" %(100*accuracy_score(y_test,


y_pred))

Accuracy of 10NN with major voting: 98.00 %

Đánh trọng số cho các điểm lân cận


Trong kỹ thuật major voting bên trên, mỗi trong 10 điểm gần nhất được coi là có vai trò như
nhau và giá trị lá phiếu của mỗi điểm này là như nhau. Tôi cho rằng như thế là không công
bằng, vì rõ ràng rằng những điểm gần hơn nên có trọng số cao hơn (càng thân cận thì càng tin
tưởng). Vậy nên tôi sẽ đánh trọng số khác nhau cho mỗi trong 10 điểm gần nhất này. Cách
đánh trọng số phải thoải mãn điều kiện là một điểm càng gần điểm test data thì phải được đánh
trọng số càng cao (tin tưởng hơn). Cách đơn giản nhất là lấy nghịch đảo của khoảng cách này.
Scikit-learn giúp chúng ta đơn giản hóa việc này bằng cách gán gía trị weights = 'distance'

clf = [Link](n_neighbors = 10, p = 2, weights = 'distance')

[Link](X_train, y_train)

y_pred = [Link](X_test)

print "Accuracy of 10NN (1/distance weights): %.2f %%" %(100*accuracy_score(y_test,


y_pred))

Accuracy of 10NN (1/distance weights): 100.00 %


Ngoài 2 phương pháp đánh trọng số weights = 'uniform' và weights = 'distance' ở trên,
scikit-learn còn cung cấp cho chúng ta một cách để đánh trọng số một cách tùy chọn. Ví dụ,
một cách đánh trọng số phổ biến khác trong Machine Learning là:

def myweight(distances):

sigma2 = .5 # we can change this number

return [Link](-distances**2/sigma2)

clf = [Link](n_neighbors = 10, p = 2, weights = myweight)

[Link](X_train, y_train)

y_pred = [Link](X_test)

print "Accuracy of 10NN (customized weights): %.2f %%" %(100*accuracy_score(y_test,


y_pred))

Accuracy of 10NN (customized weights): 98.00 %

Common questions

Powered by AI

To adapt the KNN algorithm for high-dimensional data, one can apply dimensionality reduction techniques like PCA to reduce data dimensionality, thereby avoiding the curse of dimensionality. High-dimensional data often results in sparse neighbor relationships, making distance measures less meaningful. This challenges KNN's effectiveness, as greater dimensions can dilute the proximity measure between data points, which KNN relies on for classification and regression .

Distance metrics significantly impact KNN model performance by determining how proximity is measured, affecting neighbor selection. The model typically uses Euclidean distance, but modifying distance calculation, for instance to Manhattan or Minkowski distance, can alter results. Different metrics are implemented by setting the `p` parameter in scikit-learn's KNeighborsClassifier, where `p = 2` defaults to Euclidean. Changing this parameter tailors KNN to different data distributions, potentially enhancing performance for certain types of data .

'Ground truth' in machine learning refers to the actual labels or outputs of test data. It serves as a benchmark to evaluate the performance of a predictive model like KNN. By comparing the predicted labels against the ground truth, one can compute metrics like accuracy to objectively assess how well the model is performing. This comparison is essential for any evaluation method and is used in calculating model accuracy with functions such as accuracy_score in scikit-learn .

The Iris dataset contains 150 data points, each described by four features of an Iris flower: sepal length, sepal width, petal length, and petal width. The data points are equally divided into three classes corresponding to different species. For training and testing in the KNN algorithm, the dataset is split into a training set with 100 data points and a test set with 50 data points. The KNN model is trained on the training set and predicts the species of flowers in the test set, evaluating accuracy by comparing predictions to true labels .

The KNN algorithm determines the label of a new data point by identifying the K data points in the training set that are closest to the new data point. It then assigns the label based on a majority voting system, where the most common label among these K neighbors is chosen. Alternatively, KNN can apply different weights to the neighbors, for instance by proximity, where closer neighbors have higher influence on the label assignment .

Splitting the dataset into training and test sets is crucial in the KNN algorithm for evaluating its generalization to unseen data. Without a test set, performance metrics would only reflect the model's ability to memorize the training data. Programmatically, this is accomplished using tools like scikit-learn's `train_test_split` function, which allows for random selection of test data, ensuring the model's assessment on a varied and independent validation set .

When using KNN with K = 1 on the Iris dataset, the algorithm achieved a classification accuracy of 94%. When using K = 10 with majority voting, the accuracy improved to 98%; further using distance-based weighting, the accuracy reached 100%. These results indicate that increasing K and adjusting weights can improve model performance, as it allows better handling of noise and variance by considering more context in decision-making .

Weighted voting can be more effective than majority voting in the KNN algorithm because it accounts for the relative proximity of neighbors. Closer neighbors, which are expected to be more similar to the test point, are given more influence. This is implemented by assigning weights inversely proportional to distance or using a custom function like Gaussian weights to compute them. For instance, using weights='distance', or a custom function in scikit-learn, allows this method to enhance classification accuracy .

The computational complexity of the KNN algorithm arises from its lazy learning nature, as it postpones all calculations until prediction, making it computationally expensive at runtime. KNN requires calculating the distance between the query point and all points in the training set for each prediction, resulting in a time complexity of O(n * d) per query, where n is the number of training samples and d is the dimensionality. This makes scaling to larger datasets or dimensions challenging, necessitating strategies like dimensionality reduction or implementing efficient data structures, such as KD-trees, to speed up the search .

The 'lazy learning' characteristic of the K-nearest neighbor (KNN) algorithm refers to its lack of training phase where the model does not learn any abstraction from the training data. Instead, all computations are deferred until the prediction phase, when it evaluates new data points based on the entire training dataset. This is why KNN is also known as an instance-based or memory-based learning method, as it makes predictions using stored instances rather than a developed model .

You might also like