Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

K-Nearest Neighbor in Python Hello, I need this done in Python (using SciKit-Lea

ID: 3804730 • Letter: K

Question

K-Nearest Neighbor in Python

Hello, I need this done in Python (using SciKit-Learn) using the attached CSV file. Compare Russia using the following data to the rest of the dataset. The questions are spelled out under the Russia dataset.

Afghanistan,59.61,23.21,74.30,4.44,0.40,1.5171
Haiti,45.00,47.67,73.10,0.09,3.40,1.7999
Nigeria,51.30,38.23,82.60,1.07,4.10,2.4493
Egypt,70.48,26.58,19.60,1.86,5.30,2.8622
Argentina,75.77,32.30,13.30,0.76,10.10,2.9961
China,74.87,29.98,13.70,1.95,6.40,3.6356
Brazil,73.12,42.93,14.50,1.43,7.20,3.7741
Israel,81.30,28.80,3.60,6.77,12.50,5.8069
U.S.A,78.51,29.85,6.30,4.72,13.70,7.1357
Ireland,80.15,27.23,3.50,0.60,11.50,7.5360
U.K.,80.09,28.49,4.40,2.59,13.00,7.7751
Germany,80.24,22.07,3.50,1.31,12.00,8.0461
Canada,80.99,24.79,4.90,1.42,14.20,8.6725
Australia,82.09,25.40,4.20,1.86,11.50,8.8442
Sweden,81.43,22.18,2.40,1.27,12.80,9.2985
NewZealand,80.67,27.81,4.90,1.13,12.30,9.4627

Above find the CSV file that must be used. Output for the first two parts should look like the below picture:

I need all four parts to work. (But I will settle if you get the first two parts to work)

use Russia as our query country for this question. The table below lists the de We will tive features for Russia. COUNTRY LIFE TOP-10 INFANT MIL SCHOOL PI ID EXP. INCOME MORT SPEND YEARS Russia 67.62 31.68 10.00 3.87 12.90 1. What value would a nearest neighbor prediction model using Euclidean distance return for the CPI of Russia? 2. What value would a weighted k-NN prediction model return for the CPI of Russia? Use k 16 (ie., the full dataset) and a weighting scheme of the reciprocal of the squared Euclidean distance between the neighbor and the query. 3. The descriptive features in this dataset are of different types. For example, some are percentages, others are measured in years, and others are measured in counts per 1,000. We should always consider normalizing our data, but it is particularly important to do this when the descriptive features are measured in different units. What value would a 3-nearest neighbor prediction model using Euclidean distance return for the CPI of Russia when the descriptive features have been normalized using range normalization? (Hint: The normalized query is given as follows: Russia', 0.6099, 0.3754, 0.0948, 0.5658, 0.9058 4. What value would a weighted k-NN prediction modelwith k 16 (i.e., the full dataset) and using a weighting scheme of the reciprocal of the squared Euclidean distance between the neighbor and the queryreturn for the CPI of Russia when it is applied to the range-normalized data?

Explanation / Answer

Answer:

1.

----------------------------------------

import pandas
from sklearn.neighbors import KNeighborsRegressor
#from sklearn.neighbors import NearestNeighbors

print("Reading dataset...")
dataset=pandas.read_csv('data.csv')
print("Dataset reading complete!")

#print dataset
#print(dataset)

#training data vector
train_x=dataset[["Life_Exp","Top_10_Income","Infant_Mort","Mil_Spend","School_Yrs"]]
train_y=dataset[["CPI"]]

#number of neighbors to consider
nbrs=3

#perform k-nearest neighbor classification
knn = KNeighborsRegressor(algorithm='auto', leaf_size=30, metric='minkowski',
           metric_params=None, n_jobs=1, n_neighbors=nbrs, p=2,
          weights='uniform')
model=knn.fit(train_x,train_y)
target=["Russia",67.62,31.68,10.00,3.87,12.90]

#predicted value
predicted_value=knn.predict(target[1:6])

print("CPI for",nbrs,"-NN",round(predicted_value[0][0],4))

---------------------------------------

Output:

---------------------------------------

Reading dataset...
Dataset reading complete!
CPI for 3 -NN 4.5891

-------------------------