前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Google Earth Engine(GEE)——在线计算列表二维ee.List对象为线性回归方程计算slope和残差

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

作者头像
此星光明
发布2024-02-02 08:02:28
1600
发布2024-02-02 08:02:28
举报

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

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

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

linearFit()代码:

代码语言:javascript
复制
// 定义一个列表列表,其中列代表变量。
// 第一列是自变量,第二个是因变量。
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)这个函数在这里只起到对于对象的转化

代码语言:javascript
复制
返回具有给定坐标的数组。

参数:

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):

代码语言:javascript
复制
像素类型(像素类型,默认值: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)

代码语言:javascript
复制
构造一个新的字典。

Constructs a new Dictionary.

Arguments:

dict (ComputedObject|Object, optional):

代码语言:javascript
复制
要转换为字典的对象。此构造函数接受以下类型: 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

代码:

代码语言:javascript
复制
//如果列表中的变量按行排列,则需要对其进行转置。
// 定义一个列表列表,其中行代表变量。
// 第一行是自变量,第二个是因变量。
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()示例,不同之处在于包括了一个常数自变量。

代码语言:javascript
复制
// 定义一个列表列表,其中列代表变量。
// 第一列代表一个常数项,第二个是自变量,
// 第三个是一个因变量。
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'))中系数来说,而残差因为只有一个所以直接获取就可以。

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-02-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Arguments:
  • Returns: Array
  • Arguments:
  • Returns: Dictionary
  • linearRegression()这里面一定会有一个常数自变量,也就是自变量有两个一个常数一个自己定义的数
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档