Python vs Ruby "Deep Learning from scratch" Chapter 1 Graph of sin and cos functions

Overview

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

Environment

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

Code to graph the sin and cos functions

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

Output image

Python

python_graph.png

Ruby

ruby_graph.png

Reference material

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

Python vs Ruby "Deep Learning from scratch" Chapter 1 Graph of sin and cos functions
Python vs Ruby "Deep Learning from scratch" Chapter 3 Graph of step function, sigmoid function, ReLU function
Python vs Ruby "Deep Learning from scratch" Chapter 4 Implementation of loss function
Python vs Ruby "Deep Learning from scratch" Chapter 3 Implementation of 3-layer neural network
Python vs Ruby "Deep Learning from scratch" Summary
Python vs Ruby "Deep Learning from scratch" Chapter 2 Logic circuit by Perceptron
Deep Learning from scratch The theory and implementation of deep learning learned with Python Chapter 3
[Deep Learning from scratch] Implementation of Momentum method and AdaGrad method
[Learning memo] Deep Learning made from scratch [Chapter 7]
Deep learning / Deep learning made from scratch Chapter 6 Memo
[Learning memo] Deep Learning made from scratch [Chapter 6]
Deep learning / Deep learning made from scratch Chapter 7 Memo
Learning record of reading "Deep Learning from scratch"
[Learning memo] Deep Learning made from scratch [~ Chapter 4]
Chapter 1 Introduction to Python Cut out only the good points of deep learning made from scratch
Deep Learning from scratch ① Chapter 6 "Techniques related to learning"
Deep Learning from scratch Chapter 2 Perceptron (reading memo)
About shallow and deep copies of Python / Ruby
Deep Learning from scratch
Application of Deep Learning 2 made from scratch Spam filter
Deep Learning from scratch 1-3 chapters
An amateur stumbled in Deep Learning from scratch Note: Chapter 1
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 5
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 2
An amateur stumbled in Deep Learning from scratch Note: Chapter 3
An amateur stumbled in Deep Learning from scratch Note: Chapter 7
An amateur stumbled in Deep Learning from scratch Note: Chapter 5
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 7
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 1
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 4
An amateur stumbled in Deep Learning from scratch Note: Chapter 4
An amateur stumbled in Deep Learning from scratch Note: Chapter 2
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 6
Chapter 3 Neural Network Cut out only the good points of deep learning made from scratch
"Deep Learning from scratch" Self-study memo (Part 8) I drew the graph in Chapter 6 with matplotlib
Chapter 2 Implementation of Perceptron Cut out only the good points of deep learning made from scratch
Deep Learning / Deep Learning from Zero Chapter 3 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 5 Memo
Deep learning from scratch (cost calculation)
Deep Learning / Deep Learning from Zero 2 Chapter 7 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 8 Memo
Write an impression of Deep Learning 3 framework edition made from scratch
Deep Learning / Deep Learning from Zero Chapter 5 Memo
Deep Learning / Deep Learning from Zero Chapter 4 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 3 Memo
Deep Learning memos made from scratch
"Deep Learning from scratch" Self-study memo (No. 10-2) Initial value of weight
Deep Learning / Deep Learning from Zero 2 Chapter 6 Memo
Realize environment construction for "Deep Learning from scratch" with docker and Vagrant
Build a "Deep learning from scratch" learning environment on Cloud9 (jupyter miniconda python3)
Deep Learning from scratch-Chapter 4 tips on deep learning theory and implementation learned in Python
Deep learning from scratch (forward propagation edition)
Learning notes from the beginning of Python 1
Meaning of deep learning models and parameters
Deep learning / Deep learning from scratch 2-Try moving GRU
"Deep Learning from scratch" in Haskell (unfinished)
[Windows 10] "Deep Learning from scratch" environment construction
Learning notes from the beginning of Python 2
[Deep Learning from scratch] About hyperparameter optimization
"Deep Learning from scratch" Self-study memo (Part 12) Deep learning
[Python] [Natural language processing] I tried Deep Learning ❷ made from scratch in Japanese ①