Google Earth Engine(GEE)——在线计算列表二维ee.List对象为线性回归方程计算slope和残差

简介: Google Earth Engine(GEE)——在线计算列表二维ee.List对象为线性回归方程计算slope和残差

二维ee.List对象的可以作为回归缩减器的输入。下面的例子提供了简单的证明;自变量是因变量的副本,产生等于 0 的 y 截距和等于 1 的斜率。


注意:减少的结果ee.List是一个对象。将其强制转换为 an ee.Dictionary以使访问属性更容易。


注意:行和列之间的长度必须相等。使用null表示丢失的数据条目。

linearFit()代码:

// 定义一个列表列表,其中列代表变量。
// 第一列是自变量,第二个是因变量。
var listsVarColumns = ee.List([
  [1, 1],
  [2, 2],
  [3, 3],
  [4, 4],
  [5, 5]
]);
// 计算线性函数的最小二乘估计。请注意,一个返回对象;将其转换为 ee.Dictionary 以访问
系数更容易。很简单的操作就是根据在进行线性回归之前加入 ee.Dictionary整个程序更像是在外面进行加了一层包装
var linearFit = ee.Dictionary(listsVarColumns.reduce(ee.Reducer.linearFit()));
// 检查结果
print(linearFit);
print('y-intercept:', linearFit.get('offset'));
print('Slope:', linearFit.get('scale'));

结果很明显,因为我们取得就是相同的数,所以:

如果变量由表示,则通过转换为ee.Array,转置它,然后转换回 来转置列表ee.List

函数:

ee.Array(values, pixelType)这个函数在这里只起到对于对象的转化

返回具有给定坐标的数组。
参数:

Returns an array with the given coordinates.


Arguments:

值(对象): 要转换的现有数组,或用于创建数组的任何深度的数字/数字列表/嵌套数字列表。对于嵌套列表,相同深度的所有内部数组必须具有相同的长度,并且数字只能出现在最深层.

values (Object):

An existing array to cast, or a number/list of numbers/nested list of numbers of any depth to create an array from. For nested lists, all inner arrays at the same depth must have the same length, and numbers may only be present at the deepest level.

pixelType (PixelType, default: null):

像素类型(像素类型,默认值:null):
values 参数中每个数字的类型。如果未提供像素类型,则将从“值”中的数字推断。如果“值”中没有任何数字,则必须提供此类型。

The type of each number in the values argument. If the pixel type is not provided, it will be inferred from the numbers in 'values'. If there aren't any numbers in 'values', this type must be provided.


Returns: Array

ee.Dictionary(dict)

构造一个新的字典。

Constructs a new Dictionary.


Arguments:

dict (ComputedObject|Object, optional):

要转换为字典的对象。此构造函数接受以下类型: 1) 另一个字典。 2) 键/值对列表。 3) 空或无参数(产生一个空字典)下面的例子就是2)

An object to convert to a dictionary. This constructor accepts the following types: 1) Another dictionary. 2) A list of key/value pairs. 3) A null or no argument (producing an empty dictionary)


Returns: Dictionary

代码:

//如果列表中的变量按行排列,则需要对其进行转置。
// 定义一个列表列表,其中行代表变量。
// 第一行是自变量,第二个是因变量。
var listsVarRows = ee.List([
  [1, 2, 3, 4, 5],
  [1, 2, 3, 4, 5]
]);
// 将 ee.List 转换为 ee.Array,转置它,然后转换回 ee.List。
var listsVarColumns = ee.Array(listsVarRows).transpose().toList();
// 计算线性函数的最小二乘估计。请注意,一个返回对象;
// 将其转换为 ee.Dictionary 以访问系数更容易。
var linearFit = ee.Dictionary(listsVarColumns.reduce(ee.Reducer.linearFit()));
// 进行结果显示
print(linearFit);
print('y-intercept:', linearFit.get('offset'));
print('Slope:', linearFit.get('scale'));


linearRegression()这里面一定会有一个常数自变量,也就是自变量有两个一个常数一个自己定义的数

的应用ee.Reducer.linearRegression()类似于上面的 linearFit()示例,不同之处在于包括了一个常数自变量。

// 定义一个列表列表,其中列代表变量。
// 第一列代表一个常数项,第二个是自变量,
// 第三个是一个因变量。
var listsVarColumns = ee.List([
  [1, 1, 1],
  [1, 2, 2],
  [1, 3, 3],
  [1, 4, 4],
  [1, 5, 5]
]);
// 计算普通最小二乘回归系数。 numX 是 2,因为有一个常数项和一个额外的自变量。 
//numY 为 1,因为只有一个因变量。这里有几个自变量X就为几,因变量一般为一个
//将结果对象强制转换为 ee.Dictionary 以便于访问属性。
var linearRegression = ee.Dictionary(
  listsVarColumns.reduce(ee.Reducer.linearRegression({
    numX: 2,
    numY: 1
})));
// 将系数数组转换为列表。
var coefList = ee.Array(linearRegression.get('coefficients')).toList();
// Extract the y-intercept and slope.
var b0 = ee.List(coefList.get(0)).get(0); // y-intercept
var b1 = ee.List(coefList.get(1)).get(0); // slope
// Extract the residuals.
var residuals = ee.Array(linearRegression.get('residuals')).toList().get(0);
// 检查结果。
print('OLS estimates', linearRegression);
print('y-intercept:', b0);
print('Slope:', b1);
print('Residuals:', residuals);

结果如图,一般情况下出来的默认就是0为截距,1为斜率这是对于(linearRegression.get('coefficients'))中系数来说,而残差因为只有一个所以直接获取就可以。

基本上操作还是比较简单,但是这个在云平台上用的还是较少,一般本地的软件都可以轻松实现!



相关文章
|
16天前
|
存储 JavaScript
DOM 节点列表长度(Node List Length)
名为 "title" 的元素节点,并存储在节点列表 x 中。通过 "length" 属性确定 x 的长度(即 "title" 节点总数)。利用 for 循环遍历整个列表,访问每个 "title" 节点的第一个子节点的值,并将其写入文档。
|
13天前
|
JavaScript
Vue3列表(List)
这段代码介绍了一个基于 Vue3 的列表组件库 `Vue Amazing UI`。该库提供了丰富的自定义选项,如边框、垂直布局、分割线、尺寸、加载状态等,并支持分页、自定义样式及操作项。组件通过插槽和属性实现高度灵活的内容展示与交互。
Vue3列表(List)
|
2天前
|
JavaScript
DOM 节点列表长度(Node List Length)
DOM 节点列表长度(Node List Length)
|
3天前
|
JavaScript
DOM 节点列表长度(Node List Length)
DOM 节点列表长度(Node List Length)
|
7天前
|
JavaScript
DOM 节点列表长度(Node List Length)
DOM 节点列表长度(Node List Length)
|
6天前
|
JavaScript
DOM 节点列表长度(Node List Length)
DOM 节点列表长度(Node List Length)
|
10天前
|
JavaScript
DOM 节点列表长度(Node List Length)
DOM 节点列表长度(Node List Length)
|
13天前
|
JavaScript
DOM 节点列表长度(Node List Length)
DOM 节点列表长度(Node List Length)
|
12天前
|
JavaScript
DOM 节点列表长度(Node List Length)
DOM 节点列表长度(Node List Length)
|
17天前
|
BI
【Azure Power BI】Power BI获取SharePoint List列表后,如何展开List/Table中的字段,以及使用逗号拼接为一个字符串
【Azure Power BI】Power BI获取SharePoint List列表后,如何展开List/Table中的字段,以及使用逗号拼接为一个字符串