Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Predicting Housing Prices | Economic Modeling and Regression
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
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.

Завдання

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].

Рішення

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

close

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.

Завдання

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].

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3
single

single

some-alt