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

Tehtävä

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.

Ratkaisu

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 7
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you provide the hardcoded DataFrame with the inflation data?

Could you clarify if you want the plot to display both the historical and forecasted years on the same axis?

Do you need an explanation of how linear regression is applied in this context?

close

bookChallenge: Inflation Rate Forecast

Pyyhkäise näyttääksesi valikon

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.

Tehtävä

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 7
single

single

some-alt