-
Notifications
You must be signed in to change notification settings - Fork 264
/
linear_regression_more.py
69 lines (56 loc) · 2.24 KB
/
linear_regression_more.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import ee
from ee_plugin import Map
# This function adds a time band to the image.
def createTimeBand(image):
return image.addBands(image.metadata('system:time_start').divide(1e18))
# createTimeBand = function(image) {
# # Scale milliseconds by a large constant.
# return image.addBands(image.metadata('system:time_start').divide(1e18))
# }
# This function adds a constant band to the image.
def createConstantBand(image):
return ee.Image(1).addBands(image)
# createConstantBand = function(image) {
# return ee.Image(1).addBands(image)
# }
# Load the input image 'collection': projected climate data.
collection = ee.ImageCollection('NASA/NEX-DCP30_ENSEMBLE_STATS') \
.filterDate(ee.Date('2006-01-01'), ee.Date('2099-01-01')) \
.filter(ee.Filter.eq('scenario', 'rcp85')) \
.map(createTimeBand) \
.map(createConstantBand) \
.select(['constant', 'system:time_start', 'pr_mean', 'tasmax_mean'])
# Compute ordinary least squares regression coefficients.
linearRegression = collection.reduce(
ee.Reducer.linearRegression(**{
'numX': 2,
'numY': 2
}))
# Compute robust linear regression coefficients.
robustLinearRegression = collection.reduce(
ee.Reducer.robustLinearRegression(**{
'numX': 2,
'numY': 2
}))
# The results are array images that must be flattened for display.
# These lists label the information along each axis of the arrays.
bandNames = [['constant', 'time'], # 0-axis variation.
['precip', 'temp']] # 1-axis variation.
# Flatten the array images to get multi-band images according to the labels.
lrImage = linearRegression.select(['coefficients']).arrayFlatten(bandNames)
rlrImage = robustLinearRegression.select(['coefficients']).arrayFlatten(bandNames)
# Display the OLS results.
Map.setCenter(-100.11, 40.38, 5)
Map.addLayer(lrImage,
{'min': 0, 'max': [-0.9, 8e-5, 1], 'bands': ['time_precip', 'constant_precip', 'time_precip']}, 'OLS')
# Compare the results at a specific point:
print('OLS estimates:', lrImage.reduceRegion(**{
'reducer': ee.Reducer.first(),
'geometry': ee.Geometry.Point([-96.0, 41.0]),
'scale': 1000
}).getInfo())
print('Robust estimates:', rlrImage.reduceRegion(**{
'reducer': ee.Reducer.first(),
'geometry': ee.Geometry.Point([-96.0, 41.0]),
'scale': 1000
}).getInfo())