Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help writing a K-Nearest Neighbor algorithm from scratch in python. Here are the functions that the code must implement, and it also should
I need help writing a K-Nearest Neighbor algorithm from scratch in python.
Here are the functions that the code must implement, and it also should use euclidean distance to decide closest neighbors.
class KNNClassifier (BaseEstimator,ClassifierMixin): def init (self, columntype=[], weight_type='inverse_distance'): ## add parameters here Args: columntype for each column tells you if continues[real] or if nominal[categoritcal]. weight_type: inverse_distance voting or if non distance weighting. Options ["no_weight", "inverse_distance" ] = self.columntype self.weight_type columntype #Note This won't be needed until part 5 weight_type II II II def fit(self, data, labels): Fit the data; run the algorithm (for this lab really just saves the data :D) Args : X (array-like): A 2D numpy array with the training data, excluding targets y (array-like): A 2D numpy array with the training targets Returns: self: this allows this to be chained, e.g. model.fit(x,y).predict(x_test) return self def predict(self, data): """ Predict all classes for a dataset X Args: X (array-like): A 2D numpy array with the training data, excluding targets Returns: array, shape (n_samples, ) Predicted target values per element in X. II II II pass #Returns the Mean score given input data and labels def score(self, x, y): Return accuracy of model on a given dataset. Must implement own score function. Args : X (array-like): A 2D numpy array with data, excluding targets y (array-like): A 2D numpy array with targets Returns: score : float Mean accuracy of self.predict(X) wrt. y. return 0 class KNNClassifier (BaseEstimator,ClassifierMixin): def init (self, columntype=[], weight_type='inverse_distance'): ## add parameters here Args: columntype for each column tells you if continues[real] or if nominal[categoritcal]. weight_type: inverse_distance voting or if non distance weighting. Options ["no_weight", "inverse_distance" ] = self.columntype self.weight_type columntype #Note This won't be needed until part 5 weight_type II II II def fit(self, data, labels): Fit the data; run the algorithm (for this lab really just saves the data :D) Args : X (array-like): A 2D numpy array with the training data, excluding targets y (array-like): A 2D numpy array with the training targets Returns: self: this allows this to be chained, e.g. model.fit(x,y).predict(x_test) return self def predict(self, data): """ Predict all classes for a dataset X Args: X (array-like): A 2D numpy array with the training data, excluding targets Returns: array, shape (n_samples, ) Predicted target values per element in X. II II II pass #Returns the Mean score given input data and labels def score(self, x, y): Return accuracy of model on a given dataset. Must implement own score function. Args : X (array-like): A 2D numpy array with data, excluding targets y (array-like): A 2D numpy array with targets Returns: score : float Mean accuracy of self.predict(X) wrt. y. return 0Step 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