NameError с использованием LinearRegression() с API Python

Я пытаюсь запустить некоторые регрессии в Earth Engine, используя Cloud Datalab. Когда я копирую код из этого руководства по регрессии (модифицируя его для Python), я получаю эта ошибка:

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. Используется ли LinearRegression по-разному в API Python?


person Andrew    schedule 26.06.2017    source источник
comment
Вы пытались установить numX и numY между кавычками?   -  person Val    schedule 27.06.2017
comment
Да, я сделал - это дает другую ошибку: EEException: Invalid argument specified for ee.Number(): {'numX': 2, 'numY': 2}. Однако приведенное ниже решение Родриго работает.   -  person Andrew    schedule 04.07.2017


Ответы (1)


Вы можете передавать аргументы как аргументы ключевого слова python, например:

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()

Результаты не проверял, но ошибок нет, образы создаются.

person Rodrigo E. Principe    schedule 03.07.2017
comment
Спасибо! Работал хорошо. - person Andrew; 04.07.2017
comment
Привет, Эндрю, если вы используете API Python, вы можете взглянуть на github.com/gee- сообщество/gee_tools - person Rodrigo E. Principe; 12.07.2017