Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Preise Mit Zwei Merkmalen Vorhersagen | Multiple Lineare Regression
Lineare Regression mit Python

book
Preise Mit Zwei Merkmalen Vorhersagen

Für diese Herausforderung wird dasselbe Wohnungsdatensatz verwendet. Allerdings hat es jetzt zwei Merkmale: Alter und Fläche des Hauses (Spalten age und square_feet).

import pandas as pd

df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/houseprices.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/houseprices.csv') print(df.head())
copy

Ihre Aufgabe ist es, ein Multiple Lineare Regressionsmodell mit der OLS-Klasse zu erstellen. Außerdem werden Sie die Zusammenfassungstabelle ausdrucken, um die p-Werte jedes Merkmals zu betrachten.

Aufgabe

Swipe to start coding

  1. Weisen Sie die Spalten 'age' und 'square_feet' von df X zu.
  2. Vorverarbeiten Sie X für den Konstruktor der OLS-Klasse.
  3. Erstellen und trainieren Sie das Modell mit der OLS-Klasse.
  4. Vorverarbeiten Sie das X_new-Array genauso wie X.
  5. Sagen Sie das Ziel für X_new voraus.
  6. Drucken Sie die Zusammenfassungstabelle des Modells aus.

Lösung

import pandas as pd
import numpy as np
import statsmodels.api as sm

df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/houseprices.csv')
# Assign the variables
X = df[['age', 'square_feet']]
y = df['price']
# Preprocess X
X_tilde = sm.add_constant(X)
# Build and train the model
model = sm.OLS(y, X_tilde).fit()
# Create and preprocess X_new
X_new = np.array([[4, 10000], [30, 14000], [70, 16000]])
X_new_tilde = sm.add_constant(X_new)
# Predict instances from X_new and print them
y_pred = model.predict(X_new_tilde)
print('Prediction:', np.floor(y_pred)) # np.floor() keeps only the whole part of the numbers
# Print the summary table
print(model.summary())

Wenn Sie alles richtig gemacht haben, haben Sie p-Werte nahe null erhalten. Das bedeutet, dass alle unsere Merkmale für das Modell signifikant sind.

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 5
import pandas as pd
import numpy as np
import statsmodels.api as sm

df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/houseprices.csv')
# Assign the variables
X = df[['___', 'square_feet']]
y = df['price']
# Preprocess X
X_tilde = sm.___(X)
# Build and train the model
model = sm.OLS(___, ___).___()
# Create and preprocess X_new
X_new = np.array([[4, 10000], [30, 14000], [70, 16000]])
X_new_tilde = ___.___(X_new)
# Predict instances from X_new and print them
y_pred = ___.predict(___)
print('Prediction:', np.floor(y_pred)) # np.floor() keeps only the whole part of the numbers
# Print the summary table
print(model.___())
toggle bottom row
some-alt