0

I am having a problem with the Linear Regression function (code attached) when I try to regress latitude or longitude against B4 reflectance for a S2 image. I am getting the following error:

ReduceBands reducer must have one input: <Reducer>.

https://code.earthengine.google.com/ef8f966b9ac39b308198968d45af58be

1 Answer 1

1

The problem is that the way you are reducing, the inputs to the reducer will be the values of different bands in a pixel. In case of linear regression, it does not work as linear regression is trying to fit a linear equation by minimizing the rmse but one pixel is essentially just a single feature. Think of it this way, to do linear regression you need (x1,y1),(x2,y2),..... not just (x1,y1). So one of the following two is solution depending what you were trying to achieve.

  • If you were trying to get a single regression throughout the image, you should use reduceRegion instead. This will give you a set of coefficients and residuals rather than an image.
var linear = albedo.select(['longitude', 'B4'])
  .reduceRegion(ee.Reducer.linearRegression(1,1),image.get('system:footprint'),10);
  • If you were trying to get a regression going in each pixel, you'd need to reduce over an image collection rather than an image like in the example in gee docs. In this case, the way you are doing should work. You'd just need to change albedo to an image collection.
2
  • Hi Nishanta, could you clarify in your answer. You say reduceRegions(), yet your code has reduceRegion(). Commented Aug 25, 2019 at 15:11
  • @SeanRoulet Sorry, that was a typing mistake and I intended to write reduceRegion() - now corrected. What this does is within the regions there are many pixels and each pixel has a (longitude, B4) value which is essentially a (x,y) pair if you think of it in terms of coordinates in a graph. With many such data points we can do linear regression. But with reduce() GEE tries to reduce over a stack of pixels. So, linear regression only gets one input (longitude,B4) which it can not work with. Commented Aug 26, 2019 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.