google-cloud-datalab - NameError 使用 LinearRegression() 和 python API

标签 google-cloud-datalab google-earth-engine

我正在尝试使用 Cloud Datalab 在 Earth Engine 中运行一些回归。当我复制 this regression tutorial 中的代码时(为 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 在 python API 中的使用方式不同吗?

最佳答案

您可以将参数作为 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()

我没有检查结果,但没有错误,图像已创建。

关于google-cloud-datalab - NameError 使用 LinearRegression() 和 python API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44770027/

相关文章:

python-3.x - Google Datalab 从云存储中读取

python-2.7 - HTTPException:URL 的 SSL 证书无效和/或丢失:https://accounts.google.com/o/oauth2/token

python - 可以在谷歌地球引擎python api中上传shapefile吗?

javascript - 将看起来像数字但不是整数的内容转换为整数(Google Earth Engine)

google-earth-engine - 如何平滑谷歌地球引擎中的NDVI曲线

python - Google Earth Engine 上是否有按几何图形选项叠加或连接?

google-app-engine - 如何正确停止 Google Cloud Datalab

anaconda - 将python库添加到google datalab环境

github - 如何从谷歌云数据实验室将笔记本拉动和推送到github?

google-bigquery - gcp.bigquery 引用页面不再可用?