The library has been updated and the usage has changed considerably. Please check the article below for a description of the new version.
[Use PyCall to defeat Python libraries from Ruby] (https://qiita.com/mix_dvd/items/d49ed4ff6553f3ace5a7)
mrkn has developed a library called PyCall, so I tried it immediately.
https://twitter.com/mrkn
As of February 24, 2017, you can install it by following the steps below. Thank you, mrkn.
python
gem install --pre pycall
I will leave it as a memorandum. If you cannot install by the above procedure, please try it.
You can download the source code from GitHub.
https://github.com/mrkn/pycall
As of February 23, 2017, it was said that it will be downloaded from the branch under development, so I got it with the following command.
python
git clone -b eval https://github.com/mrkn/pycall.git
From there, I had a little trouble installing Gem, but I was able to install it by following the steps below. If anyone knows a smarter way, please let me know (sweat)
python
#Creating a Gem file
gem build pycall/pycall.gemspec
#Install Gem
gem install pycall-0.1.0.alpha.gem
#For some reason, the Gem folder is empty, so delete it for the time being
rm -rf ~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pycall-0.1.0.alpha
#Copy the downloaded source
mv pycall ~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pycall-0.1.0.alpha
By the way, I installed Python with Anaconda and used it, and Ruby installed it with rbenv.
I was able to display the graph with the following code.
python
require 'pycall/import'
include PyCall::Import
pyimport 'numpy', as: 'np'
pyimport 'matplotlib.mlab', as: 'mlab'
pyimport 'matplotlib.pyplot', as: 'plt'
plt.plot.([1,3,4,6,7,9])
plt.plot.([3,2,4,1,5,0])
plt.show.()
When I run the above source code in iRuby Notebook, I can draw the graph, but the graph is displayed in a separate window as well.
Moreover, the subsequent operation is a little unstable (sweat)
This is a bit of a problem, so change it as follows.
python
require 'pycall/import'
include PyCall::Import
pyimport 'numpy', as: 'np'
pyimport 'matplotlib.mlab', as: 'mlab'
pyimport 'matplotlib.pyplot', as: 'plt'
plt.plot.([1,3,4,6,7,9])
plt.plot.([3,2,4,1,5,0])
#Save the graph as an image
plt.savefig.("pycall.png ")
plt.close.()
#Load the saved graph image
open("pycall.png ")
I was able to display it inline.
If you don't specify it, a square graph will be displayed, so change it a little.
python
require 'pycall/import'
include PyCall::Import
pyimport 'numpy', as: 'np'
pyimport 'matplotlib.mlab', as: 'mlab'
pyimport 'matplotlib.pyplot', as: 'plt'
plt.figure.(figsize: PyCall.tuple(8, 3))
plt.plot.([1,3,4,6,7,9])
plt.plot.([3,2,4,1,5,0])
#Save the graph as an image
plt.savefig.("pycall.png ")
plt.close.()
#Load the saved graph image
open("pycall.png ")
did it!
Next time you want to try machine learning (^-^)
You can refer to the result of executing the power demand forecast from the following URL, so please have a look if you are interested.
Power demand forecast from actual power demand http://blueomega.jp/predict_kw_with_pycall.html
Power demand forecast by adding the temperature one hour ago to the actual power demand http://blueomega.jp/predict_kw_with_temp_and_pycall.html
Recommended Posts