python - R 中的 scikit-learn 与网状结构

标签 python r scikit-learn reticulate

我正在尝试使用R中的reticulate包。有个好的intro here ,但我没有取得太大进展。假设我想做某事 simple like build a linear model with scikit-learn 。 (是的,我知道 R 可以完美地做到这一点,但我现在只是测试一些东西......)


library(reticulate)

# import modules
pd <- import("pandas")
np <- import("numpy")
skl_lr <- import("sklearn.linear_model")

# set up variables and response
x <- mtcars[, -1]
y <- mtcars[, 1]

# convert to python objects
pyx <- r_to_py(x)
pyy <- r_to_py(y)

# create model
skl_lr$LinearRegression$fit(pyx, pyy)

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: fit() missing 1 required positional argument: 'y'

显式传递参数不起作用。

skl_lr$LinearRegression$fit(X = pyx, y = pyy)

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: fit() missing 1 required positional argument: 'self'

有什么想法吗?

最佳答案

就像在普通的 Python/Scikit 中一样,您需要先初始化模型对象,然后才能拟合它。

lr <- skl_lr$LinearRegression()
lr$fit(pyx, pyy)

lr$coef_
# [1] -0.11144048  0.01333524 -0.02148212  0.78711097 -3.71530393  0.82104075  0.31776281
# [8]  2.52022689  0.65541302 -0.19941925

关于python - R 中的 scikit-learn 与网状结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60235726/

相关文章:

python - pyserial:没有名为工具的模块

r - 在 R 中循环并添加到计数器

python - ValueError : Input contains NaN, 无穷大或值对于 dtype ('float32' 太大)。为什么?

python - 在 Python 中处理项目脚本中的路径/可执行文件的最佳实践(例如 Django 的 manage.py 或 fabric)

python - 使用请求在 Python 中搜索网站时出现 502 错误

python - 在 Pandas 中添加日期

python - 如何在 Python 中集成 beta 发行版

r - 调整网格、相同大小的 ggplot2 图形之间的空间

python - One-Hot-Encode 分类变量并同时缩放连续变量

python - 如果相关性大于 0.75,则从 Pandas 的数据框中删除该列