Question: HW 1 : K - NN Implementation Requirements: Implement K nearest neighbors algorithm to predict the housing median value ( 2 ) ( 1 0
HW: KNN Implementation
Requirements: Implement K nearest neighbors algorithm to predict the housing median value
points
Create a function used to calculate the average price of kNNs in the training subset for any given testing sample and a given k
Test a sample from the testing subset and compare the kNN price k with the sample real price.
points
Create a function used to find root mean squarederror for all Xtest samples with given k #
points define a function which is used to find kNNs average pricethe formula to calculate the Scipy's Euclidean distance of sample and sample :def findkNNmeanpriceX y selected, k:
for any selected sample, find its KNN 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:
knns average price, floating point
A sample outputsample : the real price For a single sample, the kNN prediction:This step will take about minutes in my case
def rootmseX y Xtestk ytestk k:
For a given k collect their kNNaverage price, then find the rsme
Input:
Xtestcopy: dataframe of all testing samples
ytestcopy: 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
