Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Evaluate the Model | Polynomial Regression
Linear Regression with Python

book
Evaluate the Model

In this challenge, you are given the good old housing dataset, but this time only with the 'age' feature.

import pandas as pd

df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/houses_poly.csv')
print(df.head())
1234
import pandas as pd df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/houses_poly.csv') print(df.head())
copy

Let's build a scatterplot of this data.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/houses_poly.csv')
X = df['age']
y = df['price']
plt.scatter(X, y, alpha=0.4)
plt.show()
12345678
import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/houses_poly.csv') X = df['age'] y = df['price'] plt.scatter(X, y, alpha=0.4) plt.show()
copy

Fitting a straight line to this data may not be a great choice. The price gets higher for either brand-new or really old houses. Fitting a parabola looks like a better choice. And that's what you will do in this challenge.

But before you start, recall the PolynomialFeatures class.

The fit_transform(X) method requires X to be a 2-D array (or a DataFrame).
Using X = df[['column_name']] will get your X suited for fit_transform().
And if you have a 1-D array, use .reshape(-1, 1) to make a 2-D array with the same contents.

The task is to build a Polynomial Regression of degree 2 using PolynomialFeatures and OLS.

Tarea

Swipe to start coding

  1. Assign the X variable to a DataFrame containing column 'age'.
  2. Create an X_tilde matrix using the PolynomialFeatures class.
  3. Build and train a Polynomial Regression model.
  4. Reshape X_new to be a 2-D array.
  5. Preprocess X_new the same way as X.
  6. Print the model's parameters.

Solución

import pandas as pd
import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures

df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/houses_poly.csv')
# Assign the variables
X = df[['age']]
y = df['price']
n = 2 # A degree of Polynomial Regression
# Preprocess X
X_tilde = PolynomialFeatures(n).fit_transform(X)
# Build and train the model
model = sm.OLS(y, X_tilde).fit()
# Create and preprocess X_new
X_new = np.linspace(0, 125, 200).reshape(-1, 1)
X_new_tilde = PolynomialFeatures(n).fit_transform(X_new)
# Predict the target for X_new
y_pred = model.predict(X_new_tilde)
# Visualize the result
plt.scatter(X, y, alpha=0.4)
plt.plot(X_new, y_pred, color='orange')
plt.show()
# Print the model's parameters
print(model.params)

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 5
single

single

import pandas as pd
import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures

df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/houses_poly.csv')
# Assign the variables
X = df[['___']]
y = df['price']
n = 2 # A degree of Polynomial Regression
# Preprocess X
X_tilde = PolynomialFeatures(n).___(X)
# Build and train the model
model = sm.OLS(y, ___).fit()
# Create and preprocess X_new
X_new = np.linspace(0, 125, 200).___(-1, 1)
X_new_tilde = ___(n).fit_transform(___)
# Predict the target for X_new
y_pred = model.predict(X_new_tilde)
# Visualize the result
plt.scatter(X, y, alpha=0.4)
plt.plot(X_new, y_pred, color='orange')
plt.show()
# Print the model's parameters
print(model.___)

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

some-alt