Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Challenge: Predicting Housing Prices | Economic Modeling and Regression
Python for Economists

bookChallenge: Predicting Housing Prices

You will now apply your regression modeling skills to a practical economic dataset: predicting housing prices based on key features. Imagine you are provided with a small dataset containing information about various houses, such as the number of rooms, a location index (which quantifies neighborhood desirability), the age of the home, and the actual sale price. Your task is to create a function that builds a linear regression model using scikit-learn, extracts the learned coefficients, and uses the model to predict the price of a new house given its features.

To start, you'll need to construct a pandas DataFrame with the housing data. This DataFrame will serve as the basis for fitting your regression model. After fitting the model, you will extract the coefficients, which indicate how each feature affects the predicted price. Finally, you will use the trained model to predict the price of a new house by passing in its features.

1234567891011121314151617181920212223242526272829303132333435
import pandas as pd from sklearn.linear_model import LinearRegression # Hardcoded housing data data = { "num_rooms": [3, 4, 2, 5, 3, 4, 2, 3], "location_index": [7, 8, 6, 9, 7, 8, 5, 6], "age": [10, 5, 20, 2, 15, 7, 25, 12], "price": [210000, 275000, 150000, 340000, 200000, 265000, 130000, 180000] } df = pd.DataFrame(data) def fit_and_predict_housing(df, new_features): # Select features and target X = df[["num_rooms", "location_index", "age"]] y = df["price"] # Fit linear regression model model = LinearRegression() model.fit(X, y) # Extract coefficients coefficients = dict(zip(X.columns, model.coef_)) # Predict for new features predicted_price = model.predict([new_features])[0] return coefficients, predicted_price # Example usage: new_house = [4, 7, 8] # 4 rooms, location index 7, age 8 years coeffs, prediction = fit_and_predict_housing(df, new_house) print("Coefficients:", coeffs) print("Predicted price for new house:", prediction)
copy

This approach allows you to quantitatively understand how different features contribute to housing prices and to make informed predictions for new properties. By fitting the model on the provided DataFrame, you ensure your predictions are based on the observed relationships in the data. The coefficients reveal the expected change in price for a one-unit increase in each feature, holding others constant.

Taak

Swipe to start coding

Write a function fit_and_predict_housing that takes two arguments:

  • A pandas DataFrame containing columns: "num_rooms", "location_index", "age", and "price";
  • A list or array with three values representing a new house's features in the order: number of rooms, location index, and age.

Your function should:

  • Fit a linear regression model (using scikit-learn) to predict "price" from the other columns;
  • Return a tuple containing:
    • A dictionary mapping each feature name to its coefficient from the fitted model;
    • The predicted price for the new house.

Test your function using the hardcoded DataFrame from the code sample and the new house [4, 7, 8].

Oplossing

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3
single

single

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you explain what each coefficient means in this context?

How accurate is this model likely to be with such a small dataset?

Can you show how to predict the price for a different set of house features?

close

bookChallenge: Predicting Housing Prices

Veeg om het menu te tonen

You will now apply your regression modeling skills to a practical economic dataset: predicting housing prices based on key features. Imagine you are provided with a small dataset containing information about various houses, such as the number of rooms, a location index (which quantifies neighborhood desirability), the age of the home, and the actual sale price. Your task is to create a function that builds a linear regression model using scikit-learn, extracts the learned coefficients, and uses the model to predict the price of a new house given its features.

To start, you'll need to construct a pandas DataFrame with the housing data. This DataFrame will serve as the basis for fitting your regression model. After fitting the model, you will extract the coefficients, which indicate how each feature affects the predicted price. Finally, you will use the trained model to predict the price of a new house by passing in its features.

1234567891011121314151617181920212223242526272829303132333435
import pandas as pd from sklearn.linear_model import LinearRegression # Hardcoded housing data data = { "num_rooms": [3, 4, 2, 5, 3, 4, 2, 3], "location_index": [7, 8, 6, 9, 7, 8, 5, 6], "age": [10, 5, 20, 2, 15, 7, 25, 12], "price": [210000, 275000, 150000, 340000, 200000, 265000, 130000, 180000] } df = pd.DataFrame(data) def fit_and_predict_housing(df, new_features): # Select features and target X = df[["num_rooms", "location_index", "age"]] y = df["price"] # Fit linear regression model model = LinearRegression() model.fit(X, y) # Extract coefficients coefficients = dict(zip(X.columns, model.coef_)) # Predict for new features predicted_price = model.predict([new_features])[0] return coefficients, predicted_price # Example usage: new_house = [4, 7, 8] # 4 rooms, location index 7, age 8 years coeffs, prediction = fit_and_predict_housing(df, new_house) print("Coefficients:", coeffs) print("Predicted price for new house:", prediction)
copy

This approach allows you to quantitatively understand how different features contribute to housing prices and to make informed predictions for new properties. By fitting the model on the provided DataFrame, you ensure your predictions are based on the observed relationships in the data. The coefficients reveal the expected change in price for a one-unit increase in each feature, holding others constant.

Taak

Swipe to start coding

Write a function fit_and_predict_housing that takes two arguments:

  • A pandas DataFrame containing columns: "num_rooms", "location_index", "age", and "price";
  • A list or array with three values representing a new house's features in the order: number of rooms, location index, and age.

Your function should:

  • Fit a linear regression model (using scikit-learn) to predict "price" from the other columns;
  • Return a tuple containing:
    • A dictionary mapping each feature name to its coefficient from the fitted model;
    • The predicted price for the new house.

Test your function using the hardcoded DataFrame from the code sample and the new house [4, 7, 8].

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3
single

single

some-alt