KDB embedPy 在 Python 中使用 Q 中的数组
KDB embedPy use array from Q in Python
我正在使用 JupyterQ,并且在 q:
中有两个 2 tables
tbl1: ([]month:(1997.01;1997.01;1997.01);
ric:(3#`GTGS.SI);
date:(1997.01.06T01:19:21.160;1997.01.06T01:53:57.230;1997.01.06T07:24:19.240);
price:(0.71;0.72;0.73));
tbl2: ([]month:(1997.01;1997.01;1997.01);
ric:(3#`NSWF.SI);
date:(1997.01.06T01:19:21.160;1997.01.06T01:53:57.230;1997.01.06T07:24:19.240);
price:(0.45;0.46;0.47));
我在 python 中定义了一个 class teCalc
。
我想向它传递两个数组,如下所示(在 python 中):
teCalc.addObservations(tbl1, tbl2)
如何将那些 q tables 传递给 python 中的 class?
以下函数:
print .p.import[`numpy;`:array][tbl1]
给我:
{'month': array([1997.01, 1997.01, 1997.01]), 'ric': ['GTGS.SI', 'GTGS.SI', 'GTGS.SI'], 'date': array([-1089.94489398, -1089.92086539, -1089.69144398]), 'price': array([0.71, 0.72, 0.73])}
所以我可以看到可以将 q 中的 table 转换为 python 对象。
但是,我将其分配给 python 中的变量的所有尝试都失败了。如果我在 JupyterQ 的 python 代码行中引用 tbl1 或 tbl2(例如 p)print(tbl1)
),则无法识别 tbl1 变量名称。
请指教。如何在 JupyterQ 的 Python 中创建的 class 上使用来自 Q 的数据?
感谢和问候,
西蒙
[编辑]:这应该是 embedPy 而不是 embedQ - 我的错误!!
我从 KX 的康纳那里得到了这个答案。
选择是使用 Set 将数据导入 python 或使用 Get 将 python class 导入 Q。
真的很酷。
西蒙
嗨,西蒙,
分配 q 个对象以便从 API 的 Python 侧定义的函数访问需要您设置对象。如果您描述的是您想要 运行
p)print(tbl1)
您需要在以下步骤中运行:
// Define the q table
mytab:([]5?1f;5?1f)
// set the q table converted to a python dictionary to python variable tbl1
.p.set[`tbl1;mytab]
// Print from the python side of the interface
p)print(tbl1)
关于使用 q 对象调用 class,您需要像上面那样从 q 侧到 python 进行设置。即
.p.set[`tbl1;tbl1]
.p.set[`tbl2;tbl2]
p)teCalc.addObservations(tbl1, tbl2)
或者 'get' python 可调用对象并将其分配给 q 可调用函数和 运行 直接在没有集合
的 q 数据上
// Load the script containing the required class
\l myclass.p
// Retrieve the class
teCalc:.p.get[`teCalc]
// Access the relevant function
addObservations:teCalc[`:addObservations]
// Run on the relevant q data and return result to q (denoted by trailing `)
addObservations[tbl1;tbl2]`
应该注意的是,当 embedPy 将 q tables 转换为 Python 时,它是作为字典列表执行的(这是 q table 的准确表示,但是可能违反直觉)因此,如果您需要数据成为您用例的 pandas 数据框,您可能需要手动进行转换。这是使用 2 个函数 .ml.tab2df/.ml.df2tab 完成的,它们都在 Kx 机器学习工具包 here.
中可用
有关 embedPy 的更多信息可在以下链接中找到,其中详细概述了上述所有内容
https://code.kx.com/q/ml/embedpy/userguide/
https://code.kx.com/q/ml/embedpy/faq/
祝一切顺利,
康纳
我正在使用 JupyterQ,并且在 q:
中有两个 2 tablestbl1: ([]month:(1997.01;1997.01;1997.01);
ric:(3#`GTGS.SI);
date:(1997.01.06T01:19:21.160;1997.01.06T01:53:57.230;1997.01.06T07:24:19.240);
price:(0.71;0.72;0.73));
tbl2: ([]month:(1997.01;1997.01;1997.01);
ric:(3#`NSWF.SI);
date:(1997.01.06T01:19:21.160;1997.01.06T01:53:57.230;1997.01.06T07:24:19.240);
price:(0.45;0.46;0.47));
我在 python 中定义了一个 class teCalc
。
我想向它传递两个数组,如下所示(在 python 中):
teCalc.addObservations(tbl1, tbl2)
如何将那些 q tables 传递给 python 中的 class?
以下函数:
print .p.import[`numpy;`:array][tbl1]
给我:
{'month': array([1997.01, 1997.01, 1997.01]), 'ric': ['GTGS.SI', 'GTGS.SI', 'GTGS.SI'], 'date': array([-1089.94489398, -1089.92086539, -1089.69144398]), 'price': array([0.71, 0.72, 0.73])}
所以我可以看到可以将 q 中的 table 转换为 python 对象。
但是,我将其分配给 python 中的变量的所有尝试都失败了。如果我在 JupyterQ 的 python 代码行中引用 tbl1 或 tbl2(例如 p)print(tbl1)
),则无法识别 tbl1 变量名称。
请指教。如何在 JupyterQ 的 Python 中创建的 class 上使用来自 Q 的数据?
感谢和问候,
西蒙
[编辑]:这应该是 embedPy 而不是 embedQ - 我的错误!!
我从 KX 的康纳那里得到了这个答案。
选择是使用 Set 将数据导入 python 或使用 Get 将 python class 导入 Q。
真的很酷。
西蒙
嗨,西蒙,
分配 q 个对象以便从 API 的 Python 侧定义的函数访问需要您设置对象。如果您描述的是您想要 运行
p)print(tbl1)
您需要在以下步骤中运行:
// Define the q table
mytab:([]5?1f;5?1f)
// set the q table converted to a python dictionary to python variable tbl1
.p.set[`tbl1;mytab]
// Print from the python side of the interface
p)print(tbl1)
关于使用 q 对象调用 class,您需要像上面那样从 q 侧到 python 进行设置。即
.p.set[`tbl1;tbl1]
.p.set[`tbl2;tbl2]
p)teCalc.addObservations(tbl1, tbl2)
或者 'get' python 可调用对象并将其分配给 q 可调用函数和 运行 直接在没有集合
的 q 数据上// Load the script containing the required class
\l myclass.p
// Retrieve the class
teCalc:.p.get[`teCalc]
// Access the relevant function
addObservations:teCalc[`:addObservations]
// Run on the relevant q data and return result to q (denoted by trailing `)
addObservations[tbl1;tbl2]`
应该注意的是,当 embedPy 将 q tables 转换为 Python 时,它是作为字典列表执行的(这是 q table 的准确表示,但是可能违反直觉)因此,如果您需要数据成为您用例的 pandas 数据框,您可能需要手动进行转换。这是使用 2 个函数 .ml.tab2df/.ml.df2tab 完成的,它们都在 Kx 机器学习工具包 here.
中可用有关 embedPy 的更多信息可在以下链接中找到,其中详细概述了上述所有内容
https://code.kx.com/q/ml/embedpy/userguide/
https://code.kx.com/q/ml/embedpy/faq/
祝一切顺利,
康纳