Question: HW 1 : K - NN Implementation Requirements: Implement K nearest neighbors algorithm to predict the housing median value ( 2 ) ( 1 0
HW
: K
NN Implementation
Requirements: Implement K nearest neighbors algorithm to predict the housing median value
points
Create a function used to calculate the average price of k
NNs in the training subset for any given testing sample and a given k
Test a sample from the testing subset and compare the k
NN price
k
with the sample real price.
points
Create a function used to find root mean squared
error for all
X
test samples with given k
#
points define a function which is used to find k
NN
s average pricethe formula to calculate the Scipy's Euclidean distance of sample
and sample
:def find
kNNmean
price
X
y
selected k
:
for any selected sample, find its K
NN neighbors' average price
input:
X: the training dataframe,
y: the training data price,
selected: any row of the testing dataframe,
k: the number of neighbors in the training dataframe. k is int and larger than
return:
k
nn
s average price, floating point
A sample outputsample
: the real price
For a single sample, the k
NN prediction:This step will take about
minutes in my case
def root
mse
X
y
X
test
k
y
test
k
k
:
For a given k
collect their kNN
average price, then find the rsme
Input:
X
test
copy: dataframe of all testing samples
y
test
copy: data series of all testing sample prices
k: the number of neighbors, int.
Return:
rmse: the root mean square erros, floating point type
#k
and rmse
for k
the rmse
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
