Untitled
unknown
plain_text
8 days ago
1.4 kB
13
Indexable
from ipywidgets import interact, fixed import ipywidgets as widgets def plot_knn(k, points, classes): """ The function generates an interactive plot with a slider, allowing users to adjust the number of nearest neighbors (k-NN) used for classification. It classifies sample points based on the provided training points and their corresponding class labels. """ # Range of samples to take in the x and y direction N = 40 # These lists will be filled with all the sample points and their classifications sample_points = [] sample_classes = [] # Loop over all x and y coordinates for x in range(N): for y in range(N): # Construct the new sample point sample = [x, y] #use the knn function from previous assignment: to determine the classification of the sample point sample_class = knn(sample, points, classes, k) #append (add) the sample points + classification to the right lists. sample_points.append(sample) sample_classes.append(sample_class) plot_points(sample_points, sample_classes) # Enable the interactive plot with slider interact(plot_knn, k=widgets.IntSlider(value=1, min=1, max=11, step=2, continuous_update=False), points=fixed(training_points), classes=fixed(training_classes));
Editor is loading...
Leave a Comment