Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I need help for the below question. I have written two difference of code for two times submission, but still get wrong. Question: 1c)

Hi, I need help for the below question. I have written two difference of code for two times submission, but still get wrong.

Question:

1c) Using the feature found above (without normalizing), fit polynomial regression up to N=10 and report 2. Which polynomial degree gives the best result? [10 pts]

Hint: For N-degree polynomial fit, you may have to include all orders upto N. Use a for loop instead of running it manually. The statsmodels.formula.api formula string can understand np.power(x,n) function to include a feature representing .

my first submission code:

test_x = np.asanyarray(df[['weight']])

test_y = np.asanyarray(df[['mpg']])

test_y_ = regr.predict(test_x)

for i in range(1,11):

poly=PolynomialFeatures(i)

x_train_trans=poly.fit_transform(train_x)

#x_test_trans=poly.transform(X_test)

mod = LinearRegression()

mod.fit(x_train_trans, train_y)

y_pred = mod.predict(x_train_trans)

print('Degree', i, '- Training r-squared:', r2_score(train_y, y_pred))

My second time submission of code:

formula = 'train_y ~ ' + ' + '.join(['np.power(train_x, {})'.format(j) for j in range(1,i+1)])

model = smf.ols(formula, data=df).fit()

# Append the R2 value to the list

r2_values.append(model.rsquared)

print('Degree', i, '- Training r-squared:', r2_score(train_y, y_pred))

The output of either of the above code:

Degree 1 - Training r-squared: 0.6917929800341573

Degree 2 - Training r-squared: 0.7147881258272161

Degree 3 - Training r-squared: 0.7147882569484048

Degree 4 - Training r-squared: 0.7150370583628519

Degree 5 - Training r-squared: 0.7150074915332407

Degree 6 - Training r-squared: 0.7151614331852089

Degree 7 - Training r-squared: 0.7152005183899042

Degree 8 - Training r-squared: 0.7144718552609842

Degree 9 - Training r-squared: 0.7120272860256083

Degree 10 - Training r-squared: 0.7068352182937898

My answer is:

# return updated best_degree and best_r_squared

best_degree = 7

best_r_squared = 0.715200518389913

After submission stated that:

AssertionError: Look at best_degree.

Please guide me through this. Thank you.

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

T Sql Window Functions For Data Analysis And Beyond

Authors: Itzik Ben Gan

2nd Edition

0135861446, 978-0135861448

More Books

Students also viewed these Databases questions