Python: 如何使用 Rpy2 从 R 导入 arules 包
Python: How to import arules package from R using Rpy2
我正在尝试将 python 与 R 中的一些不错的函数一起使用。特别是我想使用 read.transactions 函数,该函数位于 R (arules)[=15 中的一个包中=]
我做了以下步骤
1- 打开 Anaconda 和午餐 R studio
在 R 工作室中
2- install.packages('arules', dep = TRUE)
3- loadNamespace('arules')
4-.libPaths()
得到
[1] "D:/Anaconda3/Lib/site-packages/rpy2/R/win-library/3.4"
[2] "C:/Program Files/R/R-3.4.4/library"
现在我去jupyter notebook
在 Jupyter Notebook 中
import rpy2
import rpy2.robjects as RObjects
from rpy2.robjects.packages import importr
utils = importr("utils")
d = {'print.me': 'print_dot_me', 'print_me': 'print_uscore_me'}
try:
arules = importr('arules', robject_translations = d, lib_loc = "D:/Anaconda3/Lib/site-packages/rpy2/R/win-library/3.4")
except:
arules = importr('arules', robject_translations = d, lib_loc = "C:/Program Files/R/R-3.4.4/library")
结果是
---------------------------------------------------------------------------
RRuntimeError Traceback (most recent call last)
<ipython-input-3-5df30d28440c> in <module>()
3 try:
----> 4 arules = importr('arules', robject_translations = d, lib_loc = "D:/Anaconda3/Lib/site-packages/rpy2/R/win-library/3.4")
5 except:
~\Anaconda3\lib\site-packages\rpy2\robjects\packages.py in importr(name, lib_loc, robject_translations, signature_translation, suppress_messages, on_conflict, symbol_r2python, symbol_check_after, data)
452 _system_file(package = rname)):
--> 453 env = _get_namespace(rname)
454 version = _get_namespace_version(rname)[0]
RRuntimeError: Error in loadNamespace(name) : there is no package called 'arules'
During handling of the above exception, another exception occurred:
RRuntimeError Traceback (most recent call last)
<ipython-input-3-5df30d28440c> in <module>()
4 arules = importr('arules', robject_translations = d, lib_loc = "D:/Anaconda3/Lib/site-packages/rpy2/R/win-library/3.4")
5 except:
----> 6 arules = importr('arules', robject_translations = d, lib_loc = "C:/Program Files/R/R-3.4.4/library")
7
~\Anaconda3\lib\site-packages\rpy2\robjects\packages.py in importr(name, lib_loc, robject_translations, signature_translation, suppress_messages, on_conflict, symbol_r2python, symbol_check_after, data)
451 if _package_has_namespace(rname,
452 _system_file(package = rname)):
--> 453 env = _get_namespace(rname)
454 version = _get_namespace_version(rname)[0]
455 exported_names = set(_get_namespace_exports(rname))
RRuntimeError: Error in loadNamespace(name) : there is no package called 'arules'
无法将 R 包导入 Python
我对 DirichletReg
做了同样的事情并且成功了。不知道为什么。
谁能帮我解决这个问题?
现在到最后的发现,python中没有这样的东西,但是,有一个方法可以使用read.transactions
groceries <- read.transactions("groceries.csv", sep = ",")
> summary(groceries)
transactions as itemMatrix in sparse format with
9835 rows (elements/itemsets/transactions) and
169 columns (items) and a density of 0.02609146
Python Jupyter 笔记本
1) 将数据导入为
import requests
url = 'https://raw.githubusercontent.com/stedy/Machine-Learning-with-R-datasets/master/groceries.csv'
grocery_dataset = requests.get(url)
# Save string as txt file
f = open('grocery_dataset.txt','w')
f.write(grocery_dataset.text)
f.close()
2) 分离数据,随意调整
import csv
grocery_items = set()
with open("grocery_dataset.txt") as f:
reader = csv.reader(f, delimiter=",")
for i, line in enumerate(reader):
grocery_items.update(line)
output_list = list()
with open("grocery_dataset.txt") as f:
reader = csv.reader(f, delimiter=",")
for i, line in enumerate(reader):
row_val = {item:0 for item in grocery_items}
row_val.update({item:1 for item in line})
output_list.append(row_val)
4) 将其保存为 python
中的 Dataframe
import pandas as pd
grocery_df = pd.DataFrame(output_list)
因此
grocery_df.shape
会给
(9835, 169)
表示R
中summary(groceries)
的行和列
importr 在 R_HOME 目录中查找已安装的 R 包。我假设,arules 包没有添加到 R_HOME 的库文件夹中,而是添加到其他位置,比如 'C:\Users\User_name\Documents\R\win-library.x.x',这可能是导致问题的原因。
如果是这种情况,请从该特定位置复制 arules 文件夹并添加到 R_HOME 目录的库文件夹中。试试这个方法,看看能不能解决问题。
我正在尝试将 python 与 R 中的一些不错的函数一起使用。特别是我想使用 read.transactions 函数,该函数位于 R (arules)[=15 中的一个包中=]
我做了以下步骤
1- 打开 Anaconda 和午餐 R studio
在 R 工作室中
2- install.packages('arules', dep = TRUE) 3- loadNamespace('arules')
4-.libPaths()
得到
[1] "D:/Anaconda3/Lib/site-packages/rpy2/R/win-library/3.4"
[2] "C:/Program Files/R/R-3.4.4/library"
现在我去jupyter notebook
在 Jupyter Notebook 中
import rpy2
import rpy2.robjects as RObjects
from rpy2.robjects.packages import importr
utils = importr("utils")
d = {'print.me': 'print_dot_me', 'print_me': 'print_uscore_me'}
try:
arules = importr('arules', robject_translations = d, lib_loc = "D:/Anaconda3/Lib/site-packages/rpy2/R/win-library/3.4")
except:
arules = importr('arules', robject_translations = d, lib_loc = "C:/Program Files/R/R-3.4.4/library")
结果是
---------------------------------------------------------------------------
RRuntimeError Traceback (most recent call last)
<ipython-input-3-5df30d28440c> in <module>()
3 try:
----> 4 arules = importr('arules', robject_translations = d, lib_loc = "D:/Anaconda3/Lib/site-packages/rpy2/R/win-library/3.4")
5 except:
~\Anaconda3\lib\site-packages\rpy2\robjects\packages.py in importr(name, lib_loc, robject_translations, signature_translation, suppress_messages, on_conflict, symbol_r2python, symbol_check_after, data)
452 _system_file(package = rname)):
--> 453 env = _get_namespace(rname)
454 version = _get_namespace_version(rname)[0]
RRuntimeError: Error in loadNamespace(name) : there is no package called 'arules'
During handling of the above exception, another exception occurred:
RRuntimeError Traceback (most recent call last)
<ipython-input-3-5df30d28440c> in <module>()
4 arules = importr('arules', robject_translations = d, lib_loc = "D:/Anaconda3/Lib/site-packages/rpy2/R/win-library/3.4")
5 except:
----> 6 arules = importr('arules', robject_translations = d, lib_loc = "C:/Program Files/R/R-3.4.4/library")
7
~\Anaconda3\lib\site-packages\rpy2\robjects\packages.py in importr(name, lib_loc, robject_translations, signature_translation, suppress_messages, on_conflict, symbol_r2python, symbol_check_after, data)
451 if _package_has_namespace(rname,
452 _system_file(package = rname)):
--> 453 env = _get_namespace(rname)
454 version = _get_namespace_version(rname)[0]
455 exported_names = set(_get_namespace_exports(rname))
RRuntimeError: Error in loadNamespace(name) : there is no package called 'arules'
无法将 R 包导入 Python
我对 DirichletReg
做了同样的事情并且成功了。不知道为什么。
谁能帮我解决这个问题?
现在到最后的发现,python中没有这样的东西,但是,有一个方法可以使用read.transactions
groceries <- read.transactions("groceries.csv", sep = ",")
> summary(groceries)
transactions as itemMatrix in sparse format with
9835 rows (elements/itemsets/transactions) and
169 columns (items) and a density of 0.02609146
Python Jupyter 笔记本
1) 将数据导入为
import requests
url = 'https://raw.githubusercontent.com/stedy/Machine-Learning-with-R-datasets/master/groceries.csv'
grocery_dataset = requests.get(url)
# Save string as txt file
f = open('grocery_dataset.txt','w')
f.write(grocery_dataset.text)
f.close()
2) 分离数据,随意调整
import csv
grocery_items = set()
with open("grocery_dataset.txt") as f:
reader = csv.reader(f, delimiter=",")
for i, line in enumerate(reader):
grocery_items.update(line)
output_list = list()
with open("grocery_dataset.txt") as f:
reader = csv.reader(f, delimiter=",")
for i, line in enumerate(reader):
row_val = {item:0 for item in grocery_items}
row_val.update({item:1 for item in line})
output_list.append(row_val)
4) 将其保存为 python
中的 Dataframeimport pandas as pd
grocery_df = pd.DataFrame(output_list)
因此
grocery_df.shape
会给
(9835, 169)
表示R
中summary(groceries)
的行和列
importr 在 R_HOME 目录中查找已安装的 R 包。我假设,arules 包没有添加到 R_HOME 的库文件夹中,而是添加到其他位置,比如 'C:\Users\User_name\Documents\R\win-library.x.x',这可能是导致问题的原因。
如果是这种情况,请从该特定位置复制 arules 文件夹并添加到 R_HOME 目录的库文件夹中。试试这个方法,看看能不能解决问题。