这里有一个简单的例子,让你开始
using PyCall
@pyimport numpy as np # 'np' becomes a julia module
a = np.array([[1, 2], [3, 4]]) # access objects directly under a module
# (in this case the 'array' function)
# using a dot operator directly on the module
#> 2×2 Array{Int64,2}:
#> 1 2
#> 3 4
a = PyObject(a) # dear Julia, we appreciate the automatic
# convertion back to a julia native type,
# but let's get 'a' back in PyObject form
# here so we can use one of its methods:
#> PyObject array([[1, 2],
#> [3, 4]])
b = a[:mean](axis=1) # 'a' here is a python Object (not a python
# module), so the way to access a method
# or object that belongs to it is via the
# pythonobject[:method] syntax.
# Here we're calling the 'mean' function,
# with the appropriate keyword argument
#> 2-element Array{Float64,1}:
#> 1.5
#> 3.5
pybuiltin(:type)(b) # Use 'pybuiltin' to use built-in python
# commands (i.e. commands that are not
# under a module)
#> PyObject <type 'numpy.ndarray'>
pybuiltin(:isinstance)(b, np.ndarray)
#> true
使问题更加清晰。如何做到这一点:a)添加一些你的代码。 b)解释你的函数的目的是什么或者它想要计算什么。 c)使您共享的代码可运行,并可能添加一些结果或错误。 d)添加一个是问题的句子(?最后),并试图描述答案是什么。 (a),(b),(c)和(d)的任何组合都会有所帮助。 –
无论如何,谢谢你,下面的第一个答案解决了我的问题。 –