{"id":9258,"date":"2020-10-13T18:01:17","date_gmt":"2020-10-13T18:01:17","guid":{"rendered":"https:\/\/www.askpython.com\/?p=9258"},"modified":"2026-09-29T16:44:49","modified_gmt":"2026-09-29T16:44:49","slug":"rmse-root-mean-square-error","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/rmse-root-mean-square-error","title":{"rendered":"RMSE &#8211; Root Mean Square Error in Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hello readers. In this article, we will be focusing on Implementing RMSE &#8211; <strong>Root Mean Square Error<\/strong> as a metric in Python. So, let us get started!!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is Root Mean Square Error (RMSE) in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before diving deep into the concept of RMSE, let us first understand the error metrics in Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Error metrics<\/strong> enable us to track the efficiency and accuracy through various metrics as shown below&#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Mean Square Error(MSE)<\/strong><\/li><li><strong>Root Mean Square Error(RMSE)<\/strong><\/li><li><strong>R-square<\/strong><\/li><li><strong>Accuracy<\/strong><\/li><li><strong>MAPE<\/strong>, etc.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Mean Square error is one such error metric for judging the accuracy and error rate of any machine learning algorithm for a regression problem.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, <strong>MSE <\/strong>is a risk function that helps us determine the <strong>average squared difference between the predicted and the actual value<\/strong> of a feature or variable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">RMSE is an acronym for <strong>Root Mean Square Error<\/strong>, which is the<strong> square root of value obtained from Mean Square Error<\/strong> function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Using RMSE, we can easily plot a difference between the estimated and actual values of a parameter of the model<\/strong>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By this, we can clearly judge the efficiency of the model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Usually, a RMSE score of less than 180 is considered a good score for a moderately or well working algorithm. In case, the RMSE value exceeds 180, we need to perform feature selection and hyper parameter tuning on the parameters of the model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let us now focus on the implementation of the same in the upcoming section.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Root Mean Square Error with NumPy module<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let us have a look at the below formula&#8211;<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"524\" height=\"175\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/RMSE.png\" alt=\"RMSE Root Mean Square Error\" class=\"wp-image-9260\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/RMSE.png 524w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/RMSE-300x100.png 300w\" sizes=\"auto, (max-width: 524px) 100vw, 524px\" \/><figcaption><strong>RMSE<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">So, as seen above, Root Mean Square Error is the square root of the average of the squared differences between the estimated and the actual value of the variable\/feature.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the below example, we have implemented the concept of RMSE using the functions of <a aria-label=\"NumPy module  (opens in a new tab)\" href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">NumPy module<\/a> as mentioned below&#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Calculate the difference between the estimated and the actual value using <code>numpy.subtract()<\/code> function.<\/li><li>Further, calculate the square of the above results using <code>numpy.square() <\/code>function.<\/li><li>Finally, calculate the mean of the squared value using <code>numpy.mean()<\/code> function. The output is the MSE score.<\/li><li>At the end, calculate the square root of MSE using <code>math.sqrt()<\/code> function to get the RMSE value.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport math\ny_actual = &#x5B;1,2,3,4,5]\ny_predicted = &#x5B;1.6,2.5,2.9,3,4.1]\n\nMSE = np.square(np.subtract(y_actual,y_predicted)).mean() \n\nRMSE = math.sqrt(MSE)\nprint(&quot;Root Mean Square Error:\\n&quot;)\nprint(RMSE)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nRoot Mean Square Error:\n\n0.6971370023173351\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">RMSE with <strong>Python scikit learn library<\/strong> <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, we have calculated the MSE score using <code>mean_square_error()<\/code> function from <strong>sklearn.metrics<\/strong> library.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Further, have calculated the RMSE score through the square root of MSE as shown below:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom sklearn.metrics import mean_squared_error\nimport math\ny_actual = &#x5B;1,2,3,4,5]\ny_predicted = &#x5B;1.6,2.5,2.9,3,4.1]\n\nMSE = mean_squared_error(y_actual, y_predicted)\n\nRMSE = math.sqrt(MSE)\nprint(&quot;Root Mean Square Error:\\n&quot;)\nprint(RMSE)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nRoot Mean Square Error:\n\n0.6971370023173351\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For more such posts related to Python, Stay tuned and till then, Happy Learning!! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello readers. In this article, we will be focusing on Implementing RMSE &#8211; Root Mean Square Error as a metric in Python. So, let us get started!! What is Root Mean Square Error (RMSE) in Python? Before diving deep into the concept of RMSE, let us first understand the error metrics in Python. Error metrics [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":68257,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-9258","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/9258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=9258"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/9258\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/68257"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=9258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=9258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=9258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}