這裏有一個簡單的例子,讓你開始
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)的任何組合都會有所幫助。 –
無論如何,謝謝你,下面的第一個答案解決了我的問題。 –