{"id":15528,"date":"2021-08-13T18:49:31","date_gmt":"2021-08-13T13:19:31","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=15528"},"modified":"2021-08-13T18:49:35","modified_gmt":"2021-08-13T13:19:35","slug":"mean-squared-error-python","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/","title":{"rendered":"How To Calculate Mean Squared Error In Python"},"content":{"rendered":"\n<p>In this article, we are going to learn how to calculate the mean squared error in python? We are using two python libraries to calculate the mean squared error.\u00a0<strong><a href=\"http:\/\/www.pythonpool.com\/category\/numpy\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\">NumPy<\/span><\/a><\/strong>\u00a0and\u00a0<strong>sklearn<\/strong>\u00a0are the libraries we are going to use here. Also, we will learn how to calculate without using any module.<\/p>\n\n\n\n<p><strong>MSE is also useful for regression problems that are normally distributed. It is the mean squared error. So the squared error<\/strong> <strong>between the predicted values and the actual values<\/strong>. <strong>The summation of all the data points of the square difference between the predicted and actual values is divided by the no. of data points.<\/strong><\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#Formula_to_calculate_mean_squared_error\" >Formula to calculate mean squared error<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#Derivation_of_Mean_Squared_Error\" >Derivation of Mean Squared Error<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#Line_regression_graph\" >Line regression graph<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#To_get_the_Mean_Squared_Error_in_Python_using_NumPy\" >To get the Mean Squared Error in Python using NumPy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#To_get_the_MSE_using_sklearn\" >To get the MSE using sklearn<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#Syntax\" >Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#Parameters\" >Parameters<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#Returns\" >Returns<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#Code\" >Code<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#Calculating_Mean_Squared_Error_Without_Using_any_Modules\" >Calculating Mean Squared Error Without Using any Modules<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#Calculate_Mean_Squared_Error_Using_Negative_Values\" >Calculate Mean Squared Error Using Negative Values<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#Code-2\" >Code<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#Bonus_Gradient_Descent\" >Bonus: Gradient Descent<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#Code-3\" >Code:<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#Frequently_Asked_Questions_Related_to_Mean_Squared_Error_in_Python\" >Frequently Asked Questions Related to Mean Squared Error in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#Recommended_Python_Articles\" >Recommended Python Articles<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-formula-to-calculate-mean-squared-error\"><span class=\"ez-toc-section\" id=\"Formula_to_calculate_mean_squared_error\"><\/span>Formula to calculate mean squared error<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/20210812_200937_0000-1024x270.png\" alt=\"mean squared error in python\" class=\"wp-image-15533\" width=\"707\" height=\"186\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/20210812_200937_0000-1024x270.png 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/20210812_200937_0000-300x79.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/20210812_200937_0000-768x203.png 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/20210812_200937_0000.png 1197w\" sizes=\"(max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p>Where Y<sub>i <\/sub>and  \u0176<sub>i<\/sub> represent the actual values and the predicted values, the difference between them is squared.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-derivation-of-mean-squared-error\"><span class=\"ez-toc-section\" id=\"Derivation_of_Mean_Squared_Error\"><\/span>Derivation of Mean Squared Error <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>First to find the regression line for the values (1,3), (2,2), (3,6), (4,1), (5,5). The regression value for the value is <strong>&nbsp;y=1.6+0.4x<\/strong>. Next to find the new Y values. The new values for y are tabulated below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-pale-ocean-gradient-background has-background\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\"><strong>Given x value<\/strong><\/th><th class=\"has-text-align-left\" data-align=\"left\"><strong>Calculating y value<\/strong><\/th><th class=\"has-text-align-left\" data-align=\"left\"><strong>New y value<\/strong><\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\">1<\/td><td class=\"has-text-align-left\" data-align=\"left\">1.6+0.4(1)<\/td><td class=\"has-text-align-left\" data-align=\"left\">2<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">2<\/td><td class=\"has-text-align-left\" data-align=\"left\">1.6+0.4(2)<\/td><td class=\"has-text-align-left\" data-align=\"left\">2.4<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">3<\/td><td class=\"has-text-align-left\" data-align=\"left\">1.6+0.4(3)<\/td><td class=\"has-text-align-left\" data-align=\"left\">2.8<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">4<\/td><td class=\"has-text-align-left\" data-align=\"left\"> 1.6+0.4(4)<\/td><td class=\"has-text-align-left\" data-align=\"left\">3.2<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">5<\/td><td class=\"has-text-align-left\" data-align=\"left\"> 1.6+0.4(5)<\/td><td class=\"has-text-align-left\" data-align=\"left\">3.6<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Now to find the error ( Y<sub>i <\/sub>&#8211; \u0176<sub>i<\/sub> ) <\/p>\n\n\n\n<p>We have to square all the errors<\/p>\n\n\n\n<p>By adding all the errors we will get the MSE<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-line-regression-graph\"><span class=\"ez-toc-section\" id=\"Line_regression_graph\"><\/span>Line regression graph<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let us consider the values  (1,3), (2,2), (3,6), (4,1), (5,5) to plot the graph. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/Screenshot-18.png\" alt=\"Line regression graph\" class=\"wp-image-15538\" width=\"712\" height=\"705\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/Screenshot-18.png 412w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/Screenshot-18-300x297.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/Screenshot-18-150x150.png 150w\" sizes=\"(max-width: 712px) 100vw, 712px\" \/><\/figure>\n\n\n\n<p>The straight line represents the predicted value in this graph, and the points represent the actual data. The difference between this line and the points is squared, known as mean squared error. <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>Also, Read |<\/strong> <a href=\"http:\/\/www.pythonpool.com\/square-root-in-python\/\" rel=\"noreferrer noopener\" target=\"_blank\"><span style=\"text-decoration: underline;\">How to Calculate Square Root in Python<\/span><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-to-get-the-mean-squared-error-in-python-using-numpy\"><span class=\"ez-toc-section\" id=\"To_get_the_Mean_Squared_Error_in_Python_using_NumPy\"><\/span>To get the Mean Squared Error in Python using NumPy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\ntrue_value_of_y= &#x5B;3,2,6,1,5]\npredicted_value_of_y= &#x5B;2.0,2.4,2.8,3.2,3.6]\nMSE = np.square(np.subtract(true_value_of_y,predicted_value_of_y)).mean()\nprint(MSE)\n<\/pre><\/div>\n\n\n<p>Importing numpy library as np. Creating two variables. true_value_of_y holds an original value. predicted_value_of_y holds a calculated value. Next, giving the formula to calculate the mean squared error. <\/p>\n\n\n\n<p class=\"has-medium-font-size\" id=\"h-output\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">3.6400000000000006<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-to-get-the-mse-using-sklearn\"><span class=\"ez-toc-section\" id=\"To_get_the_MSE_using_sklearn\"><\/span>To get the MSE using sklearn<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>sklearn is a library that is used for many mathematical calculations in python. Here we are going to use this library to calculate the MSE<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-syntax\"><span class=\"ez-toc-section\" id=\"Syntax\"><\/span>Syntax<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">sklearn.metrices.mean_squared_error(<em>y_true<\/em>,&nbsp;<em>y_pred<\/em>,&nbsp;<em>*<\/em>,&nbsp;<em>sample_weight=None<\/em>,&nbsp;<em>multioutput='uniform_average'<\/em>,&nbsp;<em>squared=True<\/em>)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-parameters\"><span class=\"ez-toc-section\" id=\"Parameters\"><\/span>Parameters<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>y_true &#8211; true value of y<\/li><li>y_pred &#8211; predicted value of y<\/li><li>sample_weight<\/li><li>multioutput<\/li><li>raw_values<\/li><li>uniform_average<\/li><li>squared<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-returns\"><span class=\"ez-toc-section\" id=\"Returns\"><\/span>Returns<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Mean squared error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-code\"><span class=\"ez-toc-section\" id=\"Code\"><\/span>Code<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom sklearn.metrics import mean_squared_error\ntrue_value_of_y= &#x5B;3,2,6,1,5]\npredicted_value_of_y= &#x5B;2.0,2.4,2.8,3.2,3.6]\nmean_squared_error(true_value_of_y,predicted_value_of_y)\nprint(mean_squared_error(true_value_of_y,predicted_value_of_y))\n<\/pre><\/div>\n\n\n<p>From sklearn.metrices library importing mean_squared_error. Creating two variables. true_value_of_y holds an original value. predicted_value_of_y holds a calculated value. Next, giving the formula to calculate the mean squared error.  <\/p>\n\n\n\n<p class=\"has-medium-font-size\" id=\"h-output-1\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">3.6400000000000006<\/pre>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-kilo monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Popular now<\/span><span class=\"monsterinsights-inline-popular-posts-border\" ><\/span><span class=\"monsterinsights-inline-popular-posts-border-2\" ><\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\">[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-calculating-mean-squared-error-without-using-any-modules\"><span class=\"ez-toc-section\" id=\"Calculating_Mean_Squared_Error_Without_Using_any_Modules\"><\/span>Calculating Mean Squared Error Without Using any Modules<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntrue_value_of_y = &#x5B;3,2,6,1,5]\npredicted_value_of_y = &#x5B;2.0,2.4,2.8,3.2,3.6]\nsummation_of_value = 0  \nn = len(true_value_of_y) \nfor i in range (0,n):\n  difference_of_value = true_value_of_y&#x5B;i] - predicted_value_of_y&#x5B;i]  \n  squared_difference = difference_of_value**2   \n  summation_of_value = summation_of_value + squared_difference\nMSE = summation_of_value\/n  \nprint (&quot;The Mean Squared Error is: &quot; , MSE)\n<\/pre><\/div>\n\n\n<p>Declaring the true values and the predicted values to two different variables. Initializing the variable summation_of_value is zero to store the values. <strong>len()<\/strong> function is useful to check the number of values in true_value_of_y. Creating for loop to iterate. Calculating the difference between true_value and the predicted_value. Next getting the square of the difference. Adding all the squared differences, we will get the MSE.<\/p>\n\n\n\n<p class=\"has-medium-font-size\" id=\"h-output-2\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The Mean Squared Error is:  3.6400000000000006<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-calculate-mean-squared-error-using-negative-values\"><span class=\"ez-toc-section\" id=\"Calculate_Mean_Squared_Error_Using_Negative_Values\"><\/span>Calculate Mean Squared Error Using Negative Values<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Now let us consider some negative values to calculate MSE. The values are (1,2), (3,-1), (5,0.6), (4,-0.7), (2,-0.2). The regression line equation is <strong>y=1.13-0.33x<\/strong><\/p>\n\n\n\n<p><strong>The line regression graph for this value is:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/Screenshot-21.png\" alt=\"Calculate mean squared error using negative values\" class=\"wp-image-15549\" width=\"702\" height=\"684\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/Screenshot-21.png 425w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/Screenshot-21-300x292.png 300w\" sizes=\"(max-width: 702px) 100vw, 702px\" \/><\/figure>\n\n\n\n<p><strong>New y values for this will be:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-pale-ocean-gradient-background has-background\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\"> <strong>Given x value<\/strong> <\/th><th class=\"has-text-align-left\" data-align=\"left\"> <strong>Calculating y value<\/strong> <\/th><th class=\"has-text-align-left\" data-align=\"left\"> <strong>New y value<\/strong> <\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\">1<\/td><td class=\"has-text-align-left\" data-align=\"left\">1.13-033(1)<\/td><td class=\"has-text-align-left\" data-align=\"left\">0.9<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">3<\/td><td class=\"has-text-align-left\" data-align=\"left\">1.13-033(3) <\/td><td class=\"has-text-align-left\" data-align=\"left\">0.1<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">5<\/td><td class=\"has-text-align-left\" data-align=\"left\">1.13-033(5) <\/td><td class=\"has-text-align-left\" data-align=\"left\">-0.4<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">4<\/td><td class=\"has-text-align-left\" data-align=\"left\">1.13-033(4) <\/td><td class=\"has-text-align-left\" data-align=\"left\">-0.1<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">2<\/td><td class=\"has-text-align-left\" data-align=\"left\">1.13-033(2) <\/td><td class=\"has-text-align-left\" data-align=\"left\">0.6<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-code-1\"><span class=\"ez-toc-section\" id=\"Code-2\"><\/span>Code<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; from sklearn.metrics import mean_squared_error\n&gt;&gt;&gt; y_true = &#x5B;2,-1,0.6,-0.7,-0.2]\n&gt;&gt;&gt; y_pred = &#x5B;0.9,0.1,-0.4,-0.1,0.6]\n&gt;&gt;&gt; mean_squared_error(y_true, y_pred)\n<\/pre><\/div>\n\n\n<p>First, importing a module. Declaring values to the variables. Here we are using negative value to calculate. Using the mean_squared_error module, we are calculating the MSE. <\/p>\n\n\n\n<p class=\"has-medium-font-size\" id=\"h-output-3\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">0.884<\/pre>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-delta monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-icon\" style=\"\"><svg width=\"14\" height=\"19\" viewBox=\"0 0 14 19\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\"><path d=\"M7.875 0.899463C7.875 1.59183 8.0816 2.24711 8.49479 2.8653C8.93229 3.48349 9.44271 4.06458 10.026 4.60859C10.6337 5.15259 11.2292 5.73369 11.8125 6.35188C12.4201 6.97007 12.9306 7.76135 13.3438 8.72572C13.7812 9.66537 14 10.7163 14 11.8785C14 13.832 13.3073 15.5011 11.9219 16.8858C10.5608 18.2953 8.92014 19 7 19C5.07986 19 3.42708 18.2953 2.04167 16.8858C0.680556 15.5011 0 13.832 0 11.8785C0 9.94973 0.668403 8.28062 2.00521 6.87116C2.27257 6.57443 2.58854 6.50024 2.95312 6.64861C3.31771 6.79697 3.5 7.08134 3.5 7.50171V10.6545C3.5 11.3221 3.71875 11.8908 4.15625 12.3607C4.61806 12.8305 5.16493 13.0654 5.79688 13.0654C6.45312 13.0654 7.01215 12.8428 7.47396 12.3978C7.93576 11.9279 8.16667 11.3592 8.16667 10.6916C8.16667 10.2712 8.04514 9.86318 7.80208 9.46754C7.58333 9.0719 7.31597 8.71336 7 8.3919C6.68403 8.07044 6.34375 7.73662 5.97917 7.39043C5.63889 7.04425 5.34722 6.66097 5.10417 6.2406C4.88542 5.82024 4.73958 5.35041 4.66667 4.83114C4.59375 4.31186 4.67882 3.68131 4.92188 2.93948C5.18924 2.17293 5.63889 1.33219 6.27083 0.417277C6.51389 0.0463641 6.84201 -0.0772735 7.25521 0.0463641C7.6684 0.170002 7.875 0.454368 7.875 0.899463Z\" fill=\"#EB5757\"><\/path><\/svg><\/span><span class=\"monsterinsights-inline-popular-posts-label\" >Trending<\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\">[Fixed] nameerror: name Unicode is not defined<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-bonus-gradient-descent\"><span class=\"ez-toc-section\" id=\"Bonus_Gradient_Descent\"><\/span>Bonus: Gradient Descent<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Gradient Descent is used to find the local minimum of the functions. In this case, the functions need to be differentiable. The basic idea is to move in the direction opposite from the derivate at any point.<\/p>\n\n\n\n<p>The following code works on a set of values that are available on the <span style=\"text-decoration: underline;\"><a href=\"https:\/\/github.com\/pythonpool\/Gradient-Descent\" target=\"_blank\" rel=\"noreferrer noopener\">Github repository<\/a>.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Code-3\"><\/span><strong>Code:<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#!\/usr\/bin\/python\n# -*- coding: utf-8 -*-\nfrom numpy import *\n\n\ndef compute_error(b, m, points):\n    totalError = 0\n    for i in range(0, len(points)):\n        x = points&#x5B;i, 0]\n        y = points&#x5B;i, 1]\n        totalError += (y - (m * x + b)) ** 2\n    return totalError \/ float(len(points))\n\n\ndef gradient_step(\n    b_current,\n    m_current,\n    points,\n    learningRate,\n    ):\n    b_gradient = 0\n    m_gradient = 0\n    N = float(len(points))\n    for i in range(0, len(points)):\n        x = points&#x5B;i, 0]\n        y = points&#x5B;i, 1]\n        b_gradient += -(2 \/ N) * (y - (m_current * x + b_current))\n        m_gradient += -(2 \/ N) * x * (y - (m_current * x + b_current))\n    new_b = b_current - learningRate * b_gradient\n    new_m = m_current - learningRate * m_gradient\n    return &#x5B;new_b, new_m]\n\n\ndef gradient_descent_runner(\n    points,\n    starting_b,\n    starting_m,\n    learning_rate,\n    iterations,\n    ):\n    b = starting_b\n    m = starting_m\n    for i in range(iterations):\n        (b, m) = gradient_step(b, m, array(points), learning_rate)\n    return &#x5B;b, m]\n\n\ndef main():\n    points = genfromtxt('data.csv', delimiter=',')\n    learning_rate = 0.00001\n    initial_b = 0\n    initial_m = 0\n    iterations = 10000\n    print('Starting gradient descent at b = {0}, m = {1}, error = {2}'.format(initial_b,\n            initial_m, compute_error(initial_b, initial_m, points)))\n    print('Running...')\n    &#x5B;b, m] = gradient_descent_runner(points, initial_b, initial_m,\n            learning_rate, iterations)\n    print('After {0} iterations b = {1}, m = {2}, error = {3}'.format(iterations,\n            b, m, compute_error(b, m, points)))\n\n\nif __name__ == '__main__':\n    main()\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Starting gradient descent at b = 0, m = 0, error = 5671.844671124282\nRunning...\nAfter 10000 iterations b = 0.11558415090685024, m = 1.3769012288001614, error = 212.26220312358794<\/code><\/pre>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-alpha monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Trending<\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\">[Solved] runtimeerror: cuda error: invalid device ordinal<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-frequently-asked-questions-related-to-mean-squared-error-in-python\"><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions_Related_to_Mean_Squared_Error_in_Python\"><\/span>Frequently Asked Questions Related to Mean Squared Error in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1628784337983\"><strong class=\"schema-faq-question\">1. What is the pip command to install numpy?<\/strong> <p class=\"schema-faq-answer\">pip install numpy<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1628784367299\"><strong class=\"schema-faq-question\">2. What is the pip command to install sklearn.metrices library?<\/strong> <p class=\"schema-faq-answer\">pip install sklearn<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1628784410599\"><strong class=\"schema-faq-question\">3. What is the expansion of MSE?<\/strong> <p class=\"schema-faq-answer\">The expansion of MSE is Mean Squared Error.<\/p> <\/div> <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learned about the mean squared error. It is effortless to calculate. This is useful for loss function for least squares regression. The formula for the MSE is easy to memorize. We hope this article is handy and easy to understand.<\/p>\n\n\n<div class=\"monsterinsights-widget-popular-posts monsterinsights-widget-popular-posts-charlie monsterinsights-popular-posts-styled monsterinsights-widget-popular-posts-columns-1\"><h2 class=\"monsterinsights-widget-popular-posts-widget-title\"><span class=\"ez-toc-section\" id=\"Recommended_Python_Articles\"><\/span>Recommended Python Articles<span class=\"ez-toc-section-end\"><\/span><\/h2><ul class=\"monsterinsights-widget-popular-posts-list\"><li style=\"background-color:#EEEEEE;border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"color:#000000;font-size:18px;\">[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><li style=\"background-color:#EEEEEE;border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"color:#000000;font-size:18px;\">[Fixed] nameerror: name Unicode is not defined<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><li style=\"background-color:#EEEEEE;border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"color:#000000;font-size:18px;\">[Solved] runtimeerror: cuda error: invalid device ordinal<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><li style=\"background-color:#EEEEEE;border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-type-numpy-ndarray-doesnt-define-__round__-method\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"color:#000000;font-size:18px;\">[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><\/ul><\/div><p><\/p>","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to learn how to calculate the mean squared error in python? We are using two python libraries to calculate &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How To Calculate Mean Squared Error In Python\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#more-15528\" aria-label=\"More on How To Calculate Mean Squared Error In Python\">Read more<\/a><\/p>\n","protected":false},"author":22,"featured_media":15602,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[4418,4420,4419],"class_list":["post-15528","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-mean-squared-error-python-numpy","tag-root-mean-squared-error-two-vectors-python","tag-what-is-a-good-mean-squared-error-python","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How To Calculate Mean Squared Error In Python - Python Pool<\/title>\n<meta name=\"description\" content=\"In this article, we are going to learn how to calculate mean squared error in python? We are using two python libraries to calculate\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Calculate Mean Squared Error In Python\" \/>\n<meta property=\"og:description\" content=\"In this article, we are going to learn how to calculate the mean squared error in python? We are using two python libraries to calculate the mean squared\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-13T13:19:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-13T13:19:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/How-To-Calculate-Mean-Squared-Error-In-Python.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Priyalakshmi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Priyalakshmi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/\"},\"author\":{\"name\":\"Priyalakshmi\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/83e6e22a6c461cc1a8445bb473db2f20\"},\"headline\":\"How To Calculate Mean Squared Error In Python\",\"datePublished\":\"2021-08-13T13:19:31+00:00\",\"dateModified\":\"2021-08-13T13:19:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/\"},\"wordCount\":731,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/How-To-Calculate-Mean-Squared-Error-In-Python.webp\",\"keywords\":[\"mean squared error python numpy\",\"root mean squared error two vectors python\",\"what is a good mean squared error python\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/\",\"url\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/\",\"name\":\"How To Calculate Mean Squared Error In Python - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/How-To-Calculate-Mean-Squared-Error-In-Python.webp\",\"datePublished\":\"2021-08-13T13:19:31+00:00\",\"dateModified\":\"2021-08-13T13:19:35+00:00\",\"description\":\"In this article, we are going to learn how to calculate mean squared error in python? We are using two python libraries to calculate\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784337983\"},{\"@id\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784367299\"},{\"@id\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784410599\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/How-To-Calculate-Mean-Squared-Error-In-Python.webp\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/How-To-Calculate-Mean-Squared-Error-In-Python.webp\",\"width\":1200,\"height\":628,\"caption\":\"Mean Squared Error Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How To Calculate Mean Squared Error In Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/83e6e22a6c461cc1a8445bb473db2f20\",\"name\":\"Priyalakshmi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g\",\"caption\":\"Priyalakshmi\"}},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784337983\",\"position\":1,\"url\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784337983\",\"name\":\"1. What is the pip command to install numpy?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"pip install numpy\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784367299\",\"position\":2,\"url\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784367299\",\"name\":\"2. What is the pip command to install sklearn.metrices library?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"pip install sklearn\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784410599\",\"position\":3,\"url\":\"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784410599\",\"name\":\"3. What is the expansion of MSE?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"The expansion of MSE is Mean Squared Error.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How To Calculate Mean Squared Error In Python - Python Pool","description":"In this article, we are going to learn how to calculate mean squared error in python? We are using two python libraries to calculate","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/","og_locale":"en_US","og_type":"article","og_title":"How To Calculate Mean Squared Error In Python","og_description":"In this article, we are going to learn how to calculate the mean squared error in python? We are using two python libraries to calculate the mean squared","og_url":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/","og_site_name":"Python Pool","article_published_time":"2021-08-13T13:19:31+00:00","article_modified_time":"2021-08-13T13:19:35+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/How-To-Calculate-Mean-Squared-Error-In-Python.webp","type":"image\/webp"}],"author":"Priyalakshmi","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Priyalakshmi","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/"},"author":{"name":"Priyalakshmi","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/83e6e22a6c461cc1a8445bb473db2f20"},"headline":"How To Calculate Mean Squared Error In Python","datePublished":"2021-08-13T13:19:31+00:00","dateModified":"2021-08-13T13:19:35+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/"},"wordCount":731,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/How-To-Calculate-Mean-Squared-Error-In-Python.webp","keywords":["mean squared error python numpy","root mean squared error two vectors python","what is a good mean squared error python"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/mean-squared-error-python\/#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/","url":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/","name":"How To Calculate Mean Squared Error In Python - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/How-To-Calculate-Mean-Squared-Error-In-Python.webp","datePublished":"2021-08-13T13:19:31+00:00","dateModified":"2021-08-13T13:19:35+00:00","description":"In this article, we are going to learn how to calculate mean squared error in python? We are using two python libraries to calculate","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784337983"},{"@id":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784367299"},{"@id":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784410599"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/mean-squared-error-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/How-To-Calculate-Mean-Squared-Error-In-Python.webp","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/08\/How-To-Calculate-Mean-Squared-Error-In-Python.webp","width":1200,"height":628,"caption":"Mean Squared Error Python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"How To Calculate Mean Squared Error In Python"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/83e6e22a6c461cc1a8445bb473db2f20","name":"Priyalakshmi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g","caption":"Priyalakshmi"}},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784337983","position":1,"url":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784337983","name":"1. What is the pip command to install numpy?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"pip install numpy","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784367299","position":2,"url":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784367299","name":"2. What is the pip command to install sklearn.metrices library?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"pip install sklearn","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784410599","position":3,"url":"https:\/\/www.pythonpool.com\/mean-squared-error-python\/#faq-question-1628784410599","name":"3. What is the expansion of MSE?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"The expansion of MSE is Mean Squared Error.","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/15528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=15528"}],"version-history":[{"count":30,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/15528\/revisions"}],"predecessor-version":[{"id":15612,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/15528\/revisions\/15612"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/15602"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=15528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=15528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=15528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}