Question: Hi, I still need help for this same question. Previously I did asked this same question here. I did follow the code that I got
Hi, I still need help for this same question. Previously I did asked this same question here. I did follow the code that I got it here. But after my submission, I still got wrong. I don't know where is it wrong. Please help me again. The question is below: 1c) Using the feature found above (without normalizing), fit polynomial regression up to N=10 and report 2 R 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 x n . My answer as per the code that I got it from the expert here: from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures test_x = np.asanyarray(df[['weight']]) test_y = np.asanyarray(df[['mpg']]) # Initialize variables to keep track of best degree and best R^2 best_degree = 0 best_r_squared = 0 for i in range(1,11): poly=PolynomialFeatures(i) x_train_trans=poly.fit_transform(test_x) mod = LinearRegression() mod.fit(x_train_trans, test_y) y_pred = mod.predict(x_train_trans) r2 = r2_score(test_y, y_pred) print('Degree', i, '- Training r-squared:', r2) # Update best_degree and best_r_squared if current R^2 is higher if r2 > best_r_squared: best_degree = i best_r_squared = r2 # Return best_degree and best_r_squared print("best degree: ",best_degree) print("best r-squared: ",best_r_squared) My output: Degree 1 - Training r-squared: 0.6917929800341573 Degree 2 - Training r-squared: 0.714788125827216 Degree 3 - Training r-squared: 0.714788256948405 Degree 4 - Training r-squared: 0.7150370588261337 Degree 5 - Training r-squared: 0.7150074915331636 Degree 6 - Training r-squared: 0.7151614331852271 Degree 7 - Training r-squared: 0.715200518389913 Degree 8 - Training r-squared: 0.7144718552599327 Degree 9 - Training r-squared: 0.7120272860223944 Degree 10 - Training r-squared: 0.706835218293759 best degree: 7 best r-squared: 0.715200518389913 The results: AssertionError: Look at best_degree. I had already gave the answer best_degree=7 and best_rsquared=0.715200518389913, but still got the error. I don't know what the system want. Please help on this issue again and please guide me. Thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
