【发布时间】:2018-11-11 23:24:15
【问题描述】:
我有一些代码(大部分不是我的原始代码),我在本地 PC Anaconda Jupyter Notebook 环境中运行。我需要扩大处理规模,因此我正在研究 Azure Databricks。有一段代码运行 Python 循环,但使用了 R 库(stats),然后通过 R 模型(tbats)传递数据。因此,一个 Jupyter Notebook 单元运行 python 和 R 代码。这也可以在 Azure Databricks Notebooks 中完成吗?我只找到了可以让您在不同单元格之间更改语言的文档。
在上一个单元格中我有:
%r libarary(stats)
因此库统计信息被导入(与其他 R 库一起)。但是,当我运行下面的代码时,我得到了
NameError: name 'stats' 未定义
我想知道 Databricks 是否希望您以这种方式告诉单元格您正在使用的语言(例如 %r、%python 等)。
我的 Python 代码:
for customerid, dataForCustomer in original.groupby(by=['customer_id']):
startYear = dataForCustomer.head(1).iloc[0].yr
startMonth = dataForCustomer.head(1).iloc[0].mnth
endYear = dataForCustomer.tail(1).iloc[0].yr
endMonth = dataForCustomer.tail(1).iloc[0].mnth
#Creating a time series object
customerTS = stats.ts(dataForCustomer.usage.astype(int),
start=base.c(startYear,startMonth),
end=base.c(endYear, endMonth),
frequency=12)
r.assign('customerTS', customerTS)
##Here comes the R code piece
try:
seasonal = r('''
fit<-tbats(customerTS, seasonal.periods = 12,
use.parallel = TRUE)
fit$seasonal
''')
except:
seasonal = 1
# APPEND DICTIONARY TO LIST (NOT DATA FRAME)
df_list.append({'customer_id': customerid, 'seasonal': seasonal})
print(f' {customerid} | {seasonal} ')
seasonal_output = pa.DataFrame(df_list)
【问题讨论】:
-
我认为您只能在 Jupyter / Databricks 笔记本中将语言从一个单元更改为另一个单元,而不能在一个单元中混合语言。您可以尝试在 Databricks 集群上安装 RStudio Open Source,并在 R 笔记本中根据需要混合 Python 和 R,这是通过 reticulate R 包支持的。
标签: python r azure jupyter-notebook databricks