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

In your own words, please explain the Conflict Perspective. Provide an example. In your own words,...
In your own words, please explain the Conflict Perspective. Provide an example. In your own words, please explain the Interactionist Perspective. Provide an example.
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...
10. Provide an example from your own experience (your own behavior) of the Premack Principle in...
10. Provide an example from your own experience (your own behavior) of the Premack Principle in two-three sentences. Answer: 11. How would you establish a motivating operation before training your dog to “give paw”? Answer: 12. How do you think you might have “shaped” someone’s behavior toward you? Explanation shouldn’t be longer than one paragraph. Answer: 13. Provide an example of a competing contingency from your own personal experience (behavior). Answer: 14. Jonah has listened to his new CD over...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT