Write a program to draw a sine-cosine graph in Python and Ruby, referring to the code in Chapter 1 of the book "Deep Learning from scratch-The theory and implementation of deep learning learned in Python".
Python
In the book "Deep Learning from scratch", the Anaconda distribution was installed to build the environment, but here we will only install numpy and matplotlib with pip.
This environment.
macOS Sierra + Homebrew + pyenv + Python 3.6.1
Install NumPy, a numerical calculation library, and Matplotlib, a graph drawing library.
$ pip install numpy matplotlib
Ruby
This environment.
macOS Sierra + Homebrew + rbenv + Ruby 2.4.1
Install Numo :: NArray, a multidimensional numeric array library, and Numo :: Gnuplot, a graph drawing library.
$ gem install numo-narray numo-gnuplot
Numo :: Gnuplot uses Gnuplot, so install it with Homebrew.
$ brew install gnuplot
Python
import numpy as np
import matplotlib
matplotlib.use("AGG") #AGG in drawing library(Anti-Grain Geometry)use
import matplotlib.pyplot as plt
#Data creation
x = np.arange(0, 6, 0.1) #0 to 6 0.Generate in 1 increments
y1 = np.sin(x)
y2 = np.cos(x)
#Output numerical data for confirmation
print("x:", *x)
print("y1:", *y1)
print("y2:", *y2)
#Drawing a graph
plt.figure(figsize=(4, 3), dpi=160) #Image size
plt.plot(x, y1, label="sin")
plt.plot(x, y2, linestyle = "--", label="cos") #Draw with dashed line
plt.xlabel("x") #x-axis label
plt.ylabel("y") #y-axis label
plt.title("sin & cos") #title
plt.legend() #Usage Guide
plt.savefig("python_graph.png ")
Ruby
require 'numo/narray'
require 'numo/gnuplot'
#Data creation
x = Numo::DFloat.new(60).seq(0, 0.1) #0 to 6 0.Generate in 1 increments
y1 = Numo::DFloat::Math.sin(x)
y2 = Numo::DFloat::Math.cos(x)
#Output numerical data for confirmation
puts "x: #{x.to_a.join(' ')}"
puts "y1: #{y1.to_a.join(' ')}"
puts "y2: #{y2.to_a.join(' ')}"
#Drawing a graph
g = Numo::gnuplot do
set term: {png: {size: [640, 480]}} #Image size
set output: 'ruby_graph.png'
set title: 'sin \& cos' #title
set key: 'box left bottom'
set offset: [0, 0, 0, 0]
plot x, y1, {w: 'lines', lw: 3, title: 'sin'},
x, y2, {w: 'lines', lw: 3, title: 'cos'}
end
Python
Ruby
--Python vs Ruby "Deep Learning from scratch" Summary --Qiita http://qiita.com/niwasawa/items/b8191f13d6dafbc2fede --O'Reilly Japan --Deep Learning from scratch https://www.oreilly.co.jp/books/9784873117584/ --GitHub --oreilly-japan/deep-learning-from-scratch: "Deep Learning from scratch" repository https://github.com/oreilly-japan/deep-learning-from-scratch
Recommended Posts