Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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.

Opgave

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 alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 7
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

close

bookChallenge: Inflation Rate Forecast

Stryg for at vise menuen

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.

Opgave

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 desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 7
single

single

some-alt