Challenge: 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.
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
InflationRatebased onYear. - 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
Takk for tilbakemeldingene dine!
single
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
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?
Fantastisk!
Completion rate forbedret til 4.76
Challenge: Inflation Rate Forecast
Sveip for å vise menyen
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.
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
InflationRatebased onYear. - 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
Takk for tilbakemeldingene dine!
single