Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Inflation Rate Forecast | Economic Modeling and Regression
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for Economists

bookChallenge: Inflation Rate Forecast

In this challenge, you will apply your knowledge of linear regression and data visualization to forecast future inflation rates. You will work with a hardcoded pandas DataFrame containing annual inflation rates for a country over the past 15 years. Your goal is to write a function that fits a linear regression model to this data and then uses it to predict the inflation rate for the next two years. Additionally, you will visualize both the historical and forecasted inflation rates using matplotlib to clearly communicate the results.

To begin, consider how economic forecasting often relies on historical trends to estimate future values. By fitting a linear regression model to past inflation data, you can capture the underlying trend and use it to make informed predictions. Plotting the results allows you to compare the actual historical data with your model's forecasts, which is a common practice in economic reporting and analysis.

You will need to use pandas to manage the data, scikit-learn's LinearRegression to fit the model, and matplotlib to create the visualization. The function you write should take no arguments, operate on the provided DataFrame, and return the two forecasted inflation rates as a tuple. The plot should show the historical inflation rates as well as the regression line extended to include the two forecasted years.

Uppgift

Swipe to start coding

Write a function named forecast_inflation that does the following:

  • Creates a pandas DataFrame with two columns: "Year" (integers for the last 15 years, ending with 2023) and "InflationRate" (15 realistic annual inflation rates, hardcoded as floats).
  • Fits a linear regression model using scikit-learn to predict InflationRate based on Year.
  • Forecasts the inflation rate for the next two years (2024 and 2025) and returns these values as a tuple.
  • Plots the historical inflation rates as blue dots, the regression line for all years (2009–2025) as a red line, and highlights the forecasted points (2024, 2025) as green dots.
  • Labels the axes and adds a legend.

Your function should not take any arguments and should return a tuple of two predicted inflation rates for 2024 and 2025.

Lösning

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 7
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

close

bookChallenge: Inflation Rate Forecast

Svep för att visa menyn

In this challenge, you will apply your knowledge of linear regression and data visualization to forecast future inflation rates. You will work with a hardcoded pandas DataFrame containing annual inflation rates for a country over the past 15 years. Your goal is to write a function that fits a linear regression model to this data and then uses it to predict the inflation rate for the next two years. Additionally, you will visualize both the historical and forecasted inflation rates using matplotlib to clearly communicate the results.

To begin, consider how economic forecasting often relies on historical trends to estimate future values. By fitting a linear regression model to past inflation data, you can capture the underlying trend and use it to make informed predictions. Plotting the results allows you to compare the actual historical data with your model's forecasts, which is a common practice in economic reporting and analysis.

You will need to use pandas to manage the data, scikit-learn's LinearRegression to fit the model, and matplotlib to create the visualization. The function you write should take no arguments, operate on the provided DataFrame, and return the two forecasted inflation rates as a tuple. The plot should show the historical inflation rates as well as the regression line extended to include the two forecasted years.

Uppgift

Swipe to start coding

Write a function named forecast_inflation that does the following:

  • Creates a pandas DataFrame with two columns: "Year" (integers for the last 15 years, ending with 2023) and "InflationRate" (15 realistic annual inflation rates, hardcoded as floats).
  • Fits a linear regression model using scikit-learn to predict InflationRate based on Year.
  • Forecasts the inflation rate for the next two years (2024 and 2025) and returns these values as a tuple.
  • Plots the historical inflation rates as blue dots, the regression line for all years (2009–2025) as a red line, and highlights the forecasted points (2024, 2025) as green dots.
  • Labels the axes and adds a legend.

Your function should not take any arguments and should return a tuple of two predicted inflation rates for 2024 and 2025.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 7
single

single

some-alt