Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Modify the code have a second model using k neighbors regression with n neighbors value of 3. 2. Compare the results between the linear

1. Modify the code have a second model using k neighbors regression with n neighbors value of 3.

2. Compare the results between the linear regression and k neighbors regression. Explain why they are different.

3. Measure the time used in the following stages: 1. Loading the data, 2. Training the model, and 3. Making the prediction. How does these measurements differ for the linear regression vs. k neighbors regression?

---------Code is Below---------------

-----Part1------

def prepare_country_stats(oecd_bli, gdp_per_capita): oecd_bli = oecd_bli[oecd_bli["INEQUALITY"]=="TOT"] oecd_bli = oecd_bli.pivot(index="Country", columns="Indicator", values="Value") gdp_per_capita.rename(columns={"2015": "GDP per capita"}, inplace=True) gdp_per_capita.set_index("Country", inplace=True) full_country_stats = pd.merge(left=oecd_bli, right=gdp_per_capita, left_index=True, right_index=True) full_country_stats.sort_values(by="GDP per capita", inplace=True) remove_indices = [0, 1, 6, 8, 33, 34, 35] keep_indices = list(set(range(36)) - set(remove_indices)) return full_country_stats[["GDP per capita", 'Life satisfaction']].iloc[keep_indices]

-----Part2--------

import os

datapath = os.path.join("datasets", "lifesat", "")

-------Part3--------

# Code example import matplotlib import matplotlib.pyplot as plt import numpy as np import pandas as pd import sklearn.linear_model

# Load the data oecd_bli = pd.read_csv(datapath + "oecd_bli_2015.csv", thousands=',') gdp_per_capita = pd.read_csv(datapath + "gdp_per_capita.csv",thousands=',',delimiter='\t', encoding='latin1', na_values="n/a")

# Prepare the data country_stats = prepare_country_stats(oecd_bli, gdp_per_capita) X = np.c_[country_stats["GDP per capita"]] y = np.c_[country_stats["Life satisfaction"]]

# Visualize the data country_stats.plot(kind='scatter', x="GDP per capita", y='Life satisfaction') plt.show()

# Select a linear model model = sklearn.linear_model.LinearRegression()

# Train the model model.fit(X, y)

# Make a prediction for Cyprus X_new = [[22587]] # Cyprus' GDP per capita print(model.predict(X_new)) # outputs [[ 5.96242338]]

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_2

Step: 3

blur-text-image_3

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

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions

Question

What does the elimination process accomplish?

Answered: 1 week ago