2017-06-26 20 views
3

我試圖在使用Cloud Datalab的地球引擎中運行一些迴歸。當我在this regression tutorial複製代碼(修改它的Python),我得到這個錯誤:使用LinearRegression的NameError()與Python API

NameErrorTraceback (most recent call last) 
<ipython-input-45-21c9bd377408> in <module>() 
    14 linearRegression = collection.reduce(
    15 ee.Reducer.linearRegression({ 
---> 16  numX: 2, 
    17  numY: 2 
    18 })) 

NameError: name 'numX' is not defined 

這似乎並沒有對其他功能的問題,同樣的代碼工作的JavaScript的API中。是否在Python API中使用了不同的LinearRegression?

+2

您是否嘗試在引號之間設置'numX'和'numY'? – Val

+0

是的,我做了 - 產生了一個不同的錯誤:'EEException:爲ee.Number()指定的無效參數:{'numX':2,'numY':2}'。但是,羅德里戈的解決方案在下面的工作。 – Andrew

回答

2

您可以傳遞參數作爲蟒關鍵字參數,就像這樣:

import ee 
ee.Initialize() 

# This function adds a time band to the image. 
def createTimeBand(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) 

# 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 the functions over the collection, to get constant and time bands. 
    .map(createTimeBand) 
    .map(createConstantBand) 
    # Select the predictors and the responses. 
    .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) 

# Print to check it works 
print rlrImage.getInfo(), lrImage.getInfo() 

我沒有檢查的結果,但沒有錯誤,是創建的映像。

+0

謝謝!工作得很好。 – Andrew

+0

嘿安德魯,如果你使用的是Python API,你可以看看https://github.com/gee-community/gee_tools –