Question
This is the Source Code of K Nearest Neighbour Algorithm In this Code, Graph values have been declared fixed using Dictionary in Python , the
This is the Source Code of K Nearest Neighbour Algorithm
In this Code, Graph values have been declared fixed using Dictionary in Python, the value of K and New Test Point are taken from User Input. And Output will be shown as Graphical Representation.
Modify: I want to take the Graph Values from User Input by Separate Two Classes.
Question: Modify this Program to Take Graph Values from User Input.
Modification Line (I want to take these values from User Input):
dataset = {'k': [[5, 10], [10, 20], [25, 10]], 'r': [[55, 50], [65, 45], [95, 90]]}
Full Source Code:
import matplotlib.pyplot as plt from matplotlib import style import warnings from math import sqrt from collections import Counter style.use('fivethirtyeight') def k_nearest(data, find_feature, k_value): if len(data) >= k_value: warnings.warn('k is set to a value less than the group of data type') distances = [] for group in data: for features in data[group]: euclidean_distance = sqrt((features[0] - find_feature[0]) ** 2 + (features[1] - find_feature[1]) ** 2) distances.append([euclidean_distance], group) votes = [i[1] for i in sorted(distances)[:k_value]] vote_result = Counter(votes).most_common(1)[0][0] return vote_result dataset = {'k': [[5, 10], [10, 20], [25, 10]], 'r': [[55, 50], [65, 45], [95, 90]]} test_point = [] for i in range(0, 2): ele = int(input("Enter the Testing Point: ")) test_point.append(ele) new_value = test_point k = int(input("Enter the value of K: ")) prediction = k_nearest(dataset, new_value, k) [[plt.scatter(ii[0], ii[1], s=100, color=i) for ii in dataset[i]] for i in dataset] plt.scatter(new_value[0], new_value[1], s=150, marker='+', color=prediction) print(prediction) plt.show()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started