I tried using the Python library from Ruby with PyCall

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)

Introduction

mrkn has developed a library called PyCall, so I tried it immediately.

https://twitter.com/mrkn

Installation

As of February 24, 2017, you can install it by following the steps below. Thank you, mrkn.

python


gem install --pre pycall

Old installation method

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.

Try drawing a graph for the time being

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.()

スクリーンショット 2017-02-24 6.12.33.png

Try to display the graph in iRuby Notebook

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 ")

Unknown.png

I was able to display it inline.

Try resizing the graph

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 ")

Unknown.png

did it!

Next time you want to try machine learning (^-^)

I also tried machine learning (additional note)

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

I tried using the Python library from Ruby with PyCall
I tried using mecab with python2.7, ruby2.3, php7
I tried using UnityCloudBuild API from Python
I tried "smoothing" the image with Python + OpenCV
[Personal memo] julia --Using Python library with julia using PyCall
vprof --I tried using the profiler for Python
I tried "differentiating" the image with Python + OpenCV
I tried "binarizing" the image with Python + OpenCV
I tried using the Datetime module by Python
I tried using the functional programming library toolz
I tried using "Streamlit" which can do the Web only with Python
I tried scraping with Python
I tried the changefinder library!
[Python] I tried using OpenPose
I tried gRPC with Python
I tried scraping with python
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I wanted to use the Python library from MATLAB
I tried to solve the problem with Python Vol.1
I tried hitting the API with echonest's python client
I tried Hello World with 64bit OS + C language without using the library
I tried using the Python library "pykakasi" that can convert kanji to romaji.
I tried running two Jetson Nano hardware PWMs from the Jetson.GPIO Python library.
[Python] I tried the same calculation as LSTM predict with from scratch [Keras]
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
I tried to simulate how the infection spreads with Python
Using Rstan from Python with PypeR
I tried sending an email from Amazon SES with Python
I tried web scraping with python.
Install PyCall on Raspberry PI and try using GPIO's library for Python from Ruby
I tried face recognition from the video (OpenCV: python version)
I tried using the DS18B20 temperature sensor with Raspberry Pi
I tried using Thonny (Python / IDE)
I liked the tweet with python. ..
Miscellaneous notes that I tried using python for the matter
[Python] I tried collecting data using the API of wikipedia
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried to divide the file into folders with Python
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
I tried using the checkio API
[Python] I tried using YOLO v3
I compared the speed of Hash with Topaz, Ruby and Python
I tried scraping the ranking of Qiita Advent Calendar with Python
I tried to solve the ant book beginner's edition with python
I tried using the python module Kwant for quantum transport calculation
I tried sending an email from the Sakura server with flask-mail
I tried to make a todo application using bottle with python
I tried to improve the efficiency of daily work with Python
I tried using Amazon SQS with django-celery
Behind the flyer: Using Docker with Python
I tried scraping Yahoo News with Python
I tried using Selenium with Headless chrome
I tried sending an email with python.
I tried using Bayesian Optimization in Python
I tried non-photorealistic rendering with Python + opencv
Working with OpenStack using the Python SDK
I tried a functional language with Python
I tried recursion with Python ② (Fibonacci sequence)