Main Content
Syntax
Description
example
err
= immse(X
,Y
)
calculates the mean-squared error (MSE) between the arrays X
and Y
. A lower MSE value indicates greater similarity between
X
and Y
.
Examples
collapse all
Calculate Mean-Squared Error in Noisy Image
Read image and display it.
ref = imread('pout.tif');
imshow(ref)
Create another image by adding noise to a copy of the reference image.
A = imnoise(ref,'salt & pepper', 0.02);
imshow(A)
Calculate mean-squared error between the two images.
err = immse(A, ref);
fprintf('n The mean-squared error is %0.4fn', err);
The mean-squared error is 353.7631
Input Arguments
collapse all
X
— Input array
numeric array
Input array, specified as a numeric array of any dimension.
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
Y
— Input array
numeric array
Input array, specified as a numeric array of the same size and data type as
X
.
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
Output Arguments
collapse all
err
— Mean-squared error
positive number
Mean-squared error, returned as a positive number. The data type of err
is double
unless the input arguments are of data type
single
, in which case err
is of
data type single
Data Types: single
| double
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
immse
supports the generation of C
code (requires MATLAB®
Coder™). For more information, see Code Generation for Image Processing.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Version History
Introduced in R2014b
Root-mean-square error between arrays
Since R2022b
Syntax
Description
example
E
= rmse(F
,A
)
returns the root-mean-square
error (RMSE) between the forecast (predicted) array F
and the
actual (observed) array A
.
-
F
andA
must either be the same size or
have sizes that are compatible. -
If
F
andA
are vectors of the same size,
thenE
is a scalar. -
If
F-A
is a matrix, thenE
is a row vector
containing the RMSE for each column. -
If
F
andA
are multidimensional arrays,
thenE
contains the RMSE computed along the first array dimension
of size greater than 1, with elements treated as vectors. The size of
E
in this dimension is 1, while the sizes of all other
dimensions are the same as inF-A
.
E
= rmse(F
,A
,"all")
returns the RMSE of all elements in F
and A
.
example
E
= rmse(F
,A
,dim
)
operates along dimension dim
. For example, if F
and
A
are matrices, then rmse(F,A,2)
operates on the
elements in each row and returns a column vector containing the RMSE of each row.
example
E
= rmse(F
,A
,vecdim
)
operates along the dimensions specified in the vector vecdim
. For
example, if F
and A
are matrices, then
rmse(F,A,[1 2])
operates on all the elements in F
and A
because every element of a matrix is contained in the array slice
defined by dimensions 1 and 2.
example
E
= rmse(___,nanflag
)
specifies whether to include or omit NaN
values in F
and A
for any of the previous syntaxes. For example,
rmse(F,A,"omitnan")
ignores NaN
values when
computing the RMSE. By default, rmse
includes NaN
values.
example
E
= rmse(___,Weight=W
)
specifies a weighting scheme W
and returns the weighted RMSE. If
W
is a vector, its length must equal the length of the operating
dimension. If W
is a matrix or multidimensional array, it must have the
same dimensions as F
, A
, or F-A
.
You cannot specify a weighting scheme if you specify vecdim
or
"all"
.
Examples
collapse all
RMSE of Two Forecasts
Create two column vectors of forecast (predicted) data and one column vector of actual (observed) data.
F1 = [1; 10; 9]; F2 = [2; 5; 10]; A = [1; 9; 10];
Compute the RMSE between each forecast and the actual data.
Alternatively, create a matrix containing both forecasts and compute the RMSE between each forecast and the actual data in one command.
The first element of E
is the RMSE between the first forecast column and the actual data. The second element of E
is the RMSE between the second forecast column and the actual data.
RMSE of Matrix Rows
Create a matrix of forecast data and a matrix of actual data.
F = [17 19; 1 6; 16 15]; A = [17 25; 3 4; 16 13];
Compute the RMSE between the forecast and the actual data across each row by specifying the operating dimension as 2. The smallest RMSE corresponds to the RMSE between the third rows of the forecast data and actual data.
E = 3×1
4.2426
2.0000
1.4142
RMSE of Array Pages
Create a 3-D array with pages containing forecast data and a matrix of actual data.
F(:,:,1) = [2 4; -2 1]; F(:,:,2) = [4 4; 8 -3]; A = [6 7; 1 4];
Compute the RMSE between the predicted data in each page of the forecast array and the actual data matrix by specifying a vector of operating dimensions 1 and 2.
E = E(:,:,1) = 3.2787 E(:,:,2) = 5.2678
The first page of E
contains the RMSE between the first page of F
and the matrix A
. The second page of E
contains the RMSE between the second page of F
and the matrix A
.
RMSE Excluding Missing Values
Create a matrix of forecast data and a matrix of actual data containing NaN
values.
F = [17 19 3; 6 16 NaN]; A = [17 25 NaN; 4 16 NaN];
Compute the RMSE between the forecast and the actual data, ignoring NaN
values. For columns that contain all NaN
values in F
or A
, the RMSE is NaN
.
E = 1×3
1.4142 4.2426 NaN
Specify RMSE Weight Vector
Create a forecast column vector and an actual column vector.
F = [2; 10; 13]; A = [1; 9; 10];
Compute the RMSE between the forecast and actual data according to a weighting scheme specified by W
.
W = [0.5; 0.25; 0.25]; E = rmse(F,A,Weight=W)
Input Arguments
collapse all
F
— Forecast array
vector | matrix | multidimensional array
Forecast or predicted array, specified as a vector, matrix, or multidimensional
array.
Inputs F
and A
must either be the same size or
have sizes that are compatible. For example, F
is an
m
-by-n
matrix and
A
is a 1-by-n
row vector. For more
information, see Compatible Array Sizes for Basic Operations.
Data Types: single
| double
Complex Number Support: Yes
A
— Actual array
vector | matrix | multidimensional array
Actual or observed array, specified as a vector, matrix, or multidimensional
array.
Inputs F
and A
must either be the same size or
have sizes that are compatible. For example, F
is an
m
-by-n
matrix and
A
is a 1-by-n
row vector. For more
information, see Compatible Array Sizes for Basic Operations.
Data Types: single
| double
Complex Number Support: Yes
dim
— Dimension to operate along
positive integer scalar
Dimension
to operate along, specified as a positive integer scalar. If you do not specify the dimension,
then the default is the first array dimension of size greater than 1.
The size of E
in the operating dimension is 1. All other
dimensions of E
have the same size as the result of
F-A
.
For example, consider four forecasts in a 3-by-4 matrix, F
, and
actual data in a 3-by-1 column vector, A
:
-
rmse(F,A,1)
computes the RMSE of the elements in each
column and returns a 1-by-4 row vector.The size of
E
in the operating dimension is 1. The
difference ofF
andA
is a 3-by-4 matrix.
The size ofE
in the nonoperating dimension is the same as the
second dimension ofF-A
, which is 4. The overall size of
E
becomes 1-by-4. -
rmse(F,A,2)
computes the RMSE of the elements in each row
and returns a 3-by-1 column vector.The size of
E
in the operating dimension is 1. The
difference ofF
andA
is a 3-by-4 matrix.
The size ofE
in the nonoperating dimension is the same as the
first dimension ofF-A
, which is 3. The overall size of
E
becomes 3-by-1.
vecdim
— Vector of dimensions to operate along
vector of positive integers
Vector of dimensions to operate along, specified as a vector of positive integers.
Each element represents a dimension of the input arrays. The size of
E
in the operating dimensions is 1. All other dimensions of
E
have the same size as the result of
F-A
.
For example, consider forecasts in a 2-by-3-by-3 array, F
, and
actual data in a 1-by-3 row vector, A
. rmse(F,A,[1
computes the RMSE over each page of
2])F
and returns a
1-by-1-by-3 array. The size of E
in the operating dimensions is 1.
The difference of F
and A
is a 2-by-3-by-3 array.
The size of E
in the nonoperating dimension is the same as the third
dimension of F-A
, which is 3.
nanflag
— Missing value condition
"includemissing"
(default) | "includenan"
| "omitmissing"
| "omitnan"
Missing value condition, specified as one of these values:
-
"includemissing"
or"includenan"
—
IncludeNaN
values in the input arrays when computing the RMSE.
If any element in the operating dimension isNaN
, then the
corresponding element inE
isNaN
.
"includemissing"
and"includenan"
have the
same behavior. -
"omitmissing"
or"omitnan"
— Ignore
NaN
values in the input arrays when computing the RMSE. If all
elements in the operating dimension areNaN
in
F
,A
, orW
, then the
corresponding element inE
isNaN
.
"omitmissing"
and"omitnan"
have the same
behavior.
W
— Weighting scheme
vector | matrix | multidimensional array
Weighting scheme, specified as a vector, matrix, or multidimensional array. The
elements of W
must be nonnegative.
If W
is a vector, it must have the same length as the operating
dimension. If W
is a matrix or multidimensional array, it must have
the same dimensions as F
, A
, or
F-A
.
You cannot specify this argument if you specify vecdim
or
"all"
.
Data Types: single
| double
More About
collapse all
Root-Mean-Square Error
For a forecast array F and actual array
A made up of n scalar observations, the
root-mean-square error is defined as
with the summation performed along the specified dimension.
Weighted Root-Mean-Square Error
For a forecast array F and actual array
A made up of n scalar observations and weighting
scheme W, the weighted root-mean-square error is defined as
with the summation performed along the specified dimension.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
This function fully supports tall arrays. For
more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more
information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2022b
expand all
R2023a: Specify missing value condition
Include or omit missing values in the input arrays when computing the RMSE by using the
"includemissing"
or "omitmissing"
options. These
options have the same behavior as the "includenan"
and
"omitnan"
options, respectively.
R2023a: Improved performance with small group size
The rmse
function shows improved performance when computing over a
real vector when the operating dimension is not specified. The function determines the
default operating dimension more quickly in R2023a than in R2022b.
For example, this code computes the root-mean-square error along the default vector
dimension. The code is about 2x faster than in the previous release.
function timingRmse F = rand(10,1); A = rand(10,1); for i = 1:8e5 rmse(F,A); end end
The approximate execution times are:
R2022b: 4.12 s
R2023a: 2.07 s
The code was timed on a Windows® 10, Intel®
Xeon® CPU E5-1650 v4 @ 3.60 GHz test system using the timeit
function.
R2023a: Code generation support
Generate C or C++ code for the rmse
function.
Среднеквадратическая ошибка
Синтаксис
Описание
пример
вычисляет среднеквадратическую ошибку (MSE) между массивами err
= immse(X
,Y
)X
и Y
.
Примеры
свернуть все
Вычислите среднеквадратическую ошибку в шумном изображении
Считайте изображение и отобразите его.
ref = imread('pout.tif');
imshow(ref)
Создайте другое изображение путем добавления шума в копию ссылочного изображения.
A = imnoise(ref,'salt & pepper', 0.02);
imshow(A)
Вычислите среднеквадратическую ошибку между двумя изображениями.
err = immse(A, ref);
fprintf('n The mean-squared error is %0.4fn', err);
The mean-squared error is 353.7631
Входные параметры
свернуть все
X
— Входной массив
числовой массив
Входной массив в виде числового массива любой размерности.
Типы данных: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
Y
— Входной массив
числовой массив
Входной массив в виде числового массива, одного размера и тип данных как X
.
Типы данных: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
Выходные аргументы
свернуть все
err
— Среднеквадратическая ошибка
положительное число
Среднеквадратическая ошибка, возвращенная как положительное число. Тип данных err
double
если входные параметры не имеют типа данных single
, в этом случае err
имеет тип данных single
Типы данных: single
| double
Расширенные возможности
Генерация кода C/C++
Генерация кода C и C++ с помощью MATLAB® Coder™.
immse
поддерживает генерацию кода С (требует MATLAB® Coder™). Для получения дополнительной информации смотрите Генерацию кода для Обработки изображений.
Генерация кода графического процессора
Сгенерируйте код CUDA® для NVIDIA® графические процессоры с помощью GPU Coder™.
Введенный в R2014b
17 авг. 2022 г.
читать 1 мин
Одной из наиболее распространенных метрик, используемых для измерения точности прогноза модели, является MSE , что означает среднеквадратичную ошибку .
Он рассчитывается как:
MSE = (1/n) * Σ(факт – прогноз) 2
куда:
- Σ — причудливый символ, означающий «сумма».
- n – размер выборки
- фактический – фактическое значение данных
- прогноз – прогнозируемое значение данных
Чем ниже значение MSE, тем лучше модель способна точно прогнозировать значения.
Чтобы вычислить MSE в MATLAB, мы можем использовать функцию mse(X, Y) .
В следующем примере показано, как использовать эту функцию на практике.
Пример: как рассчитать MSE в MATLAB
Предположим, у нас есть следующие два массива в MATLAB, которые показывают фактические значения и прогнозируемые значения для некоторой модели:
%create array of actual values and array of predicted values
actual = [34 37 44 47 48 48 46 43 32 27 26 24];
predicted = [37 40 46 44 46 50 45 44 34 30 22 23];
Мы можем использовать функцию mse(X, Y) для вычисления среднеквадратичной ошибки (MSE) между двумя массивами:
%calculate MSE between actual values and predicted values
mse(actual, predicted)
ans = 5.9167
Среднеквадратическая ошибка (MSE) этой модели оказывается равной 5,917 .
Мы интерпретируем это как означающее, что среднеквадратическая разница между предсказанными значениями и фактическими значениями составляет 5,917 .
Мы можем сравнить это значение с MSE, полученным другими моделями, чтобы определить, какая модель является «лучшей».
Модель с наименьшим MSE — это модель, которая лучше всего способна прогнозировать фактические значения набора данных.
Дополнительные ресурсы
В следующих руководствах объясняется, как рассчитать среднеквадратичную ошибку с помощью другого статистического программного обеспечения:
Как рассчитать среднеквадратичную ошибку (MSE) в Excel
Как рассчитать среднеквадратичную ошибку (MSE) в Python
Как рассчитать среднеквадратичную ошибку (MSE) в R
I don’t know whether this is possible or not but let me explain my question
Imagine that I have the below array
errors=[e1,e2,e3];
Now what I want to calculate is below
MSE=1/(array_lenght)*[e1^2+e2^2+e3^2];
I can make this with a loop but I wonder if there is any quick way.
Bart
19.5k7 gold badges68 silver badges77 bronze badges
asked Nov 8, 2012 at 21:36
Furkan GözükaraFurkan Gözükara
22.7k76 gold badges203 silver badges333 bronze badges
This finds the mean of the squared errors:
MSE = mean(errors.^2)
Each element is squared separately, and then the mean of the resulting vector is found.
answered Nov 8, 2012 at 21:38
2
sum(errors.^2) / numel(errors)
answered Nov 8, 2012 at 21:38
JohnJohn
5,7153 gold badges46 silver badges62 bronze badges
0
Raising powers and adding can be done together instead of sequentially:
MSE = (errors*errors') / numel(errors)
answered Apr 9, 2015 at 11:14
tashuhkatashuhka
4,9884 gold badges45 silver badges64 bronze badges