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'))中系数来说,而残差因为只有一个所以直接获取就可以。

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

此星光明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值