Answered step by step
Verified Expert Solution
Link Copied!

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.

image text in transcribedimage text in transcribed

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 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions

Question

=+Perform the following computations:

Answered: 1 week ago

Question

2. Discuss the importance of popular culture as a public forum.

Answered: 1 week ago

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago