Sijun
November 11, 2019, 8:08am
#1
module TestMod
using ScikitLearn: @sk_import
@sk_import linear_model: LogisticRegression
println(LogisticRegression)
export fun
function fun()
println(LogisticRegression)
end
end
When the above TestMod is imported, LogisticRegressionis imported and printed:
PyObject <class ‘sklearn.linear_model.logistic.LogisticRegression’>
But after that, strangely LogisticRegression becomes NULL:
julia> TestMod.LogisticRegression
PyObject NULL
julia> TestMod.fun()
PyObject NULL
Yeah that’s tricky, you need to place the import in the init function, see how I did it here
module HDBSCAN
using PyCall, Clustering
export hdbscan, probabilities, exemplars, outlier_scores, leaf_size
const hdbs = PyNULL()
function __init__()
@eval global hdbs = pyimport("hdbscan")
end
"""
hdbscan(min_cluster_size=5, min_samples=min_cluster_size, metric="euclidean", alpha=1.0, p=nothing, algorithm="best", leaf_size=40, memory=Memory(location=nothing), approx_min_span_tree=true, gen_min_span_tree=false, core_dist_n_jobs=4, cluster_selection_method="eom", allow_single_cluster=false, prediction_data=false, match_reference_implementation=false)
"""
function hdbscan(X; min_cluster_size=5, kwargs...)
@show hdbs
clusterer = hdbs.HDBSCAN(min_cluster_size=min_cluster_size, kwargs...)
cluster_labels = clusterer.fit_predict(X')
HdbscanResult(clusterer, cluster_labels .+ 1)
end
This file has been truncated. show original
1 Like
Sijun
November 11, 2019, 1:15pm
#3
@baggepinnen , it works very well! I don’t know what to say. Thank you so much!
Here is some small questions:
what benefit would the use of @eval give us? On the surface, all is fine without @eval .
How does the trick work?
You’ll find more info here
# Calling Python functions from the Julia language
[](https://travis-ci.org/JuliaPy/PyCall.jl)
[](https://ci.appveyor.com/project/StevenGJohnson/pycall-jl-nu3aa)
[](https://coveralls.io/r/JuliaPy/PyCall.jl?branch=master)
[](http://pkg.julialang.org/?pkg=PyCall&ver=0.7)
This package provides the ability to directly call and **fully
interoperate with Python** from [the Julia
language](https://julialang.org/). You can import arbitrary Python
modules from Julia, call Python functions (with automatic conversion
of types between Julia and Python), define Python classes from Julia
methods, and share large data structures between Julia and Python
without copying them.
## Installation
Within Julia, just use the package manager to run `Pkg.add("PyCall")` to
This file has been truncated. show original
It seems there is a recommended approach slightly different from mine.
3 Likes
Sijun
November 11, 2019, 2:39pm
#5
Aha, I understood what is happening now. Thank you!