-
Notifications
You must be signed in to change notification settings - Fork 50
/
linear_regression.py
36 lines (27 loc) · 1.13 KB
/
linear_regression.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
import ee
import geemap
# Create a map centered at (lat, lon).
Map = geemap.Map(center=[40, -100], zoom=4)
# 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 to avoid very small slopes
# # in the linear regression output.
# return image.addBands(image.metadata('system:time_start').divide(1e18))
# }
# Load the input image 'collection': projected climate data.
collection = ee.ImageCollection('NASA/NEX-DCP30_ENSEMBLE_STATS') \
.filter(ee.Filter.eq('scenario', 'rcp85')) \
.filterDate(ee.Date('2006-01-01'), ee.Date('2050-01-01')) \
.map(createTimeBand)
# Reduce the collection with the linear fit reducer.
# Independent variable are followed by dependent variables.
linearFit = collection.select(['system:time_start', 'pr_mean']) \
.reduce(ee.Reducer.linearFit())
# Display the results.
Map.setCenter(-100.11, 40.38, 5)
Map.addLayer(linearFit,
{'min': 0, 'max': [-0.9, 8e-5, 1], 'bands': ['scale', 'offset', 'scale']}, 'fit')
# Display the map.
Map