Forecasting Parameter Optimization by Minimizing the Mean Square Error in Python

Prerequisites To Follow this Exercise : 

  1. Python Installed in your System : If you are a new user to Python and want to know how to install Python via the Anaconda Distribution , You can go through the step by step Blog i have written to install Python via Anaconda Distribution & start using Jupyter Notebook :

Once you have the  Python Installed in your system you are Good to Go ahead and follow the below Use Case and Example. 


Consider the below data set stored as comma separated csv file.


Use the 2017 Data to predict the sales in the year 2018. For making prediction use Moving Average Algorithm. Use the following values for Window as a parameter in Moving Average Algorithm: {3, 4, 5, 6}.

For each of the above values of parameter compute the value of mean square error.

Plot the mean square error to select the value of Window based on least mean square value.





Try On you Own First Before Referring to the Solution Below 





Here the first step is to store the sales data in python list.

SO, first we will create an empty list to store the sales data that exists in index 4 in the csv file.

Next is to read the csv file line by line and populate the empty list line by line.

We will use the previous code to solve this. The extra step would be to add another outer loop to iterate for different values of parameter i.e. Window for Moving Average Algorithm.

For each of the predictions made with respective value of Window the mean square error is being computed in each loop.

The mse is the list where all the mean square errors are being stored.  Here is the Code Snippet To Achieve the Desire Result. If you want to get the Code as Python Code File Subscribe to our website - and our YouTube Channel Below -


There are no entries yet.
Please enter the code
* Required fields

Subscribe to Our YouTube Channel To recieve updates on Free Learning



Visit Our Website to View the Upcoming Training Schedule

Looking for Corporate Training ? Reach out to us at

Reach out to us if you are looking for Corporate Training to Build The Next Generation Analytical Workforce with an in-depth understanding of  Exploratory Data Analysis , Data Visualisation, Data Analytics , AI First , Machine Learning & Deep Learning Training & Consulting helping them to take Data Informed Decision at each stage of the business. 

We understand that At the present times , the Entire Industry is in a Tranformation stage with the Softwares  being rebuilt with Artificial Intelligence Capabilities .

We need SMART WORKFORCE for the SMART SOFTWARES to reap the maximum return . 


Whatsapp at +91-9953805788 or email at - if you would like to know more . 


  • Corporate Tableau Training in Gurgaon
  • Corporate Data Analytics Training in Gurgaon
  • Corporate Microsoft Power BI Training in Gurgaon
  • Corporate Microstrategy Training in Gurgaon
  • Corporate Google Data Studio Training in Gurgaon
  • Corporate Python Training in Gurgaon
  • Corporate Advance Analytics in R Programming Training in Gurgaon
  • Corporate Machine Learning Training in Gurgaon
  • Corporate Deep Learning Training in Gurgaon
  • Corporate Data Visualization Training in Gurgaon



Instrovate Technologies

Noida, Gurgaon 

Hit Your Refresh Button To Rise Higher

Subscribe to Our YouTube Channel To Receive Updates on the Latest Free Learning Video Added

Print Print | Sitemap
All rights reserved @ Instrovate Technologies

Terms Of Services    Privacy Policy    Disclaimer     Refund Policy