Question

In: Computer Science

Please provide your own example of KNN algorithm

Please provide your own example of KNN algorithm

Solutions

Expert Solution

KNN algorithm example:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

# Assign colum names to the dataset
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'Class']

# Read dataset to pandas dataframe
dataset = pd.read_csv(url, names=names)

dataset.head()

X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 4].values

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)


from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)

X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)


from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors=5)
classifier.fit(X_train, y_train)



y_pred = classifier.predict(X_test)


#evaluation

from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))

Output: 
[[11  0  0]
   0 13  0]
   0  1  6]]
                 precision   recall   f1-score   support

    Iris-setosa       1.00     1.00       1.00        11
Iris-versicolor       1.00     1.00       1.00        13
 Iris-virginica       1.00     1.00       1.00         6

    avg / total       1.00     1.00       1.00        30

Related Solutions

Please find implementation of KNN algorithm (in C++) below Please explain how each code implements the...
Please find implementation of KNN algorithm (in C++) below Please explain how each code implements the following steps of the algorithm in question: 1. Determine parameter K = number of nearest neighbors 2. Calculate the distance between the query-instance and all the training samples 3. Sort the distance and determine nearest neighbors based on the K-th minimumdistance 4. Gather the category Y of the nearest neighbors 5. Use simple majority of the category of nearest neighbors as the prediction value...
Can someone please provide an example of Apple's principles of ethic(in your own words)?
Can someone please provide an example of Apple's principles of ethic(in your own words)?
Can someone please provide an example of Apple's decision-making(in your own words)?
Can someone please provide an example of Apple's decision-making(in your own words)?
Can someone please provide an example of Apple's strategic management plan(in your own words)?
Can someone please provide an example of Apple's strategic management plan(in your own words)?
* Please use your own words. * Please provide sources for your answers. 1. Provide an...
* Please use your own words. * Please provide sources for your answers. 1. Provide an example of any two leading companies from the same industry which are competing directly for marketshare. Give a short profile (300-500 words) for each (provide references for your answers). 2. If you are the manager of one of these companies, what pricing policy do you adopt to be in the first position? Why? (100-200 words) 3. When the whole sector of the market is...
Provide in your own words one example of the concept of counterculture in Canada and your...
Provide in your own words one example of the concept of counterculture in Canada and your own country, from personal experience or observations in the media, books or movies. Be sure that your examples prove clearly you understand the definition.
Provide your own example of a situation where it would be appropriate to test for an...
Provide your own example of a situation where it would be appropriate to test for an interaction using a two-way independent ANOVA?
1. Explain in your own words how the KNN Classifier works. 2. Why is overfitting a...
1. Explain in your own words how the KNN Classifier works. 2. Why is overfitting a problem? Explain how can we overcome it using a validation set.
Explain in your own words, emotional intelligence and provide an example of how this concept is...
Explain in your own words, emotional intelligence and provide an example of how this concept is relevant to job performance and organizational commitment. (500 wordsor less)
In your own words, what is consideration? Provide an example (real life or hypothetical, but not...
In your own words, what is consideration? Provide an example (real life or hypothetical, but not a published online case) of adequate consideration OR lack of consideration. Explain the circumstances surrounding your example - e.g. what did each person say, etc. Then explain how the facts you detail constitute adequate consideration or lack of consideration. For example: My two year-old is precariously holding his Winnie the Pooh cup (with no lid) filled with milk. He's standing on the carpet with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT