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>.



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 reduceRegions instead. This will give you a set of coefficients and residuals rather than an image.
var linear = albedo.select(['longitude', 'B4'])
  • 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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