├── Chapter03 ├── 01. ストリームを管理し、ファイルに読み書きする │ ├── iotest.txt │ ├── iotest2.txt │ ├── iotest.jl │ └── commands.txt ├── 08. Juliaをバックグラウンドプロセスとして使う │ ├── commands.txt │ └── 3.08 Juliaをバックグラウンドプロセスとして使う.ipynb ├── 05. JSONデータを処理する │ └── commands.txt ├── 10. Featherデータを扱う │ └── commands.txt ├── 02. IOBufferを使って効率的なインメモリストリームを作る │ ├── commands.txt │ └── 3.02 IOBufferを使って効率的なインメモリストリームを作る.ipynb ├── 11. CSVファイルとFWFファイルを読み込む │ ├── commands.txt │ └── 3.11 CSVファイルとFWFファイルを読み込む.ipynb ├── 04. 簡単なRESTfulサービスを作ってみる │ ├── 3.04 簡単なRESTfulサービスを作ってみる-ZeroMQ Web Client.ipynb │ ├── 3.04 簡単なRESTfulサービスを作ってみる - クライアント.ipynb │ ├── commands.txt │ ├── 3.04 簡単なRESTfulサービスを作ってみる - サーバ.ipynb │ ├── 3.04 簡単なRESTfulサービスを作ってみる-ZeroMQ Server.ipynb │ └── 3.04 簡単なRESTfulサービスを作ってみる-ZeroMQ Web Server.ipynb ├── 07. オブジェクトをシリアライズする │ └── commands.txt ├── 03. インターネットからデータを取得する │ └── commands.txt ├── 06. 日付と時刻を扱う │ └── commands.txt └── 09. Microsoft Excelファイルを読み書きする │ └── commands.txt ├── Chapter01 ├── 07. Juliaをマルチコアで使う │ ├── hello.jl │ ├── hello2.jl │ └── commands.txt ├── 06. Julia起動時の動作を変更する │ ├── hello.jl │ ├── startup.jl │ └── commands.txt ├── 08. Juliaのインタラクティブモードを使いこなす │ ├── example.jl │ └── commands.txt ├── 02. JuliaをIDEで使う │ └── SublimeText.txt ├── 05. JuliaをAWSクラウド上のCloud9 IDEで使う │ └── JuliaRunner.run ├── 09. Juliaで計算結果を表示する │ ├── commands.txt │ ├── display.jl │ └── display2.jl ├── 01. Juliaをバイナリパッケージでインストールする │ └── commands.txt ├── 10. パッケージの管理 │ └── commands.txt ├── 11. JuliaをJupyter Notebookで使う │ └── commands.txt ├── 13. ターミナルしか使えないクラウド環境でJupyter Notebookを使う │ └── commands.txt ├── 03. Juliaをテキストエディタで使う │ └── commands.txt ├── 12. JuliaをJupyterLabで使う │ └── commands.txt └── 04. JuliaをLinux でソースからビルドする │ └── commands.txt ├── Chapter10 ├── 04. 分散環境で計算する │ ├── machinefile.txt │ ├── config │ └── commands.txt ├── 01. マルチプロセスで計算する │ └── commands.txt ├── 03. マルチスレッドで計算する │ └── commands.txt └── 02. リモートのJuliaプロセスと通信する │ └── commands.txt ├── Chapter04 ├── 10. Juliaのブロードキャストを理解する │ └── commands.txt ├── 09. 線形回帰で予測する │ ├── commands.txt │ └── lm.jl ├── 01. 行列処理を高速化する │ ├── commands.txt │ ├── sums.jl │ └── 4.01 行列処理を高速化する.ipynb ├── 03. 完全実施要因計画の生成 │ ├── commands.txt │ ├── expand.jl │ └── 4.03 完全実施要因計画の生成.ipynb ├── 08. 単純な最適化を書いてみる │ ├── commands.txt │ └── marquardt.jl ├── 11. @inbounds を使って高速化する │ ├── commands.txt │ └── inbounds.jl ├── 05. モンテカルロシミュレーションの実行 │ ├── simwalk.jl │ └── commands.txt ├── 07. 複素数を用いた計算 │ └── commands.txt ├── 12. ベクトルの集合から行列を作る │ └── commands.txt ├── 04. 級数の部分和によるπの近似 │ └── commands.txt ├── 06. 待ち行列の解析 │ ├── commands.txt │ └── mm1.jl ├── 13. 配列ビューを使って使用メモリ量を減らす │ ├── sudoku.jl │ ├── commands.txt │ └── 4.13 配列ビューを使って使用メモリ量を減らす.ipynb └── 02. 条件文のあるループの効率的な実行 │ ├── commands.txt │ └── 4.02 条件文のあるループの効率的な実行.ipynb ├── Chapter09 ├── 03. 最尤推定を行う │ ├── commands.txt │ ├── opt.jl │ └── 9.3 最尤推定を行う.ipynb ├── 02. JuMPを使って最適化問題を解く │ └── commands.txt ├── 04. Plots.jlを使って複雑なプロットを描く │ └── commands.txt ├── 05. ScikitLearn.jlを使って機械学習モデルを作る │ └── commands.txt └── 01. Juliaでデータベースを使う │ └── commands.txt ├── Chapter06 ├── 05. イントロスペクションを使ってJuliaの数値型の構成を調べる │ ├── commands.txt │ ├── types.jl │ └── 6.05 イントロスペクションを使ってJuliaの数値型の構成を調べる.ipynb ├── 03. ユーザ定義型を作ってみる−連結リスト │ ├── commands.txt │ └── ll.jl ├── 07. 変更可能型と変更不能型の性能差を確認する │ ├── commands.txt │ ├── work.jl │ └── walk.jl ├── 04. 基本型を定義する │ ├── commands.txt │ └── argb.jl ├── 08. 型安定性を保証する │ ├── quad.jl │ └── commands.txt ├── 06. 静的配列を利用する │ └── commands.txt ├── 02. マクロと関数生成を理解する │ └── commands.txt └── 01. メタプログラミングを理解する │ └── commands.txt ├── Chapter02 ├── 07. UTF-8文字列を扱う │ ├── hello.txt │ └── commands.txt ├── 04. 正規表現を使ってGitログを解析する │ ├── commands.txt │ └── parselog.jl ├── 05. 標準的でない基準でデータをソートする │ ├── commands.txt │ └── 2.5 標準的でない基準でデータをソートする.ipynb ├── 02. 行列乗算を高速に行う │ ├── commands.txt │ ├── fastmatmul.jl │ └── 2.2 行列乗算を高速に行う.ipynb ├── 01. 配列中の最小要素のインデックスを取得する │ ├── randargmin2.jl │ └── commands.txt ├── 06. 関数原像の生成-辞書とセットの機能を理解する │ └── commands.txt └── 03. カスタム擬似乱数生成器を実装する │ └── commands.txt ├── Chapter08 ├── 03. コードのプロファイリング │ ├── profiletest.jl │ └── commands.txt ├── 01. Revise.jlを用いてモジュールを開発する │ ├── commands.txt │ └── Module1.jl ├── 07. フロジェクトの依存関係を管理する │ └── commands.txt ├── 02. コードのベンチマーク │ └── commands.txt ├── 05. JuliaからPythonを使う │ └── commands.txt ├── 04. コードのログを取る │ └── commands.txt └── 06. JuliaからRを使う │ └── commands.txt ├── Chapter07 ├── 02. データフレームの内容を確認する │ └── commands.txt ├── 01. データフレームと行列を変換する │ └── commands.txt ├── 10. データフレーム変換を繰り返してピボットテーブルを作成する │ ├── commands.txt │ └── iris.csv ├── 03. インターネット上のCSVデータを読み込む │ ├── commands.txt │ └── iris.csv ├── 06. データフレームを使って分割‐適用‐結合を行う │ ├── commands.txt │ └── iris.csv ├── 04. カテゴリデータを処理する │ └── commands.txt ├── 08. データフレームの同一性を判定する │ ├── commands.txt │ └── grades.csv ├── 09. データフレームの行を変換する │ ├── commands.txt │ └── grades.csv ├── 05. 欠損値を扱う │ ├── cor.jl │ ├── commands.txt │ └── grades.csv └── 07. 縦型データフレームと横型データフレームを変換する │ ├── commands.txt │ └── iris.csv ├── Chapter05 ├── 07. 名前付きタプルの使い方 │ └── commands.txt ├── 04. 関数型でプログラミングする │ └── commands.txt ├── 03. 関数を値として使う │ └── commands.txt ├── 02. 多重ディスパッチで動作を切り替える │ └── commands.txt ├── 01. Juliaのサブタイプを理解する │ └── commands.txt ├── 06. 例外処理 │ └── commands.txt └── 05. 変数のスコープを理解する │ └── commands.txt ├── LICENSE ├── cookbookconf.jl ├── README.md └── Julia_Cookbook-Supplement.md /Chapter03/01. ストリームを管理し、ファイルに読み書きする/iotest.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 3 -------------------------------------------------------------------------------- /Chapter03/01. ストリームを管理し、ファイルに読み書きする/iotest2.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 4 -------------------------------------------------------------------------------- /Chapter01/07. Juliaをマルチコアで使う/hello.jl: -------------------------------------------------------------------------------- 1 | println("Hello " * join(ARGS, ", ")) 2 | -------------------------------------------------------------------------------- /Chapter01/06. Julia起動時の動作を変更する/hello.jl: -------------------------------------------------------------------------------- 1 | println("Hello " * join(ARGS, ", ")) 2 | -------------------------------------------------------------------------------- /Chapter01/08. Juliaのインタラクティブモードを使いこなす/example.jl: -------------------------------------------------------------------------------- 1 | println("An example was run!") 2 | -------------------------------------------------------------------------------- /Chapter01/07. Juliaをマルチコアで使う/hello2.jl: -------------------------------------------------------------------------------- 1 | println("Hello " * join(ARGS, ", ")) 2 | sleep(1) 3 | -------------------------------------------------------------------------------- /Chapter10/04. 分散環境で計算する/machinefile.txt: -------------------------------------------------------------------------------- 1 | 2*ubuntu@127.0.0.1 2 | 1*ubuntu@127.0.0.1 3 | 1*ubuntu@127.0.0.1 4 | -------------------------------------------------------------------------------- /Chapter01/06. Julia起動時の動作を変更する/startup.jl: -------------------------------------------------------------------------------- 1 | using Random 2 | 3 | ENV["JULIA_EDITOR"] = "vim" 4 | 5 | println("Setup successful") 6 | -------------------------------------------------------------------------------- /Chapter10/04. 分散環境で計算する/config: -------------------------------------------------------------------------------- 1 | User ubuntu 2 | PubKeyAuthentication yes 3 | StrictHostKeyChecking no 4 | IdentityFile ~/.ssh/cluster -------------------------------------------------------------------------------- /Chapter01/02. JuliaをIDEで使う/SublimeText.txt: -------------------------------------------------------------------------------- 1 | { 2 | "cmd": ["ConEmu64", "/cmd", "julia -i", "$file"], 3 | "selector": "source.julia" 4 | } -------------------------------------------------------------------------------- /Chapter01/06. Julia起動時の動作を変更する/commands.txt: -------------------------------------------------------------------------------- 1 | $ julia -i hello.jl Al Bo Cyd 2 | $ julia -e "println(factorial(10))" 3 | $ julia --banner=no 4 | 5 | 6 | julia> RandomDevice() 7 | -------------------------------------------------------------------------------- /Chapter04/10. Juliaのブロードキャストを理解する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> x = [1:10;] 2 | julia> s = isodd.(x) .& (rand(length(x)) .< 0.5) 3 | julia> s = @. isodd(x) & ($rand($length(x)) < 0.5) 4 | -------------------------------------------------------------------------------- /Chapter01/05. JuliaをAWSクラウド上のCloud9 IDEで使う/JuliaRunner.run: -------------------------------------------------------------------------------- 1 | { 2 | "cmd" : ["julia", "$file", "$args"], 3 | "info" : "Started $project_path$file_name", 4 | "selector" : "source.jl" 5 | } -------------------------------------------------------------------------------- /Chapter04/09. 線形回帰で予測する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("lm.jl") 2 | julia> using CSV 3 | julia> wages = CSV.read("wages.csv", categorical=true); 4 | julia> lm(wages, :LWage, setdiff(names(wages), [:LWage])) 5 | -------------------------------------------------------------------------------- /Chapter09/03. 最尤推定を行う/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("opt.jl") 2 | julia> using Random 3 | julia> Random.seed!(1); 4 | julia> x = randn(100); 5 | julia> testoptim(x) 6 | julia> mean(x), std(x)*sqrt(99/100) 7 | -------------------------------------------------------------------------------- /Chapter06/05. イントロスペクションを使ってJuliaの数値型の構成を調べる/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("types.jl") 2 | julia> printsubtypes(Number) 3 | julia> supertypes(Bool) 4 | 5 | 6 | julia> typeof(1 + 1im) 7 | julia> typeof(1.0 + 1.0im) 8 | -------------------------------------------------------------------------------- /Chapter04/01. 行列処理を高速化する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("sums.jl") 2 | julia> using BenchmarkTools 3 | julia> x = rand(10^4, 10^4); 4 | julia> @btime sum_by_row(x) 5 | julia> @btime sum_by_col(x) 6 | julia> @btime sum(x) 7 | -------------------------------------------------------------------------------- /Chapter04/03. 完全実施要因計画の生成/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("expand.jl") 2 | julia> expandgrid(1:2, 'a':'b') 3 | julia> hcat(expandgrid(1:3, [true, false], 'a':'b')...) 4 | 5 | 6 | julia> repeat([1,2], inner=2, outer=3) 7 | -------------------------------------------------------------------------------- /Chapter04/08. 単純な最適化を書いてみる/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("marquardt.jl") 2 | julia> rosenbrock(x) = 3 | sum([(1-x[i])^2 + 100(x[i+1]-x[i]^2)^2 for i in 1:length(x)-1]) 4 | julia> marquardt(rosenbrock, rand(20)) 5 | -------------------------------------------------------------------------------- /Chapter02/07. UTF-8文字列を扱う/hello.txt: -------------------------------------------------------------------------------- 1 | Bulgarian Здравейте 2 | Chinese 你好 3 | English Hello 4 | Greek Χαίρετε 5 | Hindi नमस्ते 6 | Japanese こんにちは 7 | Khmer សួស្តី 8 | Korean 여보세요 9 | Polish cześć 10 | Russian Здравствуйте 11 | -------------------------------------------------------------------------------- /Chapter01/09. Juliaで計算結果を表示する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using Pkg; Pkg.add("PyPlot") 2 | julia> include("display.jl") 3 | 4 | 5 | $ julia display.jl 6 | $ julia display2.jl 7 | 8 | 9 | julia> transpose(1:100) 10 | julia> rand(100, 100); 11 | -------------------------------------------------------------------------------- /Chapter01/09. Juliaで計算結果を表示する/display.jl: -------------------------------------------------------------------------------- 1 | using PyPlot, Random 2 | 3 | function f() 4 | Random.seed!(1) 5 | r = rand(50) 6 | @show sum(r) 7 | display(transpose(r)) 8 | print(transpose(r)) 9 | plot(r) 10 | end 11 | 12 | f() 13 | -------------------------------------------------------------------------------- /Chapter08/03. コードのプロファイリング/profiletest.jl: -------------------------------------------------------------------------------- 1 | using Statistics 2 | function timeto1(mv) 3 | x = Int[] 4 | while true 5 | push!(x, rand(1:mv)) 6 | 1 in x && return length(x) 7 | end 8 | end 9 | agg(f, mv, rep) = mean(f(mv) for i in 1:rep) -------------------------------------------------------------------------------- /Chapter03/08. Juliaをバックグラウンドプロセスとして使う/commands.txt: -------------------------------------------------------------------------------- 1 | $ mkfifo pipe 2 | $ ls 3 | $ julia log.txt 2>err.txt & 4 | $ exec 3>pipe 5 | $ echo "1+2" >&3 6 | $ echo "X" >&3 7 | $ echo "exit()" >&3 8 | $ cat log.txt 9 | $ cat err.txt 10 | $ rm pipe log.txt err.txt 11 | -------------------------------------------------------------------------------- /Chapter01/09. Juliaで計算結果を表示する/display2.jl: -------------------------------------------------------------------------------- 1 | using PyPlot, Random 2 | 3 | function f() 4 | Random.seed!(1) 5 | r = rand(50) 6 | @show sum(r) 7 | display(transpose(r)) 8 | print(transpose(r)) 9 | plot(r) 10 | show() 11 | end 12 | 13 | f() 14 | 15 | -------------------------------------------------------------------------------- /Chapter06/03. ユーザ定義型を作ってみる−連結リスト/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("ll.jl") 2 | julia> charlist = LinkedList(Char) 3 | julia> pushfirst!(charlist, collect("12345")...) 4 | julia> collect(charlist) 5 | julia> charlist[1], charlist[5] 6 | julia> charlist[0] 7 | julia> charlist[6] 8 | julia> charlist[end] 9 | -------------------------------------------------------------------------------- /Chapter07/02. データフレームの内容を確認する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using DataFrames, Random 2 | julia> Random.seed!(1); 3 | julia> df = DataFrame(rand(1000, 100)); 4 | julia> nrow(df), ncol(df), size(df) 5 | julia> describe(df[:, 1:3]) 6 | julia> filter(x -> occursin(r"1[13579]$", String(x[:variable])), 7 | describe(df)) 8 | -------------------------------------------------------------------------------- /Chapter01/01. Juliaをバイナリパッケージでインストールする/commands.txt: -------------------------------------------------------------------------------- 1 | $ sudo apt update 2 | $ sudo apt -y install build-essential 3 | 4 | $ wget https://julialang-s3.julialang.org/bin/linux/x64/1.0/julia-1.2.0-linux-x86_64.tar.gz 5 | $ tar xvfz julia-1.2.0-linux-x86_64.tar.gz 6 | $ sudo ln -s /home/ubuntu/julia-1.2.0/bin/julia /usr/local/bin/julia 7 | -------------------------------------------------------------------------------- /Chapter03/01. ストリームを管理し、ファイルに読み書きする/iotest.jl: -------------------------------------------------------------------------------- 1 | a = parse(Float64, readline(stdin)) 2 | b = parse(Float64, readline(stdin)) 3 | println(stdout, "Got values: $a, $b") 4 | if b > a 5 | println(stderr, "Wrong values: ", b, ">", a) 6 | exit(1) 7 | end 8 | println(stdout, "log(", a, "-", b, ")=", log(a-b)) 9 | exit(0) 10 | -------------------------------------------------------------------------------- /Chapter04/11. @inbounds を使って高速化する/commands.txt: -------------------------------------------------------------------------------- 1 | $ julia inbounds.jl 2 | $ julia --check-bounds=yes inbounds.jl 3 | $ julia --check-bounds=no inbounds.jl 4 | 5 | 6 | julia> x = [1,2,3] 7 | julia> x[4] 8 | julia> f(x) = @inbounds x[1] 9 | julia> g(x) = x[1] 10 | julia> @code_native f([1]) 11 | julia> @code_native g([1]) 12 | -------------------------------------------------------------------------------- /Chapter01/10. パッケージの管理/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> status 3 | (v1.0) pkg> add BenchmarkTools 4 | (v1.0) pkg> status 5 | (v1.0) pkg> precompile 6 | 7 | 8 | julia> using BenchmarkTools 9 | julia> @btime rand() 10 | 11 | julia> ] 12 | (v1.0) pkg> add BSON@v0.2.0 13 | (v1.0) pkg> pin BSON 14 | (v1.0) pkg> free BSON 15 | -------------------------------------------------------------------------------- /Chapter06/07. 変更可能型と変更不能型の性能差を確認する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("walk.jl") 2 | julia> using BenchmarkTools 3 | julia> @benchmark simI() 4 | julia> @benchmark simM() 5 | julia> @benchmark simI2() 6 | julia> @benchmark simM2() 7 | 8 | 9 | julia> include("work.jl") 10 | julia> @benchmark worker1() 11 | julia> @benchmark worker2() 12 | -------------------------------------------------------------------------------- /Chapter08/01. Revise.jlを用いてモジュールを開発する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | pkg> add Revise 3 | pkg> add HTTP 4 | 5 | 6 | push!(LOAD_PATH, ".") 7 | using Revise 8 | using Module1 9 | 10 | julia> getcoinprices("2018-06-20", "2018-06-22") 11 | 12 | julia> getcoinprices("2018-06-23", "2018-06-22") 13 | 14 | julia> getcoinprices("2018-06-23", "2018-06-22") -------------------------------------------------------------------------------- /Chapter09/03. 最尤推定を行う/opt.jl: -------------------------------------------------------------------------------- 1 | using Optim 2 | using Distributions 3 | 4 | function loglik(x, μ, logσ) 5 | nd = Normal(μ, exp(logσ)) 6 | -sum(logpdf(nd, v) for v in x) 7 | end 8 | 9 | function testoptim(x) 10 | res = optimize(par -> loglik(x, par[1], par[2]), zeros(2)) 11 | display(res) 12 | res.minimizer[1], exp(res.minimizer[2]) 13 | end 14 | -------------------------------------------------------------------------------- /Chapter06/04. 基本型を定義する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("argb.jl") 2 | julia> ARGB(10,11,12,13) 3 | julia> c = ARGB"#12345678" 4 | julia> [f(c) for f in [α, red, green, blue]] 5 | julia> UInt32(c) 6 | julia> String(c) 7 | 8 | julia> zeroalpha(c::ARGB) = ARGB(UInt32(c) & 0x00FFFFFF) 9 | julia> c = ARGB"#12345678" 10 | julia> zeroalpha(c) 11 | julia> @code_native zeroalpha(c) 12 | -------------------------------------------------------------------------------- /Chapter04/03. 完全実施要因計画の生成/expand.jl: -------------------------------------------------------------------------------- 1 | function expandgrid(levels...) 2 | lengths = length.(levels) 3 | inner = 1 4 | outer = prod(lengths) 5 | grid = [] 6 | for i in 1:length(levels) 7 | outer = div(outer, lengths[i]) 8 | push!(grid, repeat(levels[i], inner=inner, outer=outer)) 9 | inner *= lengths[i] 10 | end 11 | Tuple(grid) 12 | end 13 | -------------------------------------------------------------------------------- /Chapter07/01. データフレームと行列を変換する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using DataFrames 2 | julia> mat = [x*y for x in 1:3, y in 1:4] 3 | julia> df = DataFrame(mat) 4 | julia> df = DataFrame(mat, [:a, :b, :c, :d]) 5 | julia> Matrix(df) 6 | 7 | 8 | julia> vals = [1, 2]; 9 | julia> df = DataFrame(a=vals, b=["x", "y"]) 10 | julia> df.a[1] = 8 11 | julia> vals 12 | 13 | 14 | julia> getindex.(eachcol(df), 2) 15 | -------------------------------------------------------------------------------- /Chapter01/08. Juliaのインタラクティブモードを使いこなす/commands.txt: -------------------------------------------------------------------------------- 1 | julia> x = 10 # 単なるテスト 2 | julia> @edit sin(1.0) 3 | julia> ; 4 | 5 | 6 | # Linux bash 7 | shell> ls 8 | 9 | # Windows cmd 10 | shell> dir 11 | 12 | 13 | julia> include("example.jl") 14 | julia> ? 15 | help?> include 16 | julia> 17 | (reverse-i-search)`x =': x = 10 18 | julia> x = 10 19 | julia> exit() 20 | julia> @which sin(1.0) 21 | -------------------------------------------------------------------------------- /Chapter06/08. 型安定性を保証する/quad.jl: -------------------------------------------------------------------------------- 1 | function quadratic1(a, b, c) 2 | t(s) = (-b + s*sqrt(Δ))/(2a) 3 | a == 0 && error("a must be different than zero") 4 | Δ = Complex(b^2-4*a*c) 5 | t(1), t(-1) 6 | end 7 | 8 | function quadratic2(a, b, c) 9 | Δ = Complex(b^2-4*a*c) 10 | t(s) = (-b + s*sqrt(Δ))/(2a) 11 | a == 0 && error("a must be different than zero") 12 | t(1), t(-1) 13 | end 14 | -------------------------------------------------------------------------------- /Chapter01/11. JuliaをJupyter Notebookで使う/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.2) pkg> add IJulia 3 | 4 | julia> using IJulia 5 | julia> notebook() 6 | 7 | 8 | $ ~/.julia/packages/Conda/hsaaN/deps/usr/bin/jupyter notebook 9 | 10 | 11 | C:\> %userprofile%\.julia\packages\Conda\hsaaN\deps\usr\Scripts\jupyter-notebook 12 | 13 | 14 | ENV["JUPYTER"] = "[path to your jupyter program]" 15 | using Pkg 16 | Pkg.build("IJulia") 17 | -------------------------------------------------------------------------------- /Chapter04/05. モンテカルロシミュレーションの実行/simwalk.jl: -------------------------------------------------------------------------------- 1 | using OnlineStats, Random 2 | 3 | function simwalk() 4 | jumps = 0 5 | distance = 0.0 6 | while true 7 | jumps += 1 8 | distance += rand() 9 | distance ≥ 1.0 && return jumps 10 | end 11 | end 12 | 13 | function incremental(n) 14 | s = Mean() 15 | for i in 1:n 16 | fit!(s, simwalk()) 17 | end 18 | value(s) 19 | end 20 | 21 | -------------------------------------------------------------------------------- /Chapter06/05. イントロスペクションを使ってJuliaの数値型の構成を調べる/types.jl: -------------------------------------------------------------------------------- 1 | function printsubtypes(T, indent=0) 2 | sT = subtypes(T) 3 | println(" "^indent, T, isempty(sT) ? "" : ":") 4 | for S in sT 5 | printsubtypes(S, indent + 1) 6 | end 7 | end 8 | 9 | function supertypes(T) 10 | print(T) 11 | if T != Any 12 | print(" <: ") 13 | S = supertype(T) 14 | supertypes(S) 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /Chapter04/01. 行列処理を高速化する/sums.jl: -------------------------------------------------------------------------------- 1 | function sum_by_col(x) 2 | s = zero(eltype(x)) 3 | for j in 1:size(x, 2) 4 | for i in 1:size(x, 1) 5 | s += x[i, j] 6 | end 7 | end 8 | s 9 | end 10 | 11 | function sum_by_row(x) 12 | s = zero(eltype(x)) 13 | for i in 1:size(x, 1) 14 | for j in 1:size(x, 2) 15 | s += x[i, j] 16 | end 17 | end 18 | s 19 | end 20 | -------------------------------------------------------------------------------- /Chapter01/13. ターミナルしか使えないクラウド環境でJupyter Notebookを使う/commands.txt: -------------------------------------------------------------------------------- 1 | $ chmod 400 keyfile.pem 2 | 3 | 4 | $ ssh -i path/to/keyfile.pem -L8888:127.0.0.1:8888 ubuntu@[enter_hostname_here] 5 | 6 | $ ~/.julia/packages/Conda/hsaaN/deps/usr/bin/jupyter lab 7 | 8 | C:\> %userprofile%\.julia\packages\Conda\hsaaN\deps\usr\Scripts\jupyter-lab 9 | 10 | 11 | julia> using IJulia 12 | julia> notebook(detached=true) 13 | julia> run(`$(IJulia.notebook_cmd[1]) notebook list`) -------------------------------------------------------------------------------- /Chapter02/04. 正規表現を使ってGitログを解析する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("parselog.jl") 2 | julia> ] 3 | (v1.2) pkg> dev DataFrames 4 | julia> gitstats(joinpath(DEPOT_PATH[1], "dev/DataFrames")) 5 | julia> ] 6 | (v1.2) pkg> free DataFrames 7 | 8 | 9 | julia> function f() 10 | for i in 1:2 11 | if i == 1 12 | j = 1 13 | else 14 | println(j) 15 | end 16 | end 17 | end 18 | julia> f() 19 | -------------------------------------------------------------------------------- /Chapter06/07. 変更可能型と変更不能型の性能差を確認する/work.jl: -------------------------------------------------------------------------------- 1 | struct T1 2 | x::NTuple{1000, Int} 3 | y::Int 4 | end 5 | 6 | mutable struct T2 7 | x::NTuple{1000, Int} 8 | y::Int 9 | end 10 | 11 | function worker1() 12 | p = T1(ntuple(x->1, 1000), 0) 13 | for i in 1:10^6 14 | p = T1(p.x, p.y+1) 15 | end 16 | p 17 | end 18 | 19 | function worker2() 20 | p = T2(ntuple(x->1, 1000), 0) 21 | for i in 1:10^6 22 | p.y += 1 23 | end 24 | p 25 | end 26 | -------------------------------------------------------------------------------- /Chapter07/10. データフレーム変換を繰り返してピボットテーブルを作成する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using DataFrames, DataFramesMeta, CSV, Statistics 2 | julia> df = CSV.read("iris.csv", footerskip=1, 3 | header=["PetalLength", "PetalWidth", 4 | "SepalLength", "SepalWidth", "Class"]); 5 | 6 | 7 | julia> @linq df |> 8 | where(:Class .== "Iris-setosa") |> 9 | by([:PetalLength, :SepalWidth], meanSL = mean(:SepalLength)) |> 10 | unstack(:SepalWidth, :meanSL) 11 | -------------------------------------------------------------------------------- /Chapter01/03. Juliaをテキストエディタで使う/commands.txt: -------------------------------------------------------------------------------- 1 | $ wget -P ~/ https://raw.githubusercontent.com/Naereen/nanorc/master/julia.nanorc 2 | $ echo include \"~/julia.nanorc\" >> ~/.nanorc 3 | 4 | 5 | git clone git://github.com/JuliaEditorSupport/julia-vim.git 6 | mkdir -p ~/.vim 7 | cp -R julia-vim/* ~/.vim 8 | 9 | 10 | wget -P ~/julia-emacs/ https://raw.githubusercontent.com/JuliaEditorSupport/julia-emacs/master/julia-mode.el 11 | echo "(add-to-list 'load-path \"~/julia-emacs\")" >> ~/.emacs 12 | echo "(require 'julia-mode)" >> ~/.emacs -------------------------------------------------------------------------------- /Chapter01/12. JuliaをJupyterLabで使う/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.2) pkg> add Conda 3 | 4 | julia> using Conda 5 | julia> Conda.add("jupyterlab") 6 | 7 | 8 | $ ~/.julia/packages/Conda/hsaaN/deps/usr/bin/jupyter lab 9 | 10 | 11 | C:\> %userprofile%\.julia\packages\Conda\hsaaN\deps\usr\Scripts\jupyter-lab 12 | 13 | 14 | $ wget https://repo.anaconda.com/archive/Anaconda3-5.3.0-Linux-x86_64.sh 15 | 16 | $ sudo bash Anaconda3-5.3.0-Linux-x86_64.sh 17 | 18 | $ /home/ubuntu/anaconda3/bin/jupyter lab 19 | 20 | 21 | C:\> C:\ProgramData\Anaconda3\Scripts\jupyter-lab.exe 22 | -------------------------------------------------------------------------------- /Chapter04/07. 複素数を用いた計算/commands.txt: -------------------------------------------------------------------------------- 1 | julia> function juliapoint(z, c) 2 | for n in 1:255 3 | z = z^2 + c 4 | abs2(z) > 4 && return n 5 | end 6 | return 256 7 | end 8 | julia> using PyPlot 9 | julia> xs = -1.4:0.002:1.4; 10 | julia> ys = -1.05:0.002:1.05; 11 | julia> c = -0.4+0.6im; 12 | julia> res = [juliapoint(complex(x, y), c) for y in ys, x in xs]; 13 | julia> imshow(res, extent=[extrema(xs)..., extrema(ys)...], cmap="gray_r") 14 | 15 | 16 | julia> size(res) 17 | julia> [extrema(xs)..., extrema(ys)...] 18 | -------------------------------------------------------------------------------- /Chapter08/01. Revise.jlを用いてモジュールを開発する/Module1.jl: -------------------------------------------------------------------------------- 1 | module Module1 2 | 3 | using HTTP 4 | using JSON 5 | 6 | export getcoinprices 7 | 8 | function getcoinprices(dateFrom::String,dateTo::String) 9 | # dateFrom > dateTo && return Dict() 10 | url = string("https://api.coindesk.com/v1/bpi/historical/close.json?currency=USD&start=", 11 | dateFrom, "&end=", dateTo) 12 | res = HTTP.request("GET", url ,verbose=0) 13 | dat = JSON.parse(join(readlines(IOBuffer(res.body)), " ")) 14 | haskey(dat, "bpi") ? dat["bpi"] : Dict() 15 | end 16 | 17 | end # module 18 | -------------------------------------------------------------------------------- /Chapter08/07. フロジェクトの依存関係を管理する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | 3 | (v1.2) pkg> generate Project 4 | julia> using StaticArrays 5 | julia> ] 6 | (v1.2) pkg> activate Project 7 | (Project) pkg> add StaticArrays 8 | julia> using StaticArrays 9 | julia> print(read("Project/Project.toml", String)) 10 | julia> exit() 11 | 12 | $ julia 13 | 14 | julia> ] 15 | julia> using StaticArrays 16 | julia> ] 17 | (v1.2) pkg> activate Project 18 | julia> using StaticArrays 19 | 20 | 21 | julia> LOAD_PATH 22 | julia> using Pkg 23 | julia> pop!(LOAD_PATH) 24 | julia> LOAD_PATH 25 | julia> using Statistics 26 | -------------------------------------------------------------------------------- /Chapter02/05. 標準的でない基準でデータをソートする/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using Random, LinearAlgebra 2 | julia> Random.seed!(1); 3 | julia> x = rand(1000, 1000); 4 | julia> x1 = sortslices(x, by=norm, dims=1); 5 | julia> x2 = sortslices(x, lt=(x,y) -> norm(x) < norm(y), dims=1); 6 | julia> x3 = x[sortperm([norm(view(x, i, :)) for i in 1:size(x, 1)]), :]; 7 | julia> issorted(sum(x1.^2, dims=2)) 8 | julia> x1 == x2 == x3 9 | julia> @time x1 = sortslices(x, by=norm, dims=1); 10 | julia> @time x2 = sortslices(x, lt=(x,y) -> norm(x) < norm(y), dims=1); 11 | julia> @time x3 = x[sortperm([norm(view(x, i, :)) for i in 1:size(x, 1)]), :]; 12 | -------------------------------------------------------------------------------- /Chapter05/07. 名前付きタプルの使い方/commands.txt: -------------------------------------------------------------------------------- 1 | julia> t1 = (1, 2) 2 | julia> t2 = (1.0, 2) 3 | julia> nt1 = (a=1, b=2) 4 | julia> nt2 = (a=1.0, b=2) 5 | julia> t1 isa Tuple{Real, Int} 6 | julia> t2 isa Tuple{Real, Int} 7 | julia> nt1 isa NamedTuple{(:a, :b), Tuple{Real,Int64}} 8 | julia> nt2 isa NamedTuple{(:a, :b), Tuple{Real,Int64}} 9 | julia> nt1 isa NamedTuple{(:a, :b), Tuple{T,Int64}} where T<:Real 10 | julia> nt2 isa NamedTuple{(:a, :b), Tuple{T,Int64}} where T<:Real 11 | 12 | 13 | julia> [t1, t2] 14 | julia> [nt1, nt2] 15 | 16 | 17 | julia> foo(x; y::Integer) = (x, y) 18 | julia> @code_lowered foo(1, y=1) 19 | -------------------------------------------------------------------------------- /Chapter07/03. インターネット上のCSVデータを読み込む/commands.txt: -------------------------------------------------------------------------------- 1 | julia> download("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data", 2 | "iris.csv") 3 | julia> isfile("iris.csv") 4 | julia> readline("iris.csv") 5 | julia> using CSV, DataFrames 6 | julia> df = CSV.read("iris.csv", 7 | header=["PetalLength", "PetalWidth", 8 | "SepalLength", "SepalWidth", "Class"]); 9 | julia> describe(df) 10 | julia> last(df, 6) 11 | julia> eltypes(df) 12 | julia> df = disallowmissing!(df[1:end-1, :]); 13 | julia> eltypes(df) 14 | julia> CSV.write("iris2.csv", df); 15 | -------------------------------------------------------------------------------- /Chapter02/07. UTF-8文字列を扱う/commands.txt: -------------------------------------------------------------------------------- 1 | julia> hello = readlines("hello.txt") 2 | julia> hello_dict = Dict(map(x->Pair(x...), split.(hello, ' '))) 3 | julia> chinese = hello_dict["Chinese"] 4 | julia> codeunits(chinese) 5 | julia> ncodeunits(chinese) 6 | julia> collect(chinese) 7 | julia> length(chinese) 8 | julia> isvalid.(chinese, 1:ncodeunits(chinese)) 9 | julia> thisind.(chinese, 0:ncodeunits(chinese)+1) 10 | julia> nextind.(chinese, 0:ncodeunits(chinese)) 11 | julia> prevind.(chinese, 1:ncodeunits(chinese)+1) 12 | 13 | 14 | julia> s=String([0xff, 0xff, 0xff]) 15 | julia> isvalid(s) 16 | julia> collect(s) 17 | julia> s[1] 18 | -------------------------------------------------------------------------------- /Chapter04/08. 単純な最適化を書いてみる/marquardt.jl: -------------------------------------------------------------------------------- 1 | using ForwardDiff 2 | using LinearAlgebra 3 | 4 | function marquardt(f, x₀; ε=1e-6, maxiter=1000, λ=10.0^4, α=2) 5 | x = x₀ 6 | fx = f(x) 7 | for i in 1:maxiter 8 | g = ForwardDiff.gradient(f, x) 9 | norm(g) ≤ ε && return (x=x, converged=true, iters=i) 10 | x′ = x .- (ForwardDiff.hessian(f, x) + λ*I) \ g 11 | fx′ = f(x′) 12 | if fx′ < fx 13 | λ *= 0.5 14 | fx = fx′ 15 | x = x′ 16 | else 17 | λ *= 2.0 18 | end 19 | end 20 | (x=x, converged=false, iters=maxiter) 21 | end 22 | -------------------------------------------------------------------------------- /Chapter04/11. @inbounds を使って高速化する/inbounds.jl: -------------------------------------------------------------------------------- 1 | using BenchmarkTools 2 | 3 | mode = ["normal", "@inbounds"] 4 | i = 0 5 | for inbounds in ["", "@inbounds"] 6 | global i += 1 7 | eval(Meta.parse("""function f$i(x::AbstractArray{<:Real}) 8 | y = 0 9 | $inbounds for i in eachindex(x) 10 | y += x[i] > 0.5 11 | end 12 | y 13 | end""")) 14 | end 15 | 16 | x = rand(10^7) 17 | for (idx, f) in enumerate([f1, f2]) 18 | println("\n", mode[idx]) 19 | @btime $f($x) 20 | end 21 | -------------------------------------------------------------------------------- /Chapter06/08. 型安定性を保証する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("quad.jl") 2 | julia> using BenchmarkTools 3 | julia> @benchmark quadratic1(1,2,3) 4 | julia> @benchmark quadratic2(1,2,3) 5 | julia> using Test 6 | julia> @inferred quadratic1(1,2,3) 7 | julia> @inferred quadratic2(1,2,3) 8 | julia> @code_warntype quadratic1(1,2,3) 9 | julia> @code_warntype quadratic2(1,2,3) 10 | 11 | 12 | julia> function quadratic3(a, b, c) 13 | t(s,Δ) = (-b + s*sqrt(Δ))/(2a) 14 | a == 0 && error("a must be different than zero") 15 | Δ = Complex(b^2-4*a*c) 16 | t(1,Δ), t(-1,Δ) 17 | end 18 | julia> @benchmark quadratic3(1,2,3) 19 | -------------------------------------------------------------------------------- /Chapter02/02. 行列乗算を高速に行う/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("fastmatmul.jl") 2 | julia> using BenchmarkTools 3 | julia> A = ones(5, 5000); 4 | julia> B = ones(5000, 5); 5 | julia> @btime *(repeat([A,B], outer=10)...); 6 | julia> @btime fastmatmul(repeat([A,B], outer=10)...); 7 | 8 | 9 | julia> macro fastmatmul(ex::Expr) 10 | ex.head == :call || throw(ArgumentError("expression must be a call")) 11 | ex.args[1] == :(*) || throw(ArgumentError("only multiplication is allowed")) 12 | new_ex = deepcopy(ex) 13 | new_ex.args[1] = :fastmatmul 14 | esc(new_ex) 15 | end 16 | julia> @fastmatmul ones(2,3)*ones(3,4)*ones(4,5) 17 | -------------------------------------------------------------------------------- /Chapter03/08. Juliaをバックグラウンドプロセスとして使う/3.08 Juliaをバックグラウンドプロセスとして使う.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "このレシピにはコードがありません。" 8 | ] 9 | } 10 | ], 11 | "metadata": { 12 | "@webio": { 13 | "lastCommId": null, 14 | "lastKernelId": null 15 | }, 16 | "kernelspec": { 17 | "display_name": "Julia 1.2.0", 18 | "language": "julia", 19 | "name": "julia-1.2" 20 | }, 21 | "language_info": { 22 | "file_extension": ".jl", 23 | "mimetype": "application/julia", 24 | "name": "julia", 25 | "version": "1.2.0" 26 | } 27 | }, 28 | "nbformat": 4, 29 | "nbformat_minor": 2 30 | } 31 | -------------------------------------------------------------------------------- /Chapter07/06. データフレームを使って分割‐適用‐結合を行う/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using CSV, DataFrames 2 | julia> df = CSV.read("iris.csv", footerskip=1, 3 | header=["PetalLength", "PetalWidth", 4 | "SepalLength", "SepalWidth", "Class"]); 5 | julia> describe(df, :mean, :nmissing) 6 | julia> using Statistics 7 | julia> by(df, :Class) do x 8 | DataFrame(n = nrow(x), 9 | mean = mean(x.SepalWidth), 10 | std = std(x.SepalWidth)) 11 | end 12 | 13 | 14 | julia> by(df, :Class, x -> describe(x, :mean, :nunique)) 15 | 16 | 17 | julia> adf = aggregate(df, :Class, maximum); 18 | julia> describe(adf, :mean) 19 | julia> summary(adf) 20 | -------------------------------------------------------------------------------- /Chapter07/04. カテゴリデータを処理する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using DataFrames 2 | julia> grade_levels = ["F"; [x*y for x in 'D':-1:'A' for y in ["-", "", "+"]]] 3 | julia> using Random 4 | julia> Random.seed!(1); 5 | julia> grades = categorical(rand(grade_levels, 100), ordered=true); 6 | julia> levels!(grades, grade_levels); 7 | julia> df = DataFrame(id=eachindex(grades), grades = grades); 8 | julia> isordered(grades) 9 | julia> levels(grades) 10 | julia> describe(df, stats=:eltype) 11 | julia> filter(x -> x.grades > "A-", df) 12 | 13 | 14 | julia> grades[1:10] .* " grade" 15 | 16 | 17 | julia> x = repeat(["a"^20, "b"^20], 1000); 18 | julia> y = categorical(x); 19 | julia> Base.summarysize(x) 20 | julia> Base.summarysize(y) 21 | -------------------------------------------------------------------------------- /Chapter04/12. ベクトルの集合から行列を作る/commands.txt: -------------------------------------------------------------------------------- 1 | julia> input = [[10i+1:10i+5;] for i in 1:3] 2 | julia> output = [10i+j for i in 1:3, j in 1:5] 3 | julia> hcat(input...) 4 | julia> vcat(x...) 5 | julia> reduce(vcat, transpose.(input)) 6 | julia> hcat(transpose.(input)...) 7 | julia> transpose(hcat(input...)) 8 | julia> vcat(reshape.(input, 1, :)...) 9 | 10 | 11 | julia> hcat(input[1]) 12 | julia> x = [1 2; 3 4] 13 | julia> y = transpose(x) 14 | julia> y[1] = 100 15 | julia> x 16 | 17 | 18 | julia> hcat(permutedims.(input)...) 19 | julia> vcat(permutedims.(input)...) 20 | julia> permutedims(hcat(input...)) 21 | julia> [output[i,:] for i in 1:size(output, 1)] 22 | julia> [input[i][j] for i in 1:length(input), j in 1:length(input[1])] 23 | -------------------------------------------------------------------------------- /Chapter05/04. 関数型でプログラミングする/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.2) pkg> add UnicodePlots 3 | 4 | 5 | function deriv(f::Function)::Function 6 | h = √eps() 7 | f1(x) = (f(x+h) - f(x))/h 8 | return f1 9 | end 10 | 11 | using UnicodePlots 12 | f(x) = 2x*x + 5x - 4; 13 | x = -5:3; 14 | 15 | plot = lineplot(x, f.(x), width=45, height=15, canvas=DotCanvas, name="f(x)"); 16 | plot = lineplot!(plot, x, deriv(f).(x), name="f'(x)") 17 | 18 | function q_solve(f) 19 | c = f(0) 20 | f1 = deriv(f) 21 | b = f1(0) 22 | a = f(1)-b-c 23 | d = √(b*b-4*a*c) 24 | return ((-b-d)/(2*a),(-b+d)/(2*a)) 25 | end 26 | 27 | julia> q_solve(x -> (x-1)*(x+7)) 28 | 29 | julia> q_solve(x -> x*x + 1) 30 | 31 | julia> q_solve(x -> x*x + 1 + 0im) 32 | -------------------------------------------------------------------------------- /Chapter02/01. 配列中の最小要素のインデックスを取得する/randargmin2.jl: -------------------------------------------------------------------------------- 1 | function randargmin2(a) 2 | indices = eachindex(a) 3 | y = iterate(indices) 4 | y === nothing && throw(ArgumentError("collection must be non-empty")) 5 | (idx, state) = y 6 | minval = a[idx] 7 | bestidx = idx 8 | bestcount = 1 9 | y = iterate(indices, state) 10 | while y !== nothing 11 | (idx, state) = y 12 | curval = a[idx] 13 | if isless(curval, minval) 14 | minval = curval 15 | bestidx = idx 16 | bestcount = 1 17 | elseif isequal(curval, minval) 18 | bestcount += 1 19 | rand() * bestcount < 1 && (bestidx = idx) 20 | end 21 | y = iterate(indices, state) 22 | end 23 | bestidx 24 | end 25 | -------------------------------------------------------------------------------- /Chapter08/02. コードのベンチマーク/commands.txt: -------------------------------------------------------------------------------- 1 | julia> function f1(n::Integer) 2 | n > 0 || error("n must be a positive number") 3 | A = rand(10,10) 4 | [A*rand(10) for i in 1:n] 5 | end 6 | julia> @time f1(10^6); 7 | julia> @time f1(10^6); 8 | julia> using BenchmarkTools 9 | julia> @benchmark f1(10^6) 10 | julia> using Random 11 | julia> function f2(n::Integer) 12 | n > 0 || error("n must be a positive number") 13 | A = rand(10,10) 14 | x = rand(10) 15 | [A*rand!(x) for i in 1:n] 16 | end 17 | julia> @benchmark f2(10^6) 18 | 19 | 20 | julia> @which f1(10^6) 21 | julia> @elapsed f1(10^6) 22 | julia> @allocated f1(10^6) 23 | julia> x = 10 24 | julia> @benchmark rand(x) 25 | julia> @benchmark rand($x) 26 | -------------------------------------------------------------------------------- /Chapter02/01. 配列中の最小要素のインデックスを取得する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> function allargmin(a) 2 | isempty(a) && return Int[] 3 | m = minimum(a) 4 | filter(i -> a[i] == m, eachindex(a)) 5 | end 6 | julia> randargmin1(a) = rand(allargmin(a)) 7 | julia> include("randargmin2.jl") 8 | julia> using StatsBase 9 | julia> x = [1, 2, 3, 1, 2, 3, 1, 1] 10 | julia> countmap([randargmin1(x) for i in 1:10^6]) 11 | julia> countmap([randargmin2(x) for i in 1:10^6]) 12 | julia> x = rand(1:10, 1000); 13 | julia> using BenchmarkTools 14 | julia> @btime randargmin1($x); 15 | julia> @btime randargmin2($x); 16 | 17 | 18 | julia> 0.0 == -0.0, -0.0 < 0.0 19 | julia> isequal(0.0, -0.0), isless(-0.0, 0.0) 20 | julia> NaN == NaN, NaN < NaN 21 | julia> isequal(NaN, NaN), isless(NaN, NaN) 22 | -------------------------------------------------------------------------------- /Chapter07/08. データフレームの同一性を判定する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using CSV, DataFrames 2 | julia> df1 = CSV.read("grades.csv") 3 | 4 | 5 | julia> using Random 6 | julia> Random.seed!(1); 7 | julia> df2 = df1[shuffle(axes(df1, 1)), shuffle(axes(df1, 2))]; 8 | julia> res = join(df1, df2, kind=:outer, 9 | on=union(names(df1), names(df2)), 10 | indicator=:check, validate=(true, true)); 11 | julia> unique(res.check) 12 | julia> res = join(df1[1:end-1,:], df2[2:end,:], kind=:outer, 13 | on=union(names(df1), names(df2)), 14 | indicator=:check, validate=(true, true)); 15 | julia> by(res, :check, nrow) 16 | 17 | 18 | julia> using StatsBase 19 | julia> df_id(df) = countmap(collect(eachrow(df[:, sort(names(df))]))) 20 | julia> df_id(df1) == df_id(df2) 21 | -------------------------------------------------------------------------------- /Chapter07/09. データフレームの行を変換する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using CSV, DataFrames 2 | julia> df = CSV.read("grades.csv"); 3 | 4 | 5 | julia> function get_grade(final, midterm, takehome) 6 | (ismissing(final) || final < 50) && return "fail" 7 | if final < 75 && coalesce(midterm, 0) < 50 && coalesce(takehome, 0) < 50 8 | "fail" 9 | else 10 | "pass" 11 | end 12 | end 13 | julia> df.grade = get_grade.(df.Final, df.Midterm, df.TakeHome); 14 | julia> df.grade2 = map(eachrow(df)) do r 15 | coalesce(r.Final, 0) < 50 && return "fail" 16 | if r.Final < 75 && coalesce(r.Midterm, 0) < 50 && coalesce(r.TakeHome < 50) 17 | "fail" 18 | else 19 | "pass" 20 | end 21 | end; 22 | julia> df.grade == df.grade2 23 | -------------------------------------------------------------------------------- /Chapter04/04. 級数の部分和によるπの近似/commands.txt: -------------------------------------------------------------------------------- 1 | julia> function our_pi(n, T) 2 | s = one(T) 3 | f = one(T) 4 | for i::T in 1:n 5 | f *= i / (2i+1) 6 | s += f 7 | end 8 | 2s 9 | end 10 | julia> for T in [Float16, Float64, BigFloat] 11 | display([our_pi(2^n, T) for n in 1:10] .- big(π)) 12 | end 13 | julia> our_pi(1000, BigFloat) - pi 14 | julia> setprecision(1000) do 15 | our_pi(1000, BigFloat)-pi 16 | end 17 | julia> our_pi(23, Rational) 18 | julia> our_pi(23, Rational{BigInt}) 19 | 20 | 21 | julia> setprecision(() -> our_pi(1000, BigFloat), 1000) - pi 22 | 23 | 24 | julia> (typeof∘one).([AbstractFloat, Rational]) 25 | julia> typeof(2*one(Rational{Integer})) 26 | julia> typeof(2*one(Rational{Signed})) 27 | julia> typeof(2*one(Rational{Unsigned})) 28 | -------------------------------------------------------------------------------- /Chapter08/05. JuliaからPythonを使う/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ENV["PYTHON"]="C:\\ProgramData\\Anaconda3\\python.exe" 2 | 3 | julia> ENV["PYTHON"]="~/anaconda3/bin/python" 4 | 5 | 6 | julia> ] 7 | (v1.2) pkg> add PyCall 8 | 9 | 10 | julia> ENV["PYTHON"]="/new/Python/installation/directory/path" 11 | 12 | (v1.2) pkg> build PyCall 13 | 14 | 15 | $ pip install scrapy 16 | 17 | using Conda 18 | Conda.add("scrapy") 19 | 20 | using PyCall 21 | 22 | ssel = pyimport("scrapy.selector") 23 | 24 | txt=""" 25 | 26 | My favorite languages 27 | 32 | 33 | """ 34 | 35 | julia> s = ssel.Selector(text=txt) 36 | julia> [strip(e.extract()) for e in s.xpath("//li/text()")] 37 | 38 | julia> pybuiltin("sorted")([3, 2, 1]) 39 | -------------------------------------------------------------------------------- /Chapter01/07. Juliaをマルチコアで使う/commands.txt: -------------------------------------------------------------------------------- 1 | $ julia --banner=no -p 2 2 | 3 | 4 | julia> using Distributed 5 | julia> nworkers() 6 | julia> exit() 7 | 8 | 9 | $ julia --banner=no -p auto -L hello.jl 10 | 11 | 12 | julia> exit() 13 | 14 | 15 | $ julia --banner=no -p auto -L hello2.jl 16 | 17 | 18 | julia> exit() 19 | 20 | 21 | # Linux bash 22 | $ export JULIA_NUM_THREADS=`nproc` 23 | $ julia -e "println(Threads.nthreads())" 24 | 25 | 26 | # Windows cmd 27 | C:\> set JULIA_NUM_THREADS=%NUMBER_OF_PROCESSORS% 28 | C:\> julia -e "println(Threads.nthreads())" 29 | 30 | 31 | D:\> julia --banner=no -p 2 -L hello2.jl 32 | 33 | 34 | julia> pwd() 35 | julia> using Distributed 36 | julia> pmap(i -> (i, myid(), pwd()), 1:nworkers()) 37 | julia> cd("C:\\") 38 | julia> pwd() 39 | julia> addprocs(2) 40 | julia> pmap(i -> (i,myid(),pwd()), 1:nworkers()) 41 | -------------------------------------------------------------------------------- /Chapter05/03. 関数を値として使う/commands.txt: -------------------------------------------------------------------------------- 1 | using Random 2 | Random.seed!(0); 3 | 4 | 5 | mutable struct Agent 6 | id::Int 7 | x::Float64 8 | y::Float64 9 | times_moved::Int 10 | end 11 | 12 | function move!(agent::Agent) 13 | angle = rand()*2π 14 | agent.x += cos(angle) 15 | agent.y += sin(angle) 16 | agent.times_moved += 1 17 | end 18 | 19 | pop = Agent.(1:30, 0, 0, 0) 20 | 21 | function step!(pop::Array{Agent,1}) 22 | sort!(pop, by = a -> √(a.x*a.x + a.y*a.y), rev=true) 23 | foreach(i -> (rand() < (1/i)) && move!(pop[i]), 1:length(pop)) 24 | end 25 | 26 | foreach(s -> step!(pop), 1:1000) 27 | 28 | julia> filter(a -> √(a.x*a.x + a.y*a.y) >= 25, pop) 29 | 30 | 31 | julia> pop = fill(Agent(1, 0.0, 0.0, 0), 3) 32 | julia> pop[2].id = 2 33 | julia> pop 34 | 35 | 36 | julia> pop=[Agent(i, 0.0, 0.0, 0) for i in 1:3] -------------------------------------------------------------------------------- /Chapter07/05. 欠損値を扱う/cor.jl: -------------------------------------------------------------------------------- 1 | using Statistics 2 | 3 | abstract type CorMethod end 4 | struct CorAll <: CorMethod end 5 | struct CorComplete <: CorMethod end 6 | struct CorPairwise <: CorMethod end 7 | 8 | function Statistics.cor(df::DataFrame; method::CorMethod=CorAll()) 9 | cor1(i, j) = nrow(df) == 0 ? missing : cor(df2[!, i], df2[!, j]) 10 | 11 | function cor2(i, j) 12 | x = dropmissing(DataFrame([df2[!, i], df2[!, j]])) 13 | nrow(x) == 0 ? missing : cor(x[!, 1], x[!, 2]) 14 | end 15 | 16 | use_cor = method == CorPairwise() ? cor2 : cor1 17 | df2 = method == CorComplete() ? dropmissing(df) : df 18 | 19 | m = Matrix{Union{Float64, Missing}}(undef, ncol(df), ncol(df)) 20 | for i in 1:ncol(df), j in i:ncol(df) 21 | m[i, j] = use_cor(i, j) 22 | m[j, i] = m[i, j] 23 | end 24 | m 25 | end 26 | -------------------------------------------------------------------------------- /Chapter04/06. 待ち行列の解析/commands.txt: -------------------------------------------------------------------------------- 1 | $ julia -i mm1.jl 2 | 3 | 4 | julia> ad = Exponential(1.3) 5 | julia> sd = Exponential(0.95) 6 | julia> @time res = [queue1(2^14, 2^12, ad, sd) for i in 1:2^12]; 7 | julia> exact = mm1_exact(ad, sd) 8 | julia> @time println(bootCI(res .- exact, mean, 0.99, 2^14)) 9 | 10 | 11 | function bootCI(data, stat::Function, CI::Float64, reps::Integer) 12 | tmp = similar(data) 13 | boot = [stat(rand!(tmp, data)) for i in 1:reps] 14 | low, high = quantile(boot, [(1-CI)/2, (1+CI)/2]) 15 | (value=stat(data), low=low, high=high) 16 | end 17 | 18 | 19 | using StatsBase, Distributions 20 | 21 | function bayesbootCI(data, stat, CI::Float64, reps::Integer) 22 | d = Dirichlet(length(data), 1) 23 | boot = [stat(data, weights(rand(d))) for i in 1:reps] 24 | low, high = quantile(boot, [(1-CI)/2, (1+CI)/2]) 25 | (value=stat(data), low=low, high=high) 26 | end 27 | -------------------------------------------------------------------------------- /Chapter02/06. 関数原像の生成-辞書とセットの機能を理解する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> preimage = Dict{Float64, Set{Tuple{Float64, Float64}}}() 2 | julia> for x in -2.0:2.0, y in -2.0:2.0 3 | k = x / y 4 | v = (x, y) 5 | if haskey(preimage, k) 6 | push!(preimage[k], v) 7 | else 8 | preimage[k] = Set([v]) 9 | end 10 | end 11 | julia> for k in sort!(collect(keys(preimage))) 12 | println(k, ":\t", join(sort!(collect(preimage[k])), ",\t")) 13 | end 14 | 15 | 16 | julia> f1 = NaN 17 | julia> f2 = -NaN 18 | julia> reinterpret(UInt64, f1) 19 | julia> reinterpret(UInt64, f2) 20 | julia> f1 === f2 21 | julia> isequal(f1, f2) 22 | julia> Set([f1, f2]) 23 | julia> Set([0.0, -0.0]) 24 | julia> reinterpret(UInt64, 0.0) 25 | julia> reinterpret(UInt64, -0.0) 26 | julia> 1/0.0 27 | julia> 1/-0.0 28 | julia> unique([-0.0, 0.0, 0.0, -0.0, NaN, -NaN]) 29 | -------------------------------------------------------------------------------- /Chapter04/13. 配列ビューを使って使用メモリ量を減らす/sudoku.jl: -------------------------------------------------------------------------------- 1 | blockvalid(x, v) = count(isequal(v), x) ≤ 1 2 | 3 | function backtrack!(x) 4 | pos = findfirst(isequal(0), x) 5 | isa(pos, Nothing) && return true 6 | iloc = 3div(pos[1]-1, 3) .+ (1:3) 7 | jloc = 3div(pos[2]-1, 3) .+ (1:3) 8 | for k in 1:9 9 | x[pos] = k 10 | blockvalid(view(x, pos[1], :), k) || continue 11 | blockvalid(view(x, :, pos[2]), k) || continue 12 | blockvalid(view(x, iloc, jloc), k) || continue 13 | backtrack!(x) && return true 14 | end 15 | x[pos] = 0 16 | return false 17 | end 18 | 19 | function ssolve(lines, i) 20 | t = [lines[10i-j][k] - '0' for j in 8:-1:0, k in 1:9] 21 | backtrack!(t) 22 | sum([100, 10, 1] .* t[1, 1:3]) 23 | end 24 | 25 | lines = readlines("p096_sudoku.txt") 26 | @time sum(ssolve(lines, i) for i in 1:50) 27 | @time sum(ssolve(lines, i) for i in 1:50) 28 | -------------------------------------------------------------------------------- /Chapter04/13. 配列ビューを使って使用メモリ量を減らす/commands.txt: -------------------------------------------------------------------------------- 1 | $ julia sudoku.jl 2 | 3 | 4 | function backtrack!(x, z, idx) 5 | idx > length(z) && return true 6 | pos = z[idx] 7 | iloc = 3div(pos[1]-1, 3) 8 | jloc = 3div(pos[2]-1, 3) 9 | filled = 0 10 | @inbounds for k in 1:9 11 | filled |= x[pos[1], k] | x[k, pos[2]] 12 | end 13 | @inbounds for k1 in 1:3, k2 in 1:3 14 | filled |= x[iloc+k1, jloc+k2] 15 | end 16 | @inbounds for i in 1:9 17 | k = 1< print(json_txt) 11 | 12 | julia> JSON.parse(json_txt) 13 | 14 | data = Dict{Int64,Union{Int64, String}}(1=>"text", 2=>999); 15 | 16 | julia> print(JSON.json(data)) 17 | 18 | f = open("file.json", "w"); 19 | JSON.print(f,data); 20 | close(f); 21 | 22 | f = open("file.json", "r"); 23 | data2 = JSON.parse(f); 24 | close(f); 25 | 26 | data_copy = JSON.parsefile("file.json") 27 | 28 | julia> a = reshape(collect(1:8), 2, 4) 29 | 30 | julia> b = JSON.json(a) 31 | 32 | julia> JSON.parse(b) 33 | 34 | julia> struct S; x1::Int64; x2::Float64; x3::String; end 35 | 36 | julia> s = S(1, 4.5, "test"); 37 | 38 | julia> println(JSON.json(s)) 39 | 40 | julia> JSON.parse(JSON.json(s)) 41 | -------------------------------------------------------------------------------- /Chapter07/07. 縦型データフレームと横型データフレームを変換する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using CSV, DataFrames 2 | julia> df = CSV.read("iris.csv", footerskip=1, 3 | header=["PetalLength", "PetalWidth", 4 | "SepalLength", "SepalWidth", "Class"]); 5 | 6 | 7 | julia> df.id = axes(df, 1); 8 | julia> sdf = stack(df) 9 | julia> describe(sdf, :min, :max) 10 | julia> udf = unstack(sdf, :variable, :value); 11 | julia> names(udf) 12 | julia> permutecols!(udf, names(df)); 13 | julia> df == udf 14 | julia> using Statistics 15 | julia> agg = by(sdf, [:Class, :variable], 16 | x -> DataFrame(value=mean(x.value), n = nrow(x))) 17 | julia> agg2 = unstack(agg, :Class, :variable, :value); 18 | julia> agg3 = by(df, :Class) do x 19 | DataFrame(PetalLength=mean(x.PetalLength), 20 | PetalWidth=mean(x.PetalWidth), 21 | SepalLength=mean(x.SepalLength), 22 | SepalWidth=mean(x.SepalWidth)); 23 | end; 24 | julia> agg2 == agg3 25 | -------------------------------------------------------------------------------- /Chapter08/04. コードのログを取る/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using Logging 2 | julia> function f(x) 3 | y = Set(x) 4 | for v in x 5 | pop!(y, v) 6 | end 7 | end 8 | julia> f([1, 2, 3]) 9 | julia> f([1, 2, 1, 3]) 10 | julia> function f(x) 11 | y = Set(x) 12 | for v in x 13 | @debug v, y, (v in y) 14 | pop!(y, v) 15 | end 16 | end 17 | julia> old = global_logger(ConsoleLogger(stderr, Logging.Debug)); 18 | julia> f([1, 2, 1, 3]) 19 | julia> function f(x) 20 | y = Set(x) 21 | for v in x 22 | if v in y 23 | @debug v, y, (v in y) 24 | pop!(y, v) 25 | else 26 | @debug "$v not found" 27 | end 28 | end 29 | end 30 | julia> f([1, 2, 1, 3]) 31 | julia> global_logger(old); 32 | julia> f([1, 2, 1, 3]) 33 | 34 | 35 | julia> global_logger(NullLogger()) 36 | julia> @error "Important error" 37 | -------------------------------------------------------------------------------- /Chapter03/10. Featherデータを扱う/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.2) pkg> add DataFrames 3 | (v1.2) pkg> add Feather 4 | (v1.2) pkg> add RCall 5 | (v1.2) pkg> add PyCall 6 | (v1.2) pkg> add Conda 7 | 8 | 9 | using Feather 10 | using DataFrames 11 | 12 | using Random 13 | Random.seed!(0); 14 | 15 | julia> df = DataFrame(x1=[1:3...,missing], 16 | x2=rand(4), 17 | x3=rand(1:10,4)) 18 | 19 | julia> Feather.write("df.dat", df); 20 | 21 | julia> df2 = Feather.read("df.dat") 22 | 23 | julia> isequal(df,df2) 24 | 25 | julia> describe(df)[[1:3..., 5, 7, 8]] 26 | 27 | julia> describe(df2)[[1:3..., 5, 7, 8]] 28 | 29 | using RCall 30 | 31 | julia> $ 32 | 33 | R> library(feather) 34 | R> dfR <- read_feather("df.dat") 35 | 36 | R> dfR 37 | 38 | julia> dfR = @rget dfR 39 | 40 | julia> describe(dfR)[[1:3..., 5, 7, 8]] 41 | 42 | using Conda 43 | Conda.runconda(`install feather-format -c conda-forge -y`) 44 | 45 | using PyCall 46 | feather = pyimport("feather") 47 | 48 | julia> dat = feather.read_dataframe("df.dat") 49 | -------------------------------------------------------------------------------- /Chapter04/09. 線形回帰で予測する/lm.jl: -------------------------------------------------------------------------------- 1 | using DataFrames 2 | 3 | function df2mm(df::DataFrame) 4 | n = size(df, 1) 5 | mm_raw = [fill(1.0, n, 1)] 6 | mm_name = ["const"] 7 | for (name, value) in eachcol(df) 8 | if eltype(value) <: Real 9 | push!(mm_raw, hcat(Float64.(value))) 10 | push!(mm_name, string(name)) 11 | else 12 | uvalue = unique(value) 13 | length(uvalue) == 1 && continue 14 | dvalue = Dict(v=>i for (i, v) in enumerate(uvalue)) 15 | mvalue = zeros(n, length(uvalue)) 16 | for i in 1:n 17 | mvalue[i, dvalue[value[i]]] = 1.0 18 | end 19 | push!(mm_raw, mvalue[:, 2:end]) 20 | append!(mm_name, string.(name, "_", uvalue[2:end])) 21 | end 22 | end 23 | (data=hcat(mm_raw...), names=mm_name) 24 | end 25 | 26 | function lm(df, y, xs) 27 | yv = Float64.(df[y]) 28 | xv, xn = df2mm(df[[xs;]]) 29 | params = (transpose(xv)*xv)\(transpose(xv)*yv) 30 | DataFrame(name = xn, estimate=params) 31 | end 32 | -------------------------------------------------------------------------------- /Chapter03/02. IOBufferを使って効率的なインメモリストリームを作る/commands.txt: -------------------------------------------------------------------------------- 1 | julia> function splitstring(s::AbstractString) 2 | bufs = [IOBuffer() for i in 1:2] 3 | idx = 1 4 | for c in s 5 | write(bufs[idx], c) 6 | idx = 3 - idx 7 | end 8 | @. String(take!(bufs)) 9 | end 10 | julia> s = join('1':'9', "-") 11 | julia> splitstring(s) 12 | 13 | 14 | julia> function splitstring1(s::AbstractString) 15 | bufs = [Char[] for i in 1:2] 16 | idx = 1 17 | for c in s 18 | push!(bufs[idx], c) 19 | idx = 3 - idx 20 | end 21 | join.(bufs) 22 | end 23 | julia> function splitstring2(s::AbstractString) 24 | bufs = ["" for i in 1:2] 25 | idx = 1 26 | for c in s 27 | bufs[idx] *= c 28 | idx = 3 - idx 29 | end 30 | bufs 31 | end 32 | julia> using BenchmarkTools 33 | julia> s = "1"^10^4; 34 | julia> @benchmark splitstring($s) 35 | julia> @benchmark splitstring1($s) 36 | julia> @benchmark splitstring2($s) 37 | -------------------------------------------------------------------------------- /Chapter08/03. コードのプロファイリング/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.2) pkg> add ProfileView 3 | 4 | 5 | julia> include("profiletest.jl"); 6 | julia> @time agg(timeto1, 1000, 10_000); 7 | julia> @time agg(timeto1, 1000, 10_000); 8 | 9 | julia> @allocated agg(timeto1, 1000, 10_000) 10 | 11 | using Profile 12 | using ProfileView 13 | Profile.init(delay=0.0005) 14 | 15 | Profile.clear() 16 | Profile.@profile agg(timeto1, 1000, 10_000); 17 | 18 | julia> Profile.print() 19 | 20 | julia> ProfileView.view() 21 | 22 | function timeto2(mv) 23 | x = Set{Int}() 24 | while true 25 | push!(x, rand(1:mv)) 26 | 1 in x && return length(x) 27 | end 28 | end 29 | 30 | 31 | julia> @time agg(timeto2, 1000, 10_000); 32 | julia> @time agg(timeto2, 1000, 10_000); 33 | julia> @allocated agg(timeto2, 1000, 10_000) 34 | 35 | 36 | Juno.@profiler agg(timeto1, 1000, 10_000); 37 | 38 | julia> function timeto3(mv) 39 | x = BitSet() 40 | while true 41 | push!(x, rand(1:mv)) 42 | 1 in x && return length(x) 43 | end 44 | end 45 | julia> @time agg(timeto3, 1000, 10_000); 46 | julia> @time agg(timeto3, 1000, 10_000); 47 | -------------------------------------------------------------------------------- /Chapter08/06. JuliaからRを使う/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.2) pkg> add RCall 3 | 4 | julia> ENV["R_HOME"]="/new/R/installation/directory/path" 5 | 6 | (v1.2) pkg> build RCall 7 | 8 | (v1.2) pkg> add Ditributions 9 | (v1.2) pkg> add DateFrames 10 | 11 | R> install.packages("ggplot2") 12 | 13 | 14 | using RCall 15 | 16 | using Distributions 17 | using DataFrames 18 | using Random 19 | Random.seed!(0); 20 | dat = rand(MvNormal([1 0.75; 0.75 1]), 1000); 21 | df = DataFrame(permutedims(dat)) 22 | 23 | 24 | julia> @rlibrary ggplot2 25 | julia> ggplot(df, aes(x=:x1, y=:x2)) + geom_point() 26 | 27 | 28 | julia> R"library(ggplot2)" 29 | julia> R"ggplot($df, aes(x=x1, y=x2)) + geom_point()" 30 | 31 | 32 | julia> $ 33 | R> library(ggplot2) 34 | R> ggplot($df, aes(x=x1, y=x2)) + geom_point() 35 | 36 | 37 | julia> @rput df 38 | 39 | julia> R"library(ggplot2)" 40 | julia> R"ggplot(df, aes(x=x1, y=x2)) + geom_point()" 41 | 42 | julia> $ 43 | R> library(ggplot2) 44 | R> ggplot(df, aes(x=x1, y=x2)) + geom_point() 45 | 46 | 47 | julia> using RCall 48 | julia> a = 5; 49 | julia> @rput a 50 | julia> R"b = a*2" 51 | julia> @rget b 52 | julia> b 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Chapter05/02. 多重ディスパッチで動作を切り替える/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using DataFrames 2 | julia> df = DataFrame(s = categorical(["a", "b", "c"]), 3 | n = 1.0:3.0, 4 | f = [sin, cos, missing]) 5 | julia> simpledescribe(v) = "unknown type" 6 | julia> simpledescribe(v::Vector{<:Number}) = "numeric" 7 | julia> simpledescribe(v::CategoricalArray) = "categorical" 8 | julia> simpledisplay(df) = 9 | foreach(x -> println(x[1], ": ", 10 | simpledescribe(x[2])), 11 | eachcol(df)) 12 | julia> simpledisplay(df) 13 | 14 | 15 | julia> methods(simpledescribe) 16 | 17 | 18 | julia> df = DataFrame(x=1:10^6); 19 | julia> function helper(x) 20 | s = zero(eltype(x)) 21 | for v in x 22 | s += v 23 | end 24 | s 25 | end 26 | julia> function fun1(df) 27 | s = zero(eltype(df[1])) 28 | for v in df[1] 29 | s += v 30 | end 31 | s 32 | end 33 | julia> fun2(df) = helper(df[1]) 34 | julia> using BenchmarkTools 35 | julia> @btime fun1(df) 36 | julia> @btime fun2(df) 37 | -------------------------------------------------------------------------------- /Chapter03/11. CSVファイルとFWFファイルを読み込む/commands.txt: -------------------------------------------------------------------------------- 1 | julia> csv = """a,b,c 2 | 11,2,3 3 | 4,555,6 4 | 7,8,9999""" 5 | julia> iocsv = IOBuffer(csv) 6 | julia> fwf = """a b c 7 | 11 2 3 8 | 4 555 66 9 | 7 8 9999""" 10 | julia> iofwf = IOBuffer(fwf) 11 | 12 | 13 | julia> using DelimitedFiles 14 | julia> datacsv, headercsv = readdlm(iocsv, ',', header=true) 15 | julia> headercsv 16 | julia> datacsv 17 | julia> function getsubstring(s::AbstractString, 18 | charfrom::Int, 19 | charto::Int) 20 | SubString(s, nextind.(s, 0, (charfrom, charto))...) 21 | end 22 | julia> function readfwf(io, ranges::AbstractVector{<:Pair}) 23 | datafwf = [] 24 | starts = first.(ranges) 25 | ends = last.(ranges) 26 | while !eof(io) 27 | line = readline(io) 28 | push!(datafwf, getsubstring.(line, starts, ends)) 29 | end 30 | [datafwf[i][j] for i in 1:length(datafwf), j in 1:length(ranges)] 31 | end 32 | julia> datafwf = readfwf(iofwf, [1=>2, 4=>6, 8=>11]) 33 | julia> parse.(Int, datafwf[2:end,:]) 34 | -------------------------------------------------------------------------------- /Chapter06/04. 基本型を定義する/argb.jl: -------------------------------------------------------------------------------- 1 | primitive type ARGB 32 end 2 | 3 | ARGB(c::UInt32) = reinterpret(ARGB, c) 4 | ARGB(c) = ARGB(UInt32(c)) 5 | ARGB(α::UInt8, red::UInt8, green::UInt8, blue::UInt8) = 6 | ARGB(UInt32(α) << 24 + UInt32(red) << 16 + 7 | UInt32(green) << 8 + UInt32(blue)) 8 | ARGB(α, red, green, blue) = ARGB(UInt8(α), UInt8(red), 9 | UInt8(green), UInt8(blue)) 10 | 11 | function ARGB(c::AbstractString) 12 | if !occursin(r"^#[0-9a-fA-F]{8}$", c) 13 | throw(DomainError("wrong color string: $c")) 14 | end 15 | ARGB(parse(UInt32, c[2:end], base=16)) 16 | end 17 | 18 | macro ARGB_str(s) ARGB(s) end 19 | 20 | α(c::ARGB)::UInt8 = (UInt32(c) >> 24) & 0x000000FF 21 | red(c::ARGB)::UInt8 = (UInt32(c) >> 16) & 0x000000FF 22 | green(c::ARGB)::UInt8 = (UInt32(c) >> 8) & 0x000000FF 23 | blue(c::ARGB)::UInt8 = UInt32(c) & 0x000000FF 24 | 25 | Base.UInt32(c::ARGB) = reinterpret(UInt32, c) 26 | convert(UInt32, c::ARGB) = UInt32(c) 27 | convert(ARGB, c::UInt32) = ARGB(c) 28 | Base.String(c::ARGB) = "#" * lpad(string(UInt32(c), base=16), 8, "0") 29 | convert(String, c::ARGB) = String(c) 30 | convert(ARGB, c::AbstractString) = ARGB(c) 31 | -------------------------------------------------------------------------------- /Chapter04/06. 待ち行列の解析/mm1.jl: -------------------------------------------------------------------------------- 1 | using Distributions, OnlineStats 2 | 3 | function queue1(until::Real, burnin::Real, 4 | ad::Distribution, sd::Distribution) 5 | now, nextArrival, nextDeparture = 0.0, rand(ad), Inf 6 | queue, waits = Float64[], Mean() 7 | while now < until 8 | if nextArrival < nextDeparture 9 | now = nextArrival 10 | if isempty(queue) 11 | nextDeparture = nextArrival + rand(sd) 12 | end 13 | push!(queue, nextArrival) 14 | nextArrival += rand(ad) 15 | else 16 | now = nextDeparture 17 | insystem = nextDeparture - popfirst!(queue) 18 | burnin < now < until && fit!(waits, insystem) 19 | nextDeparture += isempty(queue) ? Inf : rand(sd) 20 | end 21 | end 22 | value(waits) 23 | end 24 | 25 | mm1_exact(ad::Exponential, sd::Exponential) = 1/(1/mean(sd)-1/mean(ad)) 26 | 27 | function bootCI(data, stat::Function, CI::Float64, reps::Integer) 28 | boot = [stat(rand(data, length(data))) for i in 1:reps] 29 | low, high = quantile(boot, [(1-CI)/2, (1+CI)/2]) 30 | (value=stat(data), low=low, high=high) 31 | end 32 | -------------------------------------------------------------------------------- /Chapter06/03. ユーザ定義型を作ってみる−連結リスト/ll.jl: -------------------------------------------------------------------------------- 1 | struct ListNode{T} 2 | value::T 3 | next::Union{ListNode{T}, Nothing} 4 | end 5 | 6 | mutable struct LinkedList{T} 7 | head::Union{ListNode{T}, Nothing} 8 | end 9 | 10 | LinkedList(T::Type) = LinkedList{T}(nothing) 11 | 12 | Base.iterate(ll::LinkedList) = ll.head === nothing ? nothing : (ll.head.value, ll.head) 13 | Base.iterate(ll::LinkedList{T}, state::ListNode{T}) where T = 14 | state.next === nothing ? nothing : (state.next.value, state.next) 15 | 16 | function Base.getindex(ll::LinkedList, idx::Integer) 17 | idx < 1 && throw(BoundsError("$idx is less than 1")) 18 | for v in ll 19 | idx -= 1 20 | idx == 0 && return v 21 | end 22 | throw(BoundsError("index beyond end of linked list")) 23 | end 24 | 25 | function Base.pushfirst!(ll::LinkedList{T}, items::T...) where T 26 | for item in reverse(items) 27 | ll.head = ListNode{T}(item, ll.head) 28 | end 29 | ll 30 | end 31 | 32 | Base.show(io::IO, ll::LinkedList{T}) where T = 33 | print(io, "LinkedList{$T}[" * join(ll, ", ") * "]") 34 | 35 | Base.eltype(ll::LinkedList{T}) where T = T 36 | 37 | Base.length(ll::LinkedList) = count(v -> true, ll) 38 | 39 | Base.firstindex(ll::LinkedList) = 1 40 | Base.lastindex(ll::LinkedList) = length(ll) 41 | -------------------------------------------------------------------------------- /Chapter07/05. 欠損値を扱う/commands.txt: -------------------------------------------------------------------------------- 1 | julia> download("https://openmv.net/file/class-grades.csv", 2 | "grades.csv") 3 | julia> using CSV, DataFrames, Statistics 4 | julia> df = CSV.read("grades.csv"); 5 | 6 | 7 | julia> summary(df) 8 | julia> describe(df, stats=[:min, :max, :nmissing]) 9 | julia> CSV.validate("grades.csv") 10 | julia> [cor(df[!, i], df[!, j]) for i in axes(df, 2), j in axes(df, 2)] 11 | julia> df2 = dropmissing(df); 12 | julia> describe(df2, stats=:nmissing) 13 | julia> [cor(df2[!, i], df2[!, j]) for i in axes(df2, 2), j in axes(df2, 2)] 14 | julia> function cor2(x, y) 15 | df = dropmissing(DataFrame([x, y])) 16 | cor(df[!, 1], df[!, 2]) 17 | end 18 | julia> [cor2(df[!, i], df[!, j]) for i in axes(df, 2), j in axes(df, 2)] 19 | 20 | 21 | julia> sin(missing) 22 | julia> 1 + missing 23 | julia> s = ["a", "bb", missing, "dddd"] 24 | julia> (x -> isequal(x, missing) ? missing : length(x)).(s) 25 | julia> 1 == missing 26 | julia> missing == missing 27 | julia> 1 < missing 28 | julia> isequal(1, missing) 29 | julia> isequal(missing, missing) 30 | julia> isless(1, missing) 31 | julia> isless(Inf, missing) 32 | 33 | 34 | julia> include("cor.jl"); 35 | julia> cor(df) 36 | julia> cor(df, method=CorComplete()) 37 | julia> cor(df, method=CorPairwise()) 38 | -------------------------------------------------------------------------------- /Chapter04/05. モンテカルロシミュレーションの実行/commands.txt: -------------------------------------------------------------------------------- 1 | julia> include("simwalk.jl") 2 | julia> n = 10^6; 3 | julia> Random.seed!(1); 4 | julia> res1 = mean([simwalk() for i in 1:n]) 5 | julia> res2 = mean(map(x -> simwalk(), 1:n)) 6 | julia> res3 = mean(simwalk() for i in 1:n) 7 | julia> res4 = incremental(n) 8 | julia> n = 10^8; 9 | julia> @time mean([simwalk() for i in 1:n]); 10 | julia> @time mean(map(x -> simwalk(), 1:n)); 11 | julia> @time mean(simwalk() for i in 1:n); 12 | julia> @time incremental(n); 13 | julia> MathConstants.e - incremental(10^9) 14 | 15 | 16 | julia> function simwalk(n) 17 | jumps = 0 18 | for i in 1:n 19 | distance = 0.0 20 | while true 21 | jumps += 1 22 | distance += rand() 23 | distance ≥ 1.0 && break 24 | end 25 | end 26 | jumps / n 27 | end 28 | julia> simwalk(10^6) 29 | julia> @time simwalk(10^8) 30 | julia> function simwalk(n) 31 | jumps = Int32(0) 32 | for i in 1:n 33 | distance = 0.0 34 | while true 35 | jumps += Int32(1) 36 | distance += rand() 37 | distance ≥ 1.0 && break 38 | end 39 | end 40 | jumps / n 41 | end 42 | -------------------------------------------------------------------------------- /Chapter06/06. 静的配列を利用する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.2) pkg> add StaticArrays 3 | (v1.2) pkg> add BenchmarkTools 4 | 5 | 6 | using StaticArrays 7 | using BenchmarkTools 8 | using Random 9 | 10 | function profit(demand, prices_sale, purchases, prices_purchase) 11 | sales = min.(purchases,demand) 12 | sum(sales .* prices_sale .- purchases .* prices_purchase) 13 | end 14 | 15 | Random.seed!(0); 16 | demand, prices_sale, purchases, prices_purchase = 17 | (rand(10).*100, 300:10:390, rand(10).*100, 100:10:190); 18 | 19 | Random.seed!(0); 20 | demand_s, prices_sale_s, purchases_s, prices_purchase_s = 21 | ((@SVector rand(10)).*100, SVector{10}(300:10:390), 22 | (@SVector rand(10)).*100, SVector{10}(100:10:190)); 23 | 24 | julia> @btime profit($demand, $prices_sale, 25 | $purchases, $prices_purchase) 26 | 27 | julia> @btime profit($demand_s, $prices_sale_s, 28 | $purchases_s, $prices_purchase_s) 29 | 30 | 31 | julia> a=rand(5, 5); 32 | julia> b=rand(5, 5); 33 | julia> @btime $a*$b; 34 | julia> as=rand(SMatrix{5,5}); 35 | julia> bs=rand(SMatrix{5,5}); 36 | julia> @btime $as*$bs; 37 | 38 | julia> @time m1 = rand(SMatrix{1,10}); 39 | julia> @time m1 = rand(SMatrix{1,100}); 40 | julia> @time m1 = rand(SMatrix{1,200}); 41 | julia> @time m1 = rand(SMatrix{1,500}); 42 | 43 | julia> @time m1 = rand(SMatrix{1,500}); 44 | -------------------------------------------------------------------------------- /Chapter04/02. 条件文のあるループの効率的な実行/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using Random, BenchmarkTools 2 | julia> Random.seed!(1); 3 | julia> x = randn(10^6); 4 | 5 | 6 | julia> @btime sum(v for v in x if v > 0) 7 | julia> function possum1(x) 8 | s = zero(eltype(x)) 9 | for v in x 10 | if v > 0 11 | s += v 12 | end 13 | end 14 | s 15 | end 16 | julia> @btime possum1(x) 17 | julia> function possum2a(x) 18 | s = zero(eltype(x)) 19 | for v in x 20 | s += ifelse(v > 0, v, zero(s)) 21 | end 22 | s 23 | end 24 | julia> @btime possum2a(x) 25 | julia> function possum2b(x) 26 | s = zero(eltype(x)) 27 | @simd for v in x 28 | s += ifelse(v > 0, v, zero(s)) 29 | end 30 | s 31 | end 32 | julia> @btime possum2b(x) 33 | 34 | 35 | julia> function possum2c(x) 36 | s = 0 37 | for v in x 38 | s += ifelse(v > 0, v, 0) 39 | end 40 | s 41 | end 42 | julia> @btime possum2c(x) 43 | 44 | 45 | julia> function possum2d(x::AbstractArray{T}) where T 46 | s = zero(T) 47 | @simd for v in x 48 | s += ifelse(v > 0, v, zero(T)) 49 | end 50 | s 51 | end 52 | julia> @btime possum2d(x) 53 | -------------------------------------------------------------------------------- /Chapter03/01. ストリームを管理し、ファイルに読み書きする/commands.txt: -------------------------------------------------------------------------------- 1 | $ julia iotest.jl 2 | 3 | 4 | $ more iotest.txt | julia iotest.jl 1> ioout1.txt 2> ioout2.txt 5 | $ more ioout1.txt 6 | $ more ioout2.txt 7 | 8 | 9 | $ more iotest2.txt | julia iotest.jl 1> ioout1.txt 2> ioout2.txt 10 | $ more ioout1.txt 11 | $ more ioout2.txt 12 | 13 | 14 | julia> f = open("my_data.txt", "w") 15 | 16 | julia> write(f, "first line\nsecond line\n") 17 | 18 | julia> println(f, "last line") 19 | julia> close(f) 20 | 21 | 22 | julia> f = open("my_data.txt", "r") 23 | julia> readline(f) 24 | julia> readline(f) 25 | julia> readline(f) 26 | 27 | julia> eof(f) 28 | 29 | julia> readline(f) 30 | 31 | julia> close(f) 32 | 33 | 34 | julia> lines = open("my_data.txt", "r") do f 35 | readlines(f) 36 | end 37 | 38 | 39 | julia> data = open("my_data.txt", "r") do f 40 | read(f) 41 | end 42 | 43 | julia> text = String(data); 44 | 45 | julia> length(data) 46 | 47 | julia> text = read(f, String); 48 | 49 | 50 | julia> ] 51 | 52 | (v1.0) pkg> add StringEncodings 53 | 54 | 55 | using StringEncodings 56 | data = open("my_data.txt", "r") do f 57 | read(f) 58 | end; 59 | txt = StringEncodings.decode(data,"Windows-1250") 60 | 61 | 62 | open("my_data.txt", "w") do f 63 | write(f, "line\nsecond line") 64 | end 65 | -------------------------------------------------------------------------------- /Chapter03/04. 簡単なRESTfulサービスを作ってみる/3.04 簡単なRESTfulサービスを作ってみる-ZeroMQ Web Client.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "HTTP.Messages.Response:\n", 12 | "\"\"\"\n", 13 | "HTTP/1.1 200 OK\r\n", 14 | "Content-Type: application/json;charset=utf-8\r\n", 15 | "Transfer-Encoding: chunked\r\n", 16 | "\r\n", 17 | "{\"data\":1114,\"code\":0}\"\"\"" 18 | ] 19 | }, 20 | "execution_count": 2, 21 | "metadata": {}, 22 | "output_type": "execute_result" 23 | } 24 | ], 25 | "source": [ 26 | "using HTTP\n", 27 | "res = HTTP.get(\"http://127.0.0.1:8008/testfn/5/9?optarg1=100&optarg2=1000 \")" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [] 36 | } 37 | ], 38 | "metadata": { 39 | "@webio": { 40 | "lastCommId": null, 41 | "lastKernelId": null 42 | }, 43 | "kernelspec": { 44 | "display_name": "Julia 1.2.0", 45 | "language": "julia", 46 | "name": "julia-1.2" 47 | }, 48 | "language_info": { 49 | "file_extension": ".jl", 50 | "mimetype": "application/julia", 51 | "name": "julia", 52 | "version": "1.2.0" 53 | } 54 | }, 55 | "nbformat": 4, 56 | "nbformat_minor": 2 57 | } 58 | -------------------------------------------------------------------------------- /Chapter03/04. 簡単なRESTfulサービスを作ってみる/3.04 簡単なRESTfulサービスを作ってみる - クライアント.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "5-element Array{String,1}:\n", 12 | " \"\" \n", 13 | " \"HTTP/1.1 200 OK\" \n", 14 | " \"Content-Type: text/html\" \n", 15 | " \"\" \n", 16 | " \"3*7=21\"" 17 | ] 18 | }, 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "output_type": "execute_result" 22 | } 23 | ], 24 | "source": [ 25 | "using Sockets\n", 26 | "\n", 27 | "client = Sockets.connect(\"127.0.0.1\",8080) \n", 28 | "write(client, \"GET /3*7\\n\")\n", 29 | "\n", 30 | "readlines(client)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [] 39 | } 40 | ], 41 | "metadata": { 42 | "kernelspec": { 43 | "display_name": "Julia 1.2.0", 44 | "language": "julia", 45 | "name": "julia-1.2" 46 | }, 47 | "language_info": { 48 | "file_extension": ".jl", 49 | "mimetype": "application/julia", 50 | "name": "julia", 51 | "version": "1.2.0" 52 | } 53 | }, 54 | "nbformat": 4, 55 | "nbformat_minor": 2 56 | } 57 | -------------------------------------------------------------------------------- /Chapter02/03. カスタム擬似乱数生成器を実装する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using Random 2 | julia> mutable struct Xorshift <: AbstractRNG 3 | state::UInt64 4 | end 5 | julia> Xorshift() = Xorshift(rand(RandomDevice(), UInt64)) 6 | julia> Random.seed!(r::Xorshift, seed::UInt64 = rand(RandomDevice(), UInt64)) = r.state = seed 7 | julia> function xorshift_rand(r::Xorshift) 8 | state = r.state 9 | state ⊻= state << 13 10 | state ⊻= state >> 7 11 | state ⊻= state << 17 12 | r.state = state 13 | end 14 | julia> const XorshiftSamplers = Union{map(T->Random.SamplerType{T}, 15 | [Bool, UInt32, Int32, UInt64, Int64])...} 16 | julia> Base.rand(r::Xorshift, sampler::XorshiftSamplers) = xorshift_rand(r) % sampler[] 17 | julia> Random.rng_native_52(::Xorshift) = UInt64 18 | julia> using StatsBase 19 | julia> r = Xorshift(0x0139408dcbbf7a44) 20 | julia> countmap(rand(r, 1:10, 10^8)) 21 | julia> const X = zeros(Int, 5, 5) 22 | julia> foreach(i -> X[rand(r, 1:5), rand(r, 1:5)] += 1, 1:10^7) 23 | julia> 25 * X / 10^7 24 | 25 | 26 | julia> rand(Xorshift(), UInt128) 27 | julia> function Base.rand(r::Xorshift, sampler::Random.SamplerType{UInt128}) 28 | r1 = rand(r, UInt64) 29 | r2 = rand(r, UInt64) 30 | (UInt128(r1) << 64) | r2 31 | end 32 | julia> rand(Xorshift(), UInt128) 33 | julia> using BenchmarkTools 34 | julia> r = Xorshift() 35 | julia> @benchmark rand() 36 | julia> @benchmark rand($r) 37 | -------------------------------------------------------------------------------- /Chapter03/07. オブジェクトをシリアライズする/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.2) pkg> add JLD2 3 | (v1.2) pkg> add FileIO 4 | (v1.2) pkg> add BSON 5 | 6 | 7 | using Serialization 8 | 9 | x = 1:5; 10 | open(f -> serialize(f,x), "x.jls", "w"); 11 | 12 | y = open(deserialize, "x.jls"); 13 | 14 | julia> dump(y) 15 | 16 | open("data.jls", "w") do f 17 | serialize(f,Array{Int8}([1, 2, 4])); 18 | serialize(f,Dict{Int64,String}(1=>"a", 2=>"b")); 19 | end 20 | 21 | julia> f = open("data.jls", "r"); 22 | julia> deserialize(f) 23 | julia> deserialize(f) 24 | julia> close(f) 25 | 26 | 27 | using JLD2 28 | using FileIO 29 | 30 | x1 = 1:5; 31 | x2 = rand(3); 32 | file = File(format"JLD2","myfile.jld2") 33 | save(file, "x1", x1, "x2", x2) 34 | 35 | julia> data = load(file) 36 | 37 | julia> dump(data["x1"]) 38 | 39 | 40 | using BSON 41 | 42 | x = 1:5 43 | d = Dict{Int64, String}(1=>"a", 2=>"b") 44 | e = 5+3im 45 | 46 | f = open("data.bson", "w") 47 | bson(f, Dict("x" => x, "d" => d)) 48 | bson(f, Dict("e" => e)) 49 | close(f) 50 | 51 | julia> f = open("data.bson", "r"); 52 | julia> BSON.load(f) 53 | julia> BSON.load(f) 54 | julia> close(f) 55 | 56 | julia> ] 57 | (v1.2) pkg> add CodecZlib 58 | 59 | using CodecZlib 60 | using Serialization 61 | d=Dict([("txt", collect(1:1000000))]); 62 | open("big2.bin", "w") do f 63 | comp = DeflateCompressorStream(f); 64 | serialize(comp, d); 65 | close(comp); 66 | end 67 | 68 | f=open("big2.bin", "r"); 69 | decomp = DeflateDecompressorStream(f); 70 | d2=deserialize(decomp); 71 | close(f); 72 | -------------------------------------------------------------------------------- /Chapter06/07. 変更可能型と変更不能型の性能差を確認する/walk.jl: -------------------------------------------------------------------------------- 1 | abstract type AbstractPoint end 2 | 3 | struct PointI <: AbstractPoint 4 | x::Int 5 | y::Int 6 | end 7 | 8 | mutable struct PointM <: AbstractPoint 9 | x::Int 10 | y::Int 11 | end 12 | 13 | PointM(p::PointM) = PointM(p.x, p.y) 14 | 15 | d(p::AbstractPoint) = abs(p.x) + abs(p.y) 16 | move(p::PointI, d::PointI) = PointI(p.x+d.x, p.y+d.y) 17 | move(p::PointM, d::PointM) = (p.x += d.x; p.y += d.y; p) 18 | 19 | function simI() 20 | maxd = 0 21 | x = PointI(0, 0) 22 | @inbounds for i in 1:10^6 23 | x = move(x, PointI(2rand(Bool)-1, 2rand(Bool)-1)) 24 | curd = d(x) 25 | maxd = max(maxd, curd) 26 | end 27 | maxd 28 | end 29 | 30 | function simM() 31 | maxd = 0 32 | x = PointM(0, 0) 33 | m = PointM(0, 0) 34 | @inbounds for i in 1:10^6 35 | m.x, m.y = 2rand(Bool)-1, 2rand(Bool)-1 36 | move(x, m) 37 | curd = d(x) 38 | maxd = max(maxd, curd) 39 | end 40 | maxd 41 | end 42 | 43 | function simI2() 44 | path = PointI[] 45 | x = PointI(0, 0) 46 | @inbounds for i in 1:10^6 47 | push!(path, x) 48 | x = move(x, PointI(2rand(Bool)-1, 2rand(Bool)-1)) 49 | end 50 | path 51 | end 52 | 53 | function simM2() 54 | path = PointM[] 55 | x = PointM(0, 0) 56 | m = PointM(0, 0) 57 | @inbounds for i in 1:10^6 58 | push!(path, PointM(x)) 59 | m.x, m.y = 2rand(Bool)-1, 2rand(Bool)-1 60 | move(x, m) 61 | end 62 | path 63 | end 64 | -------------------------------------------------------------------------------- /Chapter01/04. JuliaをLinux でソースからビルドする/commands.txt: -------------------------------------------------------------------------------- 1 | $ sudo apt update 2 | $ sudo apt install --yes build-essential python-minimal gfortran m4 cmake pkg-config libssl-dev 3 | 4 | 5 | $ git clone git://github.com/JuliaLang/julia.git 6 | $ cd julia 7 | $ git checkout v1.2.0 8 | 9 | $ make -j $((`nproc`-1)) 1>build_log.txt 2>build_error.txt 10 | 11 | 12 | $ cd ~ 13 | # Get link from MKL website 14 | $ wget http://registrationcenter-download.intel.com/[go to Intel MKL web site to get link]/l_mkl_2019.0.117.tgz 15 | $ tar zxvf l_mkl_2019.0.117.tgz 16 | $ cd l_mkl_2019.0.117 17 | $ sudo bash install.sh 18 | 19 | cd ~/julia 20 | echo "USEICC = 0" >> Make.user 21 | echo "USEIFC = 0" >> Make.user 22 | echo "USE_INTEL_MKL = 1" >> Make.user 23 | echo "USE_INTEL_LIBM = 0" >> Make.user 24 | source /opt/intel/bin/compilervars.sh intel64 25 | make -j $((`nproc`-1)) 1>build_log.txt 2>build_error.txt 26 | 27 | 28 | $ cd ~ 29 | # Get the link from Intel C++ compilers website 30 | $ wget http://[go to Intel to get link]/parallel_studio_xe_2018_update3_professional_edition.tgz 31 | $ tar zxvf parallel_studio_xe_2018_update3_professional_edition.tgz 32 | $ cd parallel_studio_xe_2018_update3_professional_edition 33 | $ sudo bash install.sh 34 | 35 | cd ~/julia 36 | echo "USEICC = 0" >> Make.user 37 | echo "USEIFC = 0" >> Make.user 38 | echo "USE_INTEL_MKL = 1" >> Make.user 39 | echo "USE_INTEL_LIBM = 1" >> Make.user 40 | source /opt/intel/bin/compilervars.sh intel64 41 | make -j $((`nproc`-1)) 1>build_log.txt 2>build_error.txt 42 | 43 | 44 | $ sudo ln -s /home/ubuntu/julia/usr/bin/julia /usr/local/bin/julia 45 | -------------------------------------------------------------------------------- /Chapter02/04. 正規表現を使ってGitログを解析する/parselog.jl: -------------------------------------------------------------------------------- 1 | using DataFrames, DataFramesMeta 2 | 3 | function parselog(lines) 4 | author = r"^Author: ([^<]*) <" 5 | insc = r"^.+changed, ([0-9]+) insertion" 6 | delc = r"^.+changed.*, ([0-9]+) deletion" 7 | authordata = DataFrame(author=String[], action=String[], count=Int[]) 8 | curauthor = "" 9 | for line in lines 10 | m = match(author, line) 11 | m === nothing || (curauthor = m[1]) 12 | m = match(insc, line) 13 | m === nothing || push!(authordata, 14 | (curauthor, "insertion", parse(Int, m[1]))) 15 | m = match(delc, line) 16 | m === nothing || push!(authordata, 17 | (curauthor, "deletion", parse(Int, m[1]))) 18 | end 19 | authorstats = @by(authordata, [:author, :action], 20 | count=sum(:count)) 21 | unstack(authorstats, :action, :count) 22 | end 23 | 24 | function gitstats(dir) 25 | if isdir(dir) 26 | println("\nAnalyzing") 27 | cd(dir) do 28 | try 29 | res = read(`git log --stat`, String) 30 | lines = split(res, ['\r', '\n'], keepempty=false) 31 | df = parselog(lines) 32 | df.all = coalesce.(df.deletion, 0) .+ 33 | coalesce.(df.insertion, 0) 34 | display(sort!(df, :all, rev=true)) 35 | catch 36 | error("Running git log failed") 37 | end 38 | end 39 | else 40 | error("$dir is not a directory") 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /Chapter09/02. JuMPを使って最適化問題を解く/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.2) pkg> add JuMP 3 | (v1.2) pkg> add Clp 4 | (v1.2) pkg> add Cbc 5 | 6 | (v1.2) pkg> add Gurobi 7 | 8 | using JuMP 9 | using Clp 10 | 11 | 12 | m = Model(solver = ClpSolver()); 13 | 14 | julia> @variable(m, x₁ >= 0) 15 | julia> @variable(m, x₂ >= 0) 16 | 17 | julia> @objective(m, Min, 50x₁ + 70x₂) 18 | 19 | julia> @constraint(m, 200x₁ + 2000x₂ >= 9000); 20 | julia> @constraint(m, 100x₁ + 30x₂ >= 300); 21 | julia> @constraint(m, 9x₁ + 11x₂ >= 60); 22 | 23 | julia> println(m) 24 | 25 | julia> status = solve(m) 26 | 27 | julia> println("Cost: $(getobjectivevalue(m))\nx₁=$(getvalue(x₁))\nx₂=$(getvalue(x₂))") 28 | 29 | 30 | using JuMP 31 | using Cbc 32 | m = Model(solver = CbcSolver()); 33 | @variable(m, x₁ >= 0) 34 | @variable(m, x₂ >= 0, Int) 35 | @objective(m, Min, 50x₁ + 70x₂) 36 | @constraint(m, 200x₁ + 2000x₂ >= 9000) 37 | @constraint(m, 100x₁ + 30x₂ >= 300) 38 | @constraint(m, 9x₁ + 11x₂ >= 60) 39 | 40 | julia> status = solve(m) 41 | julia> println("Cost: $(getobjectivevalue(m))\nx₁=$(getvalue(x₁))\nx₂=$(getvalue(x₂))") 42 | 43 | 44 | julia> status = solve(m) 45 | 46 | $ grbgetkey xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 47 | 48 | using JuMP 49 | using Gurobi 50 | m = Model(solver = GurobiSolver()); 51 | @variable(m, x₁ >= 0) 52 | @variable(m, x₂ >= 0, Int) 53 | @objective(m, Min, 50x₁ + 70x₂) 54 | @constraint(m, 200x₁ + 2000x₂ >= 9000) 55 | @constraint(m, 100x₁ + 30x₂ >= 300) 56 | @constraint(m, 9x₁ + 11x₂ >= 60) 57 | 58 | julia> status = solve(m) 59 | 60 | julia> println("Cost: $(getobjectivevalue(m))\nx₁=$(getvalue(x₁))\nx₂=$(getvalue(x₂))") 61 | -------------------------------------------------------------------------------- /Chapter02/02. 行列乗算を高速に行う/fastmatmul.jl: -------------------------------------------------------------------------------- 1 | # take a sequence of matrices and multiply them in an optimal order 2 | function fastmatmul(args::AbstractMatrix...) 3 | length(args) ≤ 1 && return *(args...) 4 | sizes = size.(args) 5 | if !all(sizes[i][2] == sizes[i+1][1] for i in 1:length(sizes)-1) 6 | throw(ArgumentError("matrix dimensions mismatch")) 7 | end 8 | partcost = Dict{Tuple{Int,Int}, Tuple{Int, Int}}() 9 | from, to = 1, length(sizes) 10 | solvemul(sizes, partcost, from, to) 11 | domul(args, partcost, from, to) 12 | end 13 | 14 | # find the optimal sequence of multiplications 15 | function solvemul(sizes, partcost, from, to) 16 | if from == to 17 | partcost[(from, to)] = (0, from) 18 | return 19 | end 20 | mincost = typemax(Int) 21 | minj = -1 22 | for j in from:to-1 23 | haskey(partcost, (from, j)) || solvemul(sizes, partcost, from, j) 24 | haskey(partcost, (j+1, to)) || solvemul(sizes, partcost, j+1, to) 25 | curcost = sizes[from][1]*sizes[j][2]*sizes[to][2] + 26 | partcost[(from, j)][1] + partcost[(j+1, to)][1] 27 | if curcost < mincost 28 | minj = j 29 | mincost = curcost 30 | end 31 | end 32 | partcost[(from, to)] = (mincost, minj) 33 | end 34 | 35 | # perform the multiplication given precomputed information 36 | # about the optimal multiplication order 37 | function domul(args, partcost, from, to) 38 | from == to && return args[from] 39 | from+1 == to && return args[from]*args[to] 40 | j = partcost[(from, to)][2] 41 | domul(args, partcost, from, j) * domul(args, partcost, j+1, to) 42 | end 43 | -------------------------------------------------------------------------------- /Chapter10/01. マルチプロセスで計算する/commands.txt: -------------------------------------------------------------------------------- 1 | $ julia 2 | 3 | julia> using Distributed 4 | julia> addprocs(1) 5 | 6 | julia> Distributed.myid() 7 | julia> workers() 8 | 9 | julia> res = @spawnat 2 myid() 10 | julia> fetch(res) 11 | 12 | remote_f = function(s::Int=3) 13 | println("Worker $(myid()) will sleep for $s seconds") 14 | sleep(s) 15 | val=rand(1:1000) 16 | println("Completed worker $(myid()) - return $val") 17 | return val 18 | end 19 | 20 | julia> @fetchfrom 2 remote_f(4) 21 | 22 | function run_timeout(timeout::Int, f::Function, params...) 23 | wid = addprocs(1)[1] 24 | result = RemoteChannel(()->Channel{Tuple}(1)); 25 | @spawnat wid put!(result, (f(params...), myid())) 26 | res = nothing 27 | time_elapsed = 0.0 28 | while time_elapsed < timeout && !isready(result) 29 | sleep(0.25) 30 | time_elapsed += 0.25 31 | end 32 | if !isready(result) 33 | println("Not completed! Computation at $wid will be 34 | terminated!") 35 | else 36 | res = take!(result) 37 | end 38 | rmprocs(wid); 39 | return res 40 | end 41 | 42 | julia> run_timeout(3, remote_f, 2) 43 | 44 | julia> run_timeout(3, remote_f, 10) 45 | 46 | julia> workers() 47 | 48 | 49 | $ julia -p 2 50 | 51 | julia> using Distributed 52 | julia> @everywhere function myF2(); println("myF2 ", myid()); end; 53 | julia> @spawnat workers()[end] myF2(); 54 | 55 | 56 | julia> hello() = println("hello"); 57 | julia> @fetchfrom 2 hello() 58 | julia> f_lambda = () -> hello(); 59 | julia> f_lambda() 60 | julia> @fetchfrom 2 f_lambda() 61 | 62 | julia> @everywhere hello() = println("hello") 63 | julia> @fetchfrom 2 f_lambda() 64 | -------------------------------------------------------------------------------- /Chapter03/03. インターネットからデータを取得する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.0) pkg> add HTTP 3 | (v1.0) pkg> add Gumbo 4 | (v1.0) pkg> add Cascadia 5 | 6 | 7 | using HTTP, Gumbo, Cascadia 8 | 9 | r=HTTP.get("https://github.com/JuliaWeb"); 10 | 11 | page_body = String(r.body); 12 | 13 | h = Gumbo.parsehtml(page_body); 14 | 15 | qs = HTMLElement[] 16 | Cascadia.matchAllInto(sel"h3 .d-inline-block", h.root, qs); 17 | names_links = Tuple{String, String}[] 18 | for q in qs 19 | name = strip(nodeText(q)) 20 | link = q.attributes["href"] 21 | push!(names_links, (name, link)) 22 | end 23 | 24 | julia> names_links 25 | 26 | julia> stats = Dict{String,String}() 27 | julia> @sync for (name, link) in names_links 28 | @async begin 29 | r2 = HTTP.get("https://github.com"*link); 30 | h2 = parsehtml(String(r2.body)); 31 | qs2 = HTMLElement[] 32 | Cascadia.matchAllInto(sel".social-count.js-social-count", h2.root, qs2); 33 | stats[name] = strip(nodeText(qs2[1])) 34 | end 35 | end 36 | 37 | 38 | julia> stats 39 | 40 | julia> stats2 = Dict(key => parse(Int64, stats[key]) for key in keys(stats)) 41 | 42 | julia> m = maximum(values(stats2)) 43 | 44 | 45 | julia> ] 46 | (v1.2) pkg> add PyCall 47 | (v1.2) pkg> add Conda 48 | 49 | using PyCall 50 | using Conda 51 | Conda.add("scrapy") 52 | 53 | ssel = pyimport("scrapy.selector") 54 | 55 | julia> s = ssel.Selector(text=page_body) 56 | 57 | julia> elems = s.xpath("//a[@itemprop='name codeRepository']") 58 | 59 | julia> strip(elems[1].xpath("text()")[1].extract()) 60 | 61 | julia> strip(elems[2].xpath("text()")[1].extract()) 62 | 63 | julia> a = elems[1].xpath("@href")[1].extract() 64 | 65 | julia> a = elems[2].xpath("@href")[1].extract() 66 | -------------------------------------------------------------------------------- /Chapter09/04. Plots.jlを使って複雑なプロットを描く/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.2) pkg> add DataFrames 3 | (v1.2) pkg> add Plots 4 | (v1.2) pkg> add Distributions 5 | (v1.2) pkg> add StatsPlots 6 | (v1.2) pkg> add CSV 7 | 8 | 9 | using Random 10 | using DataFrames 11 | using Plots 12 | using Distributions 13 | using StatPlots 14 | 15 | Random.seed!(0); 16 | df = DataFrame(x1=randn(1000), x2=randn(1000)); 17 | ρ = 0.8 18 | df.x3 = ρ*df.x1 + √(1-ρ*ρ)*df.x2; 19 | 20 | julia> gr() 21 | 22 | p = histogram(df.x1, nbins=25, labels="x1"); 23 | histogram!(p, df.x3, fillalpha=0.5, bar_width=0.3, labels="x3") 24 | 25 | p = histogram(df.x3, normed=true) 26 | plot!(p, Normal(0,1), width=4) 27 | 28 | plot(scatter(df.x1, df.x3, legend=false), histogram2d(df.x1, df.x3), layout=Plots.GridLayout(1, 2), xlabel="x1", ylabel="x3" ) 29 | 30 | corrplot(convert(Array, df), bins=25, labels=["x1", "x2", "x3"]) 31 | 32 | 33 | using CSV 34 | iris = CSV.read(joinpath(dirname(pathof(DataFrames)), "..", "test/data/iris.csv")); 35 | 36 | font_h6 = Plots.font("Helvetica", 6) 37 | plts = Plots.Plot[] 38 | for i in 1:4, j in 1:4 39 | if i == j 40 | push!(plts, histogram(iris[!, i], group=iris[!, :Species], 41 | xlabel=names(iris)[j], ylabel="count", 42 | legend=false, fillalpha=0.5, 43 | guidefont=font_h6, tickfont=font_h6)) 44 | else 45 | push!(plts, scatter(iris[!, j], iris[!, i], 46 | xlabel=names(iris)[j], ylabel=names(iris)[i], 47 | group=iris[!, :Species], legend=(i==4&&j==1), 48 | guidefont=font_h6, tickfont=font_h6, legendfont=font_h6, 49 | background_color_legend=RGBA(255, 255, 255, 0.8), 50 | foreground_color_legend=nothing)) 51 | end 52 | end 53 | p = plot(plts..., layout=Plots.GridLayout(4, 4)) 54 | 55 | -------------------------------------------------------------------------------- /Chapter10/03. マルチスレッドで計算する/commands.txt: -------------------------------------------------------------------------------- 1 | # on Windows 2 | C:\ set JULIA_NUM_THREADS=4 3 | 4 | # on Linux 5 | $ export JULIA_NUM_THREADS=4 6 | 7 | 8 | $ julia 9 | 10 | julia> Threads.nthreads() 11 | 12 | 13 | using DataFrames, BenchmarkTools, Random, Statistics 14 | 15 | Random.seed!(0); 16 | N = 100_000; 17 | const data = DataFrame(rowtype = rand(1:12, N)); 18 | data.x1 = data.rowtype .* randn(N); 19 | 20 | julia> first(data, 5) 21 | 22 | function stats(df) 23 | m = MersenneTwister() 24 | median_val = 0 25 | for i in 1:100 26 | median_val += median(rand(m, df.x1, nrow(df))) 27 | end 28 | return (rowtype=df.rowtype[1], n=nrow(df), 29 | tid=Threads.threadid(), median=median_val / 100) 30 | end 31 | 32 | julia> @time by(data, :rowtype, stats) 33 | 34 | julia> @time by(data, :rowtype, stats); 35 | 36 | function threaded_by(df::DataFrame, groupcol::Symbol, f::Function) 37 | groups = groupby(df, groupcol) 38 | f(view(groups[1], 1:2, :)); # 事前にコンパイルするために必要! 39 | res = Vector{NamedTuple}(undef, length(groups)) 40 | Threads.@threads for g in 1:length(groups) 41 | rv = f(groups[g]) 42 | res[g] = rv 43 | end 44 | DataFrame(rowtype=getfield.(res, groupcol), x1=res) 45 | end 46 | 47 | julia> @time threaded_by(data, :rowtype, stats) 48 | 49 | julia> @time threaded_by(data, :rowtype, stats); 50 | 51 | 52 | julia> Threads.nthreads() 53 | julia> total = 0; 54 | julia> Threads.@threads for i in 1:1_000_000 55 | global total = total + 1 56 | end 57 | julia> total 58 | 59 | julia> total2 = 0; 60 | julia> s = Threads.SpinLock() 61 | julia> Threads.@threads for i in 1:1_000_000 62 | Threads.lock(s) 63 | global total2 = total2 + 1 64 | Threads.unlock(s) 65 | end 66 | julia> total2 67 | -------------------------------------------------------------------------------- /Chapter10/02. リモートのJuliaプロセスと通信する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.2) pkg> add ParallelDataTransfer 3 | 4 | $ julia -p 4 5 | 6 | julia> using Distributed 7 | julia> nworkers() 8 | 9 | 10 | using Distributed 11 | @everywhere using ParallelDataTransfer 12 | 13 | @everywhere function rule30(ca::Array{Bool}) 14 | lastv = ca[1] 15 | for i in 2:(length(ca)-1) 16 | current = ca[i] 17 | ca[i] = xor(lastv, ca[i] || ca[i+1]) 18 | lastv = current 19 | end 20 | end 21 | 22 | @everywhere function getsetborder(ca::Array{Bool}, 23 | neighbours::Tuple{Int64,Int64}) 24 | ca[1] = (@fetchfrom neighbours[1] caa[end-1]) 25 | ca[end] = (@fetchfrom neighbours[2] caa[2]) 26 | end 27 | 28 | function printsimdist(workers::Array{Int}) 29 | for w in workers 30 | dat = @fetchfrom w caa 31 | for b in dat[2:end-1] 32 | print(b ? "#" : " ") 33 | end 34 | end 35 | println() 36 | end 37 | 38 | function runca(steps::Int, visualize::Bool) 39 | @sync for w in workers() 40 | @async @fetchfrom w fill!(caa, false) 41 | end 42 | @fetchfrom wks[Int(nwks/2)+1] caa[2]=true 43 | visualize && printsimdist(workers()) 44 | for i in 1:steps 45 | @sync for w in workers() 46 | @async @fetchfrom w getsetborder(caa, neighbours) 47 | end 48 | @sync for w in workers() 49 | @async @fetchfrom w rule30(caa) 50 | end 51 | visualize && printsimdist(workers()) 52 | end 53 | end 54 | 55 | wks = workers() 56 | nwks = length(wks) 57 | for i in 1:nwks 58 | sendto(wks[i],neighbours = (i==1 ? wks[nwks] : wks[i-1], 59 | i==nwks ? wks[1] : wks[i+1])) 60 | fetch(@defineat wks[i] const caa = zeros(Bool, 15+2)); 61 | end 62 | 63 | julia> runca(20,true) 64 | -------------------------------------------------------------------------------- /Chapter03/04. 簡単なRESTfulサービスを作ってみる/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.2) pkg> add JuliaWebAPI 3 | (v1.2) pkg> add ZMQ 4 | (v1.2) pkg> add HTTP 5 | 6 | 7 | julia> using Sockets 8 | 9 | julia> server = Sockets.listen(8080) 10 | 11 | julia> while true 12 | sock = Sockets.accept(server) 13 | data = readline(sock) 14 | print("Got request:\n", data, "\n") 15 | cmd = split(data, " ")[2][2:end] 16 | println(sock, "\nHTTP/1.1 200 OK\nContent-Type: text/html\n") 17 | println(sock, string("", cmd, "=", eval(Meta.parse(cmd)), "")) 18 | close(sock) 19 | end # これを実行するとJuliaのコンソールがブロックする 20 | 21 | $ curl http://127.0.0.1:8080/5+9 22 | 23 | 24 | julia> using Sockets 25 | julia> client = Sockets.connect("127.0.0.1", 8080) 26 | 27 | julia> write(client, "GET /3*8\n") 28 | 29 | julia> readlines(client) 30 | 31 | using JuliaWebAPI 32 | using ZMQ 33 | 34 | function testfn(arg1, arg2; optarg1="10", optarg2="20") 35 | println("T: ", arg1, " ", arg2, " ", optarg1, " ", optarg2) 36 | return parse(Int, arg1)+parse(Int, arg2)+parse(Int, optarg1)+parse(Int, optarg2) 37 | end 38 | 39 | 40 | tr = JuliaWebAPI.ZMQTransport("tcp://127.0.0.1:9999", ZMQ.REP, true) 41 | apir = JuliaWebAPI.APIResponder(tr, JuliaWebAPI.JSONMsgFormat()) 42 | 43 | 44 | julia> register(apir, testfn; resp_json=true, 45 | resp_headers=Dict("Content-Type" => "application/json; charset=utf-8")) 46 | 47 | 48 | julia> process(apir) # これを実行するとJuliaのコンソールがブロックする 49 | 50 | 51 | using JuliaWebAPI 52 | const apiclnt = JuliaWebAPI.APIInvoker("tcp://127.0.0.1:9999"); 53 | 54 | julia> JuliaWebAPI.run_http(apiclnt, 8888) # これを実行するとJuliaのコンソールがブロックする 55 | 56 | 57 | 58 | $ curl "http://127.0.0.1:8888/testfn/5/9?optarg1=100&optarg2=1000" 59 | 60 | 61 | julia> process(apir) 62 | 63 | 64 | julia> using HTTP 65 | julia> res = HTTP.get("http://127.0.0.1:8888/testfn/5/9?optarg1=100&optarg2=1000") 66 | -------------------------------------------------------------------------------- /Chapter03/06. 日付と時刻を扱う/commands.txt: -------------------------------------------------------------------------------- 1 | julia> using Dates 2 | 3 | julia> ts = Dates.now() 4 | 5 | julia> typeof(ts) 6 | 7 | julia> Date(2018, 08, 15) 8 | 9 | julia> Date(2018, 8) 10 | 11 | julia> Date(2018) 12 | 13 | julia> DateTime(2018, 8, 15, 18, 22, 55, 123) 14 | 15 | julia> dt1 = Date("2018-08-15T18:22:55.123", DateFormat("y-m-dTH:M:S.s")) 16 | 17 | julia> dtm1 = DateTime("2018-08-15T18:22:55.123", DateFormat("y-m-dTH:M:S.s")) 18 | 19 | julia> dtm2 = DateTime("2018-08-16T19:32:55.223", dateformat"y-m-dTH:M:S.s") 20 | 21 | 22 | julia> delta = dtm2 - dtm1 23 | julia> typeof(delta) 24 | 25 | 26 | julia> Date("2018-03-01") - Date("2018-02-01") 27 | julia> dtm1 + Year(1) + Month(4) + Day(4) + Hour(3) 28 | julia> dtm1 + Millisecond(24*3600*1000) 29 | 30 | 31 | julia> Year(1)*3 + Month(4)/2 + Day(4)*3 + Hour(3) 32 | 33 | julia> Day(4)/2 34 | 35 | julia> Day(4)/8 36 | 37 | julia> Day(4)*0.5 38 | 39 | julia> d1 = Date("2018-08-15") 40 | julia> dump(d1) 41 | 42 | 43 | julia> d1 = Date("2018-08-15") 44 | julia> d1 - Day(d1.instant.periods.value) + Day(1) 45 | 46 | 47 | julia> dt = DateTime("2018-08-15T18:22:55.123") 48 | julia> dump(dt) 49 | 50 | 51 | julia> dt = DateTime("2018-08-15T18:22:55.123"); 52 | julia> dt - Millisecond(dt.instant.periods.value) + Day(1) 53 | 54 | 55 | julia> Date(2008, 1, 31) + Month(1) 56 | julia> Date(2008, 1, 31) + Month(1) - Month(1) 57 | julia> Date(2008, 1, 31) + (Month(1) - Month(1)) 58 | 59 | 60 | julia> ] 61 | (v1.2) pkg> add TimeZones 62 | 63 | using TimeZones 64 | 65 | julia> dtz = parse(ZonedDateTime, "2017-11-14 11:03:53 +0100", 66 | dateformat"yyyy-mm-dd HH:MM:SS zzzzz") 67 | 68 | 69 | julia> dtz = parse(ZonedDateTime, "22/Aug/2018:09:22:07 -0100", 70 | DateFormat("dd/uuu/yyyy:H:M:S zzzzz")) 71 | 72 | julia> dump(dtz) 73 | 74 | 75 | julia> using Dates 76 | julia> dtz2 = dtz + Dates.Year(2) - Dates.Month(1) + Dates.Day(20) 77 | -------------------------------------------------------------------------------- /Chapter10/04. 分散環境で計算する/commands.txt: -------------------------------------------------------------------------------- 1 | $ ssh-keygen -P "" -t rsa -f ~/.ssh/cluster 2 | 3 | $ cat ~/.ssh/cluster.pub >> ~/.ssh/authorized_keys 4 | 5 | $ ssh ubuntu@localhost 6 | 7 | $ julia --machine-file machinefile.txt 8 | 9 | julia> using Distributed 10 | 11 | julia> nworkers() 12 | 13 | julia> ] 14 | (v1.2) pkg> add Distributions 15 | (v1.2) pkg> add DataFrames 16 | 17 | using Distributed 18 | 19 | @everywhere using Distributed, Distributions, DataFrames, Random 20 | 21 | @everywhere function sim_inventory(reorder_q::Int64, 22 | reorder_point::Int64; 23 | days = 100, 24 | sd = Normal(20,20^0.5), # 日毎の売上の確率分布 25 | wh = 0.1, # 在庫コスト 26 | p = 4.0, # ユニットあたりの売上利益 27 | d_prob = 0.50, # 注文する確率 28 | k = 60.0, # 固定配送コスト 29 | rng = MersenneTwister(0)) 30 | profit = 0.0 # 利益の総計 31 | stock = reorder_q 32 | for day in 1:days 33 | if stock < reorder_point && rand(rng)< d_prob # 注文到着 34 | profit -= k # 配送料支払い 35 | stock += reorder_q 36 | end 37 | sale = max(0, min(Int(round(rand(rng, sd))), stock)) 38 | stock -= sale # 在庫を減らす 39 | profit += p*sale - wh*stock # 利益を増やす 40 | end 41 | return profit / days 42 | end 43 | 44 | julia> sweep = vec(collect(Base.product(10:10:250,10:10:250,20:5:60))) 45 | julia> Random.seed!(0); 46 | julia> Random.shuffle!(sweep); 47 | 48 | const rngs = Dict(i=>MersenneTwister(i) for i in workers()); 49 | 50 | res = @distributed (append!) for s in sweep 51 | rng = deepcopy(rngs[myid()]) 52 | profit = 0.0 53 | for sim in 1:10000 54 | profit += sim_inventory(s[1], s[2], days=s[3], rng=rng) 55 | end 56 | DataFrame(worker=myid(), reorder_q=s[1], reorder_point=s[2], 57 | days=s[3], profit=profit/10000) 58 | end 59 | 60 | julia> res 61 | 62 | julia> DataFrame([sdf[argmax(sdf.profit), :] for sdf in groupby(res, :days, sort=true)]) 63 | 64 | -------------------------------------------------------------------------------- /Chapter05/01. Juliaのサブタイプを理解する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> struct Point{T<:Integer, S<:AbstractString} 2 | pos::Complex{T} 3 | label::S 4 | end 5 | julia> Point(x::T, y::T, label::S) where {T<:Integer, 6 | S<:AbstractString} = 7 | Point{T,S}(Complex(x,y), label) 8 | julia> Point(x, y, label) = Point(promote(Integer.((x,y))...)..., 9 | label) 10 | julia> p1 = Point(1, 0, "1") 11 | julia> p2 = Point(1, 0, SubString("1", 1)) 12 | julia> p3 = Point(true, false, "1") 13 | julia> p4 = Point(2, 0, "2") 14 | julia> [p1, p2, p3, p4] 15 | julia> [p1, p2] 16 | julia> [p1, p3] 17 | julia> [p1, p4] 18 | julia> sumpoint1(v::AbstractVector{Point}) = 19 | Point(sum(p.pos for p in v), "") 20 | julia> sumpoint1([p1, p2]) 21 | julia> sumpoint2(v::AbstractVector{<:Point}) = 22 | Point(sum(p.pos for p in v), "") 23 | julia> sumpoint2([p1, p2]) 24 | julia> foo(p::Point) = "generic definition" 25 | julia> foo(p::Point{Int, <:AbstractString}) = "Intが渡された際のデフォルト" 26 | julia> foo(p::Point{<:Integer, String}) = "Stringが渡された際のデフォルト" 27 | julia> foo(Point(true, true, s"12")) 28 | julia> foo(Point(1, 1, s"12")) 29 | julia> foo(Point(true, true, "12")) 30 | julia> foo(Point(1, 1, "12")) 31 | julia> foo(p::Point{Int, String}) = "厳密に型を指定したメソッド" 32 | julia> foo(Point(1, 1, "12")) 33 | 34 | 35 | julia> push!([p1], p2) 36 | julia> push!(Point[p1], p2) 37 | julia> Int <: Integer 38 | julia> Point{Int, String} <: Point{Integer, String} 39 | julia> Point{Int, String} <: Point{<:Integer, String} 40 | julia> Point{Int, String} <: Point{T, String} where T<:Integer 41 | julia> Point{Int} 42 | julia> Point{<:Signed, String} 43 | julia> Point{Int}{String} 44 | julia> Point{Int, String} 45 | julia> sumpoint1(Point[p1, p2]) 46 | 47 | 48 | julia> methods(foo) 49 | julia> Tuple{Point{Int, String}, Point{Bool, SubString{String}}} <: Tuple{Point{Int}, Point} 50 | julia> sumpoint_tuple(v::Tuple{Vararg{Point}}) = 51 | Point(sum(p.pos for p in v), "") 52 | julia> sumpoint_tuple((p1, p2, p3)) 53 | -------------------------------------------------------------------------------- /Chapter03/04. 簡単なRESTfulサービスを作ってみる/3.04 簡単なRESTfulサービスを作ってみる - サーバ.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "using Sockets" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "Sockets.TCPServer(RawFD(0x00000032) active)" 21 | ] 22 | }, 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "server = Sockets.listen(8080)" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": null, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "Got request:\n", 42 | "GET /3*7\n" 43 | ] 44 | } 45 | ], 46 | "source": [ 47 | "while true\n", 48 | " sock = Sockets.accept(server)\n", 49 | " data = readline(sock)\n", 50 | " print(\"Got request:\\n\", data, \"\\n\")\n", 51 | " cmd = split(data, \" \")[2][2:end]\n", 52 | " println(sock, \"\\nHTTP/1.1 200 OK\\nContent-Type: text/html\\n\")\n", 53 | " println(sock, string(\"\", cmd, \"=\",\n", 54 | " eval(Meta.parse(cmd)), \"\"))\n", 55 | " close(sock)\n", 56 | "end" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "-----" 64 | ] 65 | } 66 | ], 67 | "metadata": { 68 | "@webio": { 69 | "lastCommId": null, 70 | "lastKernelId": null 71 | }, 72 | "kernelspec": { 73 | "display_name": "Julia 1.2.0", 74 | "language": "julia", 75 | "name": "julia-1.2" 76 | }, 77 | "language_info": { 78 | "file_extension": ".jl", 79 | "mimetype": "application/julia", 80 | "name": "julia", 81 | "version": "1.2.0" 82 | } 83 | }, 84 | "nbformat": 4, 85 | "nbformat_minor": 2 86 | } 87 | -------------------------------------------------------------------------------- /Chapter03/09. Microsoft Excelファイルを読み書きする/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.2) pkg> add PyCall 3 | (v1.2) pkg> add Conda 4 | (v1.2) pkg> add DataFrames 5 | (v1.2) pkg> add XLSX 6 | 7 | julia> using Conda 8 | julia> Conda.add("openpyxl") 9 | 10 | 11 | using PyCall 12 | using Dates 13 | using Random 14 | xl = pyimport("openpyxl") 15 | 16 | julia> wb = xl.Workbook(); 17 | julia> ws = wb.active 18 | 19 | julia> ws.cell(1, 1, "Data generated on:") 20 | julia> ws.cell(2, 1, Dates.now()) 21 | 22 | Random.seed!(0) 23 | dat = rand(3, 5) 24 | for i in 1:size(dat)[1] 25 | ws.append((dat[i, :]..., )) 26 | end 27 | 28 | wb.save("sample1.xlsx") 29 | 30 | wb = xl.load_workbook(filename = "sample1.xlsx") 31 | ws = wb.active 32 | 33 | println(ws.cell(1, 1).value, "\n", ws.cell(2, 1).value) 34 | 35 | julia> typeof(ws.cell(2, 1).value) 36 | 37 | julia> ws.cell(20, 221).value == nothing 38 | 39 | julia> using Printf 40 | julia> for row in ws.rows 41 | for cell in row 42 | print("|") 43 | if typeof(cell.value) <: Number 44 | @printf("%.3f", cell.value) 45 | else 46 | show(cell.value) 47 | end 48 | end 49 | println("|") 50 | end 51 | 52 | 53 | wb[:close]() 54 | 55 | 56 | using XLSX 57 | 58 | XLSX.openxlsx("sample2.xlsx", mode="w") do xf 59 | sheet = xf[1] 60 | XLSX.rename!(sheet, "SheetName") 61 | sheet["A1"] = "Data generated on:" 62 | sheet["A2"] = Dates.now() 63 | dat = rand(3, 5) 64 | for row in 1:3 65 | for col in 1:5 66 | XLSX.setdata!(sheet, XLSX.CellRef(2+row, col), rand()) 67 | end 68 | end 69 | end 70 | 71 | julia> wb = XLSX.readxlsx("sample2.xlsx") 72 | 73 | julia> sheetname = XLSX.sheetnames(wb)[1] 74 | julia> ws = wb[sheetname] 75 | 76 | julia> dim = ws.dimension 77 | 78 | XLSX.close(wb) 79 | 80 | using DataFrames 81 | df1 = DataFrame(a=[1, 2, 3], b=[4, 5, 6]); 82 | df2 = DataFrame(x1=[1, 2, 3], x2=["A", "B", "C"]); 83 | 84 | XLSX.writetable("sample3.xlsx", 85 | SheetName1=( DataFrames.columns(df1), DataFrames.names(df1) ), 86 | SheetName2=( DataFrames.columns(df2), DataFrames.names(df2) )); 87 | -------------------------------------------------------------------------------- /Chapter05/06. 例外処理/commands.txt: -------------------------------------------------------------------------------- 1 | function loglines(filename, lines...) 2 | f = open(filename, "a") 3 | foreach(line -> (println(f, line)),lines) 4 | sqrt(-2) 5 | close(f) 6 | end 7 | 8 | julia> loglines("mylog.txt", "Test log:") 9 | 10 | julia> try 11 | loglines("mylog.txt", "Test log:") 12 | catch e 13 | dump(e) 14 | end 15 | 16 | for i in 1:100_000 17 | try 18 | loglines("mylog2.txt", string(i)) 19 | catch 20 | end 21 | end 22 | 23 | julia> f = open("mylog2.txt") 24 | 25 | julia> close() 26 | 27 | function loglines2(filename, lines...) 28 | f = open(filename, "a") 29 | try 30 | foreach(line -> println(f, line),lines) 31 | sqrt(-2) 32 | finally 33 | close(f) 34 | end 35 | end 36 | 37 | julia> loglines2("mylog3.txt", "Test log:") 38 | 39 | open("mylog3.txt", "w") do f end #make sure the file is empty 40 | for i in 1:100_000 41 | try 42 | loglines2("mylog3.txt", string(i)) 43 | catch e 44 | if !(e isa DomainError) 45 | rethrow(e) 46 | end 47 | end 48 | end 49 | 50 | julia> lines = open("mylog3.txt") do f 51 | readlines(f) 52 | end; 53 | 54 | julia> all([lines[i]==string(i) for i in 1:100_000]) 55 | 56 | 57 | julia> function ff(a) 58 | res = missing 59 | try 60 | res = sqrt(a) 61 | finally 62 | return res 63 | end 64 | end; 65 | 66 | julia> ff(4) 67 | julia> ff(-2) 68 | 69 | function divide(a, b) 70 | b == 0 && @warn "Division by zero" 71 | a/b 72 | end 73 | 74 | julia> divide(3, 5) 75 | julia> divide(3, 0) 76 | 77 | function divide2(a, b) 78 | b == zero(typeof(b)) && @error "Division by zero" 79 | a/b 80 | end 81 | 82 | julia> divide2(3, 0) 83 | 84 | function divide3(a, b) 85 | if b == zero(typeof(b)) 86 | @error "Division by zero" 87 | throw(ErrorException("Division by zero")) 88 | end 89 | a/b 90 | end 91 | 92 | julia> divide3(3, 0) 93 | 94 | julia> subtypes(Exception) 95 | 96 | julia> struct MyException <: Exception 97 | msg::String 98 | end 99 | julia> throw(MyException("something went wrong")) 100 | 101 | 102 | -------------------------------------------------------------------------------- /Chapter06/02. マクロと関数生成を理解する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.2) pkg> add BenchmarkTools 3 | 4 | 5 | function fib(n) 6 | n <= 2 ? 1 : fib(n-1) + fib(n-2) 7 | end 8 | 9 | julia> fib(4) 10 | julia> @time fib(40) 11 | 12 | function memoit(f::Function, p) 13 | if !isdefined(Main, :memoit_cache) 14 | global memoit_cache = Dict{Function,Dict{Any,Any}}() 15 | end 16 | c = haskey(memoit_cache, f) ? memoit_cache[f] : memoit_cache[f]=Dict() 17 | haskey(c, p) ? c[p] : c[p] = f(p) 18 | end 19 | 20 | function fib2(n) 21 | n <= 2 ? 1 : memoit(fib2, n-1) + memoit(fib2, n-2) 22 | end 23 | 24 | julia> fib2(4) 25 | julia> @time fib2(40) 26 | 27 | macro memo(e) 28 | println("macro @memo is run: ", e, " ", e.args) 29 | (!(typeof(e) <: Expr) || !(e.head == :call)) && 30 | error("wrong @memo params") 31 | return quote # we handle only functions with a single parameter 32 | memoit($(esc(e.args[1])), $(esc(e.args[2]))) 33 | end 34 | end 35 | 36 | julia> function fib3(n) 37 | n <= 2 ? 1 : (@memo fib3(n-1)) + (@memo fib3(n-2)) 38 | end 39 | 40 | julia> fib3(4) 41 | julia> @time fib3(40) 42 | 43 | 44 | function sumx1(objs...) 45 | isempty(objs) && return 0 46 | total=objs[1].x 47 | for i in 2:length(objs) 48 | total += objs[i].x 49 | end 50 | total 51 | end 52 | 53 | struct A x::Int end 54 | struct B x::Float64 end 55 | 56 | julia> sumx1(A(5), B(7)) 57 | julia> sumx1(A(5), A(17)) 58 | 59 | @generated function sumx2(objs...) 60 | isempty(objs) && return 0 # default where no arguments were given 61 | total = :(objs[1].x) 62 | for i in 2:length(objs) 63 | total = :($total + objs[$i].x) 64 | end 65 | total 66 | end 67 | 68 | julia> sumx2(A(5), B(7)) == sumx1(A(5), B(7)) 69 | julia> sumx2(A(5), A(17)) == sumx1(A(5), A(17)) 70 | 71 | julia> using BenchmarkTools 72 | julia> const valsx = ([A(i) for i=1:10]..., [B(i) for i=1:10]...); 73 | julia> typeof(valsx) 74 | 75 | julia> @btime sumx1(valsx...) 76 | julia> @btime sumx2(valsx...) 77 | 78 | 79 | julia> macro example(v) 80 | :(($v, $(esc(v)))) 81 | end 82 | julia> function f() 83 | x = 1 84 | @example x 85 | end 86 | julia> x = 10 87 | julia> @macroexpand @example x 88 | 89 | 90 | julia> n=5 91 | julia> macroexpand(Main, :(@memo fib3(n-1))) 92 | 93 | julia> @code_lowered sumx2(A(1), B(2)) 94 | 95 | julia> @code_warntype sumx2(A(1), B(2)) 96 | 97 | julia> @code_warntype sumx1(A(1), B(2)) 98 | -------------------------------------------------------------------------------- /Chapter09/05. ScikitLearn.jlを使って機械学習モデルを作る/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.2) pkg> add Conda 3 | (v1.2) pkg> add CSV 4 | (v1.2) pkg> add HTTP 5 | (v1.2) pkg> add DataFrames 6 | (v1.2) pkg> add ScikitLearn 7 | (v1.2) pkg> add PyCall 8 | 9 | using Conda 10 | Conda.runconda(`install mlxtend -c conda-forge -y`) 11 | 12 | 13 | using CSV, HTTP, DataFrames, ScikitLearn, Random, PyCall, Statistics 14 | 15 | dat = HTTP.get("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data") 16 | buf = IOBuffer(dat.body[1:end-1]) # 最後の改行文字を切り落とす 17 | iris = CSV.read(buf; header=false) 18 | names!(iris, Symbol.(["SepalLength", "SepalWidth", "PetalLength", "PetalWidth", "Class"])) 19 | 20 | ua = unique(iris[!, :Class]) 21 | iris[!, :Class]=[findfirst(==(x), ua) - 1 for x in iris[!, :Class]] 22 | y = iris[!, :Class] 23 | X = Matrix(iris[!, 1:4]) 24 | 25 | using ScikitLearn.CrossValidation: train_test_split 26 | Random.seed!(0) 27 | X_train, X_test, y_train, y_test = 28 | train_test_split(X, y, test_size=0.3, random_state=0, stratify=y); 29 | 30 | @sk_import preprocessing : StandardScaler 31 | stdsc = StandardScaler(); 32 | X_train_std = stdsc.fit_transform(X_train) 33 | X_test_std = stdsc.transform(X_test) 34 | 35 | @sk_import linear_model: LogisticRegression 36 | logreg = LogisticRegression(fit_intercept=true) 37 | fit!(logreg, X_train_std, y_train) 38 | y_pred = predict(logreg, X_test_std) 39 | 40 | julia> @sk_import metrics : (accuracy_score,confusion_matrix); 41 | julia> accuracy_score(y_test,y_pred) 42 | julia> confusion_matrix(y_test,y_pred) 43 | 44 | @sk_import ensemble : RandomForestClassifier 45 | forest = RandomForestClassifier(n_estimators=100, random_state=0) 46 | fit!(forest, X_train, y_train) 47 | y_pred = predict(forest,X_test) 48 | importances = forest.feature_importances_ 49 | indices = sortperm(importances,rev=true) 50 | 51 | julia> DataFrame(Name=names(iris)[indices], Importance=importances[indices]) 52 | 53 | julia> confusion_matrix(y_test,y_pred) 54 | 55 | using PyPlot #重要! 56 | mlp = pyimport("mlxtend.plotting") 57 | fill_vals = Dict{Int,Float64}() 58 | fill_rngs = Dict{Int,Float64}() 59 | for ind in indices[3:end] 60 | fill_vals[ind-1] = mean(iris[!, names(iris)[ind]]) 61 | fill_rngs[ind-1] = std(iris[!, names(iris)[ind]]) 62 | end 63 | mlp.plot_decision_regions(X, y, forest, X_highlight=X_test, 64 | feature_index=(indices[1:2].-1), 65 | filler_feature_values=fill_vals, filler_feature_ranges=fill_rngs) 66 | xlabel(names(iris)[indices[1]]) 67 | ylabel(names(iris)[indices[2]]) 68 | -------------------------------------------------------------------------------- /Chapter06/01. メタプログラミングを理解する/commands.txt: -------------------------------------------------------------------------------- 1 | data=""" 2 | id,val,class 3 | 1,4,A 4 | 2,39,B 5 | 3,44,C 6 | """ 7 | 8 | function new_struct(fields::Vector{Tuple{String,DataType}}) 9 | name = "A" * string(hash(fields), base=16) 10 | code = "begin\nstruct $name\n" 11 | for field in fields 12 | code *= field[1]*"::"*string(field[2])*"\n" 13 | end 14 | eval(Meta.parse(code * "end\n$name\nend")) 15 | end 16 | 17 | julia> MyS = new_struct([("a", Int), ("b", String), ("c", Int)]); 18 | julia> dump(MyS) 19 | 20 | function parse_data(data::AbstractString) 21 | lines = filter(x->length(x)>0, strip.(split(data, ('\n', '\r')))) 22 | colnames = string.(split(lines[1], ',')) 23 | row1=split(lines[2], ',') 24 | coltypes = [occursin(r"^-?\d+$", val) ? Int64 : String for val in row1] 25 | (lines[2:end], new_struct(collect(zip(colnames, coltypes)))) 26 | end 27 | 28 | julia> dump(parse_data("col1,col2,col3\nabc,123,123.5")[2]) 29 | 30 | function parse_text(data::AbstractString) 31 | lines, MyStruct = parse_data(data) 32 | res = MyStruct[] 33 | for line in lines 34 | colvals = split(line, ',') 35 | f = (t, v)->t<:Int ? parse(Int, v) : string(v) 36 | vals = f.(MyStruct.types, colvals) 37 | push!(res, Base.invokelatest(MyStruct, vals...)) 38 | end 39 | return res 40 | end 41 | 42 | julia> parse_text(data) 43 | 44 | 45 | function new_struct2(fields::Vector{Tuple{String,DataType}}) 46 | name = "A" * string(hash(fields), base=16) 47 | c = Expr(:block, 48 | Expr(:struct,false,Symbol(name), 49 | Expr(:block, [Expr(:(::), Symbol(f[1]), 50 | f[2]) for f in fields]...)), 51 | Symbol(name)) 52 | eval(c) 53 | end 54 | 55 | function new_struct3(fields::Vector{Tuple{String,DataType}}) 56 | name = "A" * string(hash(fields), base=16) 57 | c = :(begin 58 | struct $(Symbol(name)) 59 | $([:($(Symbol(f[1]))::$(f[2])) for f in fields]...) 60 | end 61 | $(Symbol(name)) 62 | end) 63 | eval(c) 64 | end 65 | 66 | julia> MyS2 = new_struct2([("a", Int), ("b", String), ("c", Int)]); 67 | julia> dump(MyS2) 68 | 69 | julia> MyS == MyS2 70 | 71 | 72 | julia> dump(VERSION) 73 | 74 | julia> function f1() 75 | eval(:(g1() = 10)) 76 | g1() 77 | end 78 | julia> function f2() 79 | eval(:(g2() = 10)) 80 | Base.invokelatest(g2) 81 | end 82 | julia> f1() 83 | julia> f2() 84 | -------------------------------------------------------------------------------- /cookbookconf.jl: -------------------------------------------------------------------------------- 1 | using Pkg 2 | 3 | function addandpin(spec) 4 | Pkg.add(PackageSpec(; spec...)) 5 | Pkg.pin(spec.name) 6 | end 7 | 8 | # packages without external dependencies 9 | # that have to be manually instlled 10 | 11 | pkg1 = [(name="StatsBase", version="0.31.0"), 12 | (name="TimeZones", version="0.9.2"), 13 | (name="BSON", version="0.2.3"), 14 | (name="Revise", version="2.1.8"), 15 | (name="Distributions", version="0.21.0"), 16 | (name="Clp", version="0.6.2"), 17 | (name="HTTP", version="0.8.5"), 18 | (name="Gumbo", version="0.5.1"), 19 | (name="StringEncodings", version="0.3.1"), 20 | (name="ZMQ", version="1.0.0"), 21 | (name="CodecZlib", version="0.6.0"), 22 | (name="JSON", version="0.21.0"), 23 | (name="BenchmarkTools", version="0.4.2"), 24 | (name="JuliaWebAPI", version="0.6.0"), 25 | (name="FileIO", version="1.0.7"), 26 | (name="ProfileView", version="0.4.1"), 27 | (name="StaticArrays", version="0.11.0"), 28 | (name="ForwardDiff", version="0.10.3"), 29 | (name="Optim", version="0.19.1"), 30 | (name="JuMP", version="0.19.2"), 31 | (name="JLD2", version="0.1.2"), 32 | (name="XLSX", version="0.5.4"), 33 | (name="Cbc", version="0.6.2"), 34 | (name="DataFrames", version="0.19.2"), 35 | (name="CSV", version="0.5.9"), 36 | (name="DataFramesMeta", version="0.5.0"), 37 | (name="Feather", version="0.5.3"), 38 | (name="FreqTables", version="0.3.1"), 39 | (name="OnlineStats", version="0.27.0"), 40 | (name="MySQL", version="0.7.0"), 41 | (name="Cascadia", version="0.4.0"), 42 | (name="UnicodePlots", version="1.1.0"), 43 | (name="ParallelDataTransfer", version="0.5.0")] 44 | 45 | foreach(addandpin, pkg1) 46 | 47 | # packages that depend on Python Anaconda 48 | # whose manual installation is optional 49 | 50 | pkg2 = [(name="Conda", version="1.3.0"), 51 | (name="PyCall", version="1.91.2"), 52 | (name="PyPlot", version="2.8.1"), 53 | (name="Plots", version="0.26.0"), 54 | (name="StatsPlots", version="0.10.2")] 55 | 56 | foreach(addandpin, pkg2) 57 | 58 | # packages that have external dependencies 59 | # that have to be manually installed 60 | 61 | pkg3 = [(name="RCall", version="0.13.3"), 62 | (name="JDBC", version="0.4.1"), 63 | (name="LibPQ", version="0.11.0"), 64 | (name="Gurobi", version="0.6.0")] 65 | 66 | foreach(addandpin, pkg3) 67 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Juliaプログラミングクックブック 2 | 3 | ## このレポジトリについて 4 | 5 | このレポジトリでは、[『Juliaプログラミングクックブック』](https://www.oreilly.co.jp/books/9784873118895/)で使用するサンプルコードをダウンロードすることができます。 6 | 7 | 『Juliaプログラミングクックブック』の原書はPackt Publishingから発刊された[Julia 1.0 Programming Cookbook : Over 100 numerical and distributed computing recipes for your daily data science workflow](https://www.packtpub.com/application-development/julia-10-programming-cookbook)です。 8 | 9 | 原書のサンプルコードは 10 | https://github.com/PacktPublishing/Julia-1.0-Programming-Cookbook 11 | からダウンロード可能です。本サンプルコードは、日本語翻訳版の内容に合わせ、Julia 1.2で動くように原書のサンプルコードを修正しました。 12 | 13 | ## 使用法 14 | 15 | サンプルコードは、レシピごとにそれぞれのディレクトリに格納されています。それぞれのレシピには読者が入力するべきテキストを収めた`commands.txt`ファイルが用意されています。`commands.txt`ファイルの個々のエントリにはプロンプト(`$`や`julia>`)が付けられており、どの環境で入力するべきコマンドなのか(OSシェルなのかJuliaのインタラクティブモード(REPL)なのか)がわかるようになっています。ほとんどのレシピには、Juliaプログラムのソースコードを収めたファイルも用意されています。また、可能なレシピについては、Jupyter Notebookも用意しました。ファイルのリストと、その内容は、本文の各レシピの **「準備しよう」** の節に示してあります。 16 | 17 | 18 | ## 検証環境 19 | 20 | * macOS Mojave 10.14.6 21 | * Linux Ubuntu 18.04 22 | * Windows 10 Pro 23 | * Julia 1.2.0 24 | 25 | ## 使用パッケージのバージョン 26 | 27 | 本書の翻訳時に使用したパッケージのバージョンを次に挙げます。本レポジトリの 28 | `cookbookconf.jl`ファイルのスクリプトを用いると、本書のレシピで用いるすべてパッケージをインストールしてバージョンを固定することができます。パッケージの管理については1章の **「レシピ1.10 パッケージの管理」** を参考にしてください。 29 | 30 | 31 | | パッケージ名|バージョン|外部依存 32 | | -------- | --------| ----------------------------| 33 | | BSON | v0.2.3 | 依存なし | 34 | | BenchmarkTools |v0.4.2 | 依存なし | 35 | | CSV | v0.5.9 | 依存なし | 36 | | Cascadia |v0.4.0 | 依存なし | 37 | | Cbc | v0.6.2 | 依存なし | 38 | | Clp |v0.6.2 | 依存なし | 39 | | CodecZib | v0.6.0 | 依存なし | 40 | | Conda | v1.3.0 | 外部のPython Anacondaに依存してもよい | 41 | | DataFrames | v0.19.2 | 依存なし | 42 | | DataFramesMeta | v0.5.0 | 依存なし | 43 | | Distributions | v0.21.0 | 依存なし | 44 | | Feather | v0.5.3 | 依存なし | 45 | | FileIO | v1.0.7 | 依存なし | 46 | | ForwardDiff |v0.10.3 | 依存なし | 47 | | FreqTables |v0.3.1 | 依存なし | 48 | | Gumbo | v0.5.1 | 依存なし | 49 | | Gurobi | v0.6.0 | Gurobi Solver | 50 | | HTTP | v0.8.5 | 依存なし | 51 | | JDBC | v0.4.1 | Java実行環境 | 52 | | JLD2 | v0.1.2 | 依存なし | 53 | | JSON |v0.21.0 | 依存なし | 54 | | JuMP | v0.19.2 | 依存なし | 55 | | JuliaWebAPI |v0.6.0 | 依存なし | 56 | | LibPQ |v0.11.0 | PostgreSQL libpqライブラリ | 57 | | MySQL | v0.7.0 | 依存なし | 58 | | OnlineStats |v0.27.0 | 依存なし | 59 | | Optim | v0.19.1 | 依存なし | 60 | | Plots | v0.26.0 | 外部のPython Anacondaに依存してもよい (PyPlot描画バックエンド) | 61 | | ParallelDataTransfer | v0.5.0 | 依存なし | 62 | | ProfileView | v0.4.1 | 依存なし | 63 | | PyCall | v1.91.2 | 外部のPython Anacondaに依存してもよい | 64 | | PyPlot | v2.8.1 | 外部のPython Anacondaに依存してもよい | 65 | | RCall | v0.13.3 | GNU R | 66 | | Revise |v2.1.8 | 依存なし | 67 | | ScikitLearn | 最新バージョン | 外部のPython Anacondaに依存してもよい + Anaconda scikit-learnライブラリ | 68 | | StatsPlots |v0.10.2 | 外部のPython Anacondaに依存してもよい (PyPlot描画バックエンド) | 69 | | StaticArrays |v0.11.0 | 依存なし | 70 | | StatsBase | v0.31.0 | 依存なし | 71 | | StringEncodings |v0.3.1 | 依存なし | 72 | | TimeZones | v0.9.2 | 依存なし | 73 | | UnicodePlots |v1.1.0 | 依存なし | 74 | | XLSX | v0.5.4 | 依存なし | 75 | | ZMQ | v1.0.0 | 依存なし | 76 | 77 | 78 | 79 | 80 | パッケージを固定しなくてもレシピは動くかもしれませんが、APIが互換性のない形で変更されている場合にはうまく動かない場合もあります。そのような場合にはレシピの方を少し修正しなければならないかもしれません。 81 | -------------------------------------------------------------------------------- /Chapter02/05. 標準的でない基準でデータをソートする/2.5 標準的でない基準でデータをソートする.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "using Random, LinearAlgebra" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "Random.seed!(1);\n", 19 | "x = rand(1000, 1000);" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 3, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "x1 = sortslices(x, by=norm, dims=1);\n", 29 | "x2 = sortslices(x, lt=(x,y) -> norm(x) < norm(y), dims=1);\n", 30 | "x3 = x[sortperm([norm(view(x, i, :)) for i in 1:size(x,1)]), :];" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 4, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "true" 42 | ] 43 | }, 44 | "execution_count": 4, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "issorted(sum(x1.^2, dims=2))\n", 51 | "x1 == x2 == x3" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 5, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | " 0.191367 seconds (1.04 k allocations: 7.707 MiB, 2.10% gc time)\n" 64 | ] 65 | } 66 | ], 67 | "source": [ 68 | "@time x1 = sortslices(x, by=norm, dims=1);" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 6, 74 | "metadata": {}, 75 | "outputs": [ 76 | { 77 | "name": "stdout", 78 | "output_type": "stream", 79 | "text": [ 80 | " 0.314299 seconds (215.99 k allocations: 18.713 MiB, 1.45% gc time)\n" 81 | ] 82 | } 83 | ], 84 | "source": [ 85 | "@time x2 = sortslices(x, lt=(x,y) -> norm(x) < norm(y), dims=1);" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 7, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "name": "stdout", 95 | "output_type": "stream", 96 | "text": [ 97 | " 0.048002 seconds (59.11 k allocations: 10.586 MiB)\n" 98 | ] 99 | } 100 | ], 101 | "source": [ 102 | "@time x3 = x[sortperm([norm(view(x, i, :)) for i in 1:size(x, 1)]), :];" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [] 111 | } 112 | ], 113 | "metadata": { 114 | "@webio": { 115 | "lastCommId": null, 116 | "lastKernelId": null 117 | }, 118 | "kernelspec": { 119 | "display_name": "Julia 1.2.0", 120 | "language": "julia", 121 | "name": "julia-1.2" 122 | }, 123 | "language_info": { 124 | "file_extension": ".jl", 125 | "mimetype": "application/julia", 126 | "name": "julia", 127 | "version": "1.2.0" 128 | } 129 | }, 130 | "nbformat": 4, 131 | "nbformat_minor": 2 132 | } 133 | -------------------------------------------------------------------------------- /Chapter05/05. 変数のスコープを理解する/commands.txt: -------------------------------------------------------------------------------- 1 | julia> a, b = 1, 2; 2 | julia> let a=30, b=40 3 | let b=500 4 | println("inner scope $a $b") 5 | end 6 | println("outer scope $a $b") 7 | end 8 | julia> println("global scope $a $b") 9 | 10 | julia> x=5; 11 | julia> let 12 | println(x+1) 13 | end 14 | julia> let 15 | x = x+1 16 | end 17 | julia> let 18 | global x = x+1 19 | end 20 | 21 | # この関数は実行に失敗する 22 | function twogram(s::AbstractString) 23 | twograms = String[] 24 | for (i, c) in enumerate(s) 25 | if i == 1 26 | prev = c 27 | else 28 | push!(twograms, string(prev, c)) 29 | prev = c 30 | end 31 | end 32 | twograms 33 | end 34 | 35 | julia> twogram("ABCD") 36 | 37 | function twogram2(s::AbstractString) 38 | twograms = String[] 39 | local prev 40 | for (i, c) in enumerate(s) 41 | if i == 1 42 | prev = c 43 | else 44 | push!(twograms, string(prev, c)) 45 | prev = c 46 | end 47 | end 48 | twograms 49 | end 50 | 51 | julia> twogram2("ABCD") 52 | 53 | julia> s="ABCD"; 54 | julia> twograms = String[]; 55 | julia> local prev 56 | julia> for (i, c) in enumerate(s) 57 | if i == 1 58 | prev = c 59 | else 60 | push!(twograms, string(prev, c)) 61 | prev = c 62 | end 63 | end 64 | 65 | julia> s="ABCD"; 66 | julia> twograms = String[]; 67 | julia> let 68 | local prev 69 | for (i, c) in enumerate(s) 70 | if i == 1 71 | prev = c 72 | else 73 | push!(twograms, string(prev, c)) 74 | prev = c 75 | end 76 | end 77 | twograms 78 | end 79 | 80 | julia> s="ABCD"; 81 | julia> twograms = String[]; 82 | julia> for (i, c) in enumerate(s) 83 | if i == 1 84 | global prev = c 85 | else 86 | push!(twograms, string(prev, c)) 87 | global prev = c 88 | end 89 | end 90 | julia> twograms 91 | 92 | 93 | module B 94 | x = 1 95 | function getxplusone() 96 | return x+1 97 | end 98 | function increasex() 99 | return x+=1 100 | end 101 | function increasexglob() 102 | return global x+=1 103 | end 104 | end 105 | 106 | julia> B.getxplusone() 107 | julia> B.increasex() 108 | julia> B.increasexglob() 109 | 110 | function f() 111 | x = 1 112 | for a in 1:10 113 | x +=1 114 | end 115 | x 116 | end 117 | 118 | julia> f() 119 | 120 | julia> x=1; 121 | julia> for a in 1:10 122 | x +=1 123 | end 124 | 125 | julia> let 126 | x = 1 127 | for a in 1:10 128 | x +=1 129 | end 130 | x 131 | end 132 | 133 | julia> x = 1 134 | julia> for a in 1:10 135 | global x += 1 136 | end 137 | julia> x 138 | 139 | 140 | julia> z=5; 141 | julia> [(x=z+i;x) for i in 1:2] 142 | 143 | julia> z = 5; 144 | julia> [(z=z+i;z) for i in 1:2] 145 | 146 | julia> z = 5; 147 | julia> [(global z=z+i;z) for i in 1:2] 148 | 149 | julia> let state = 0 150 | global counter() = (state += 1) 151 | end; 152 | julia> counter() 153 | julia> counter() 154 | 155 | julia> state 156 | -------------------------------------------------------------------------------- /Chapter03/04. 簡単なRESTfulサービスを作ってみる/3.04 簡単なRESTfulサービスを作ってみる-ZeroMQ Server.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "using JuliaWebAPI\n", 10 | "using ZMQ" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "data": { 20 | "text/plain": [ 21 | "testfn (generic function with 1 method)" 22 | ] 23 | }, 24 | "execution_count": 2, 25 | "metadata": {}, 26 | "output_type": "execute_result" 27 | } 28 | ], 29 | "source": [ 30 | "function testfn(arg1, arg2; optarg1=\"10\", optarg2=\"20\")\n", 31 | " println(\"T: \", arg1, \" \", arg2, \" \", optarg1, \" \", optarg2)\n", 32 | " return parse(Int,arg1) + parse(Int,arg2) + parse(Int,optarg1) + parse(Int,optarg2)\n", 33 | "end" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 3, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "tr = JuliaWebAPI.ZMQTransport(\"tcp://127.0.0.1:9999\", ZMQ.REP, true);\n", 43 | "apir = JuliaWebAPI.APIResponder(tr, JuliaWebAPI.JSONMsgFormat());" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 4, 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "name": "stderr", 53 | "output_type": "stream", 54 | "text": [ 55 | "┌ Info: registering\n", 56 | "│ endpt = testfn\n", 57 | "└ @ JuliaWebAPI /home/nakada/.julia/packages/JuliaWebAPI/9VrIU/src/APIResponder.jl:52\n" 58 | ] 59 | }, 60 | { 61 | "data": { 62 | "text/plain": [ 63 | "JuliaWebAPI.APIResponder with 1 endpoints (testfn)" 64 | ] 65 | }, 66 | "execution_count": 4, 67 | "metadata": {}, 68 | "output_type": "execute_result" 69 | } 70 | ], 71 | "source": [ 72 | "register(apir, testfn; resp_json=true,\n", 73 | " resp_headers = Dict(\"Content-Type\" => \"application/json;charset=utf-8\"))" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "name": "stderr", 83 | "output_type": "stream", 84 | "text": [ 85 | "┌ Info: received\n", 86 | "│ command = testfn\n", 87 | "└ @ JuliaWebAPI /home/nakada/.julia/packages/JuliaWebAPI/9VrIU/src/APIResponder.jl:141\n" 88 | ] 89 | }, 90 | { 91 | "name": "stdout", 92 | "output_type": "stream", 93 | "text": [ 94 | "T: 5 9 100 1000\n" 95 | ] 96 | } 97 | ], 98 | "source": [ 99 | "process(apir)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": null, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [] 115 | } 116 | ], 117 | "metadata": { 118 | "@webio": { 119 | "lastCommId": null, 120 | "lastKernelId": null 121 | }, 122 | "kernelspec": { 123 | "display_name": "Julia 1.2.0", 124 | "language": "julia", 125 | "name": "julia-1.2" 126 | }, 127 | "language_info": { 128 | "file_extension": ".jl", 129 | "mimetype": "application/julia", 130 | "name": "julia", 131 | "version": "1.2.0" 132 | } 133 | }, 134 | "nbformat": 4, 135 | "nbformat_minor": 2 136 | } 137 | -------------------------------------------------------------------------------- /Chapter07/05. 欠損値を扱う/grades.csv: -------------------------------------------------------------------------------- 1 | Prefix,Assignment,Tutorial,Midterm,TakeHome,Final 2 | 05,57.14,34.09,64.38,51.48,52.50 3 | 08,95.05,105.49,67.50,99.07,68.33 4 | 08,83.70,83.17,30.0,63.15,48.89 5 | 07,81.22,96.06,49.38,105.93,80.56 6 | 08,91.32,93.64,95.0,107.41,73.89 7 | 07,95.0,92.58,93.12,97.78,68.06 8 | 08,95.05,102.99,56.25,99.07,50.0 9 | 07,72.85,86.85,60.0,,56.11 10 | 08,84.26,93.10,47.50,18.52,50.83 11 | 07,90.10,97.55,51.25,88.89,63.61 12 | 07,80.44,90.20,75.0,91.48,39.72 13 | 06,86.26,80.60,74.38,87.59,77.50 14 | 08,97.16,103.71,72.50,93.52,63.33 15 | 07,91.28,83.53,81.25,99.81,92.22 16 | 08,84.80,89.08,44.38,16.91,35.83 17 | 07,93.83,95.43,88.12,80.93,90.0 18 | 08,84.80,89.08,47.50,16.91,53.33 19 | 04,92.01,102.52,38.75,86.11,49.17 20 | 08,55.14,81.85,75.0,56.11,62.50 21 | 08,93.04,82.93,79.38,83.33,91.11 22 | 08,63.40,86.21,63.12,72.78,, 23 | 08,75.27,97.52,63.12,61.11,66.11 24 | 08,63.78,76.21,39.38,42.22,34.44 25 | 07,80.44,90.20,46.25,91.48,72.22 26 | 07,53.36,82.01,74.38,102.59,56.39 27 | 06,91.28,95.24,82.50,97.59,92.78 28 | 08,82.45,86.65,93.12,85.56,89.17 29 | 08,75.27,86.67,69.38,61.11,88.89 30 | 08,91.32,94.89,76.25,107.41,85.56 31 | 07,91.62,65.18,71.88,90.0,45.56 32 | 07,98.58,102.46,67.50,97.59,63.33 33 | 07,86.26,88.57,70.0,87.59,55.0 34 | 08,67.29,95.64,48.12,72.22,43.33 35 | 07,98.58,91.03,101.25,104.26,107.78 36 | 08,85.42,95.67,56.25,103.52,64.72 37 | 05,88.09,63.39,74.38,93.70,50.83 38 | 06,95.05,70.24,52.50,52.41,47.78 39 | 07,89.89,57.97,32.50,85.19,51.67 40 | 06,90.74,89.64,61.25,90.0,, 41 | 07,95.0,94.36,89.38,100.93,85.0 42 | 06,28.14,58.51,72.50,53.70,68.33 43 | 07,95.14,82.67,110.0,89.81,90.83 44 | 07,92.01,112.58,86.25,86.11,83.33 45 | 07,86.26,74.66,85.0,64.07,82.22 46 | 06,57.14,34.09,66.88,51.48,55.83 47 | 07,93.83,57.32,28.12,77.96,45.56 48 | 08,68.95,65.11,44.38,57.41,65.28 49 | 08,85.01,98.47,91.25,83.33,72.22 50 | 08,95.90,99.99,95.62,105.56,102.22 51 | 08,92.46,95.75,61.88,83.33,48.89 52 | 08,96.73,88.11,71.88,97.41,65.56 53 | 08,83.70,83.17,60.62,63.15,57.78 54 | 07,95.14,94.01,99.38,100.0,95.0 55 | 07,98.58,88.30,90.62,100.93,99.17 56 | 08,71.79,102.87,54.37,21.53,36.11 57 | 08,71.79,101.68,75.0,21.53,49.44 58 | 08,87.93,106.53,37.50,97.41,28.06 59 | 08,87.93,108.97,28.75,87.96,47.78 60 | 08,68.95,65.11,40.0,57.41,78.89 61 | 07,72.85,86.85,41.25,60.37,46.67 62 | 08,71.79,102.87,41.88,24.77,, 63 | 08,92.02,97.76,46.25,47.22,60.56 64 | 07,90.33,87.56,68.75,77.96,58.33 65 | 07,95.0,94.36,90.62,100.93,101.11 66 | 07,91.28,108.71,96.25,99.81,88.89 67 | 08,97.0,103.02,93.12,106.48,94.44 68 | 08,93.01,104.18,55.0,96.85,67.22 69 | 08,92.02,100.58,54.37,63.89,63.89 70 | 07,100.83,105.57,101.25,104.44,108.89 71 | 08,80.53,92.80,51.25,72.78,66.67 72 | 08,90.98,97.55,86.25,88.89,90.0 73 | 08,93.59,103.83,92.50,96.85,87.22 74 | 08,97.33,100.42,69.38,102.59,83.06 75 | 07,84.26,91.31,63.12,83.33,75.56 76 | 08,84.26,96.66,52.50,83.33,50.0 77 | 07,93.83,102.19,106.25,94.44,102.78 78 | 08,75.27,86.67,70.0,71.85,80.0 79 | 08,92.02,100.58,73.12,63.89,65.28 80 | 08,97.16,103.71,83.75,95.93,78.89 81 | 08,66.17,93.68,71.88,42.22,61.39 82 | 08,81.22,91.95,79.38,105.93,90.0 83 | 07,74.29,65.70,78.75,103.52,55.0 84 | 08,97.33,106.74,76.88,108.89,83.89 85 | 04,86.86,62.64,92.50,85.19,62.78 86 | 06,95.60,61.40,64.38,99.81,42.78 87 | 04,87.93,99.47,53.12,87.96,61.11 88 | 06,98.49,95.43,42.50,24.77,39.44 89 | 07,74.35,92.93,86.25,78.70,73.89 90 | 07,86.29,88.81,83.12,77.96,75.83 91 | 08,97.0,100.52,64.38,90.74,58.61 92 | 08,97.33,106.74,81.25,108.89,71.11 93 | 08,96.41,103.71,56.25,95.93,66.39 94 | 07,95.60,82.28,76.88,108.33,78.33 95 | 08,87.52,91.58,56.25,71.85,85.0 96 | 08,96.73,103.71,45.0,93.52,61.94 97 | 07,85.34,80.54,41.25,93.70,39.72 98 | 08,89.94,102.77,87.50,90.74,87.78 99 | 07,95.60,76.13,66.25,99.81,85.56 100 | 08,63.40,97.37,73.12,72.78,77.22 -------------------------------------------------------------------------------- /Chapter07/09. データフレームの行を変換する/grades.csv: -------------------------------------------------------------------------------- 1 | Prefix,Assignment,Tutorial,Midterm,TakeHome,Final 2 | 05,57.14,34.09,64.38,51.48,52.50 3 | 08,95.05,105.49,67.50,99.07,68.33 4 | 08,83.70,83.17,30.0,63.15,48.89 5 | 07,81.22,96.06,49.38,105.93,80.56 6 | 08,91.32,93.64,95.0,107.41,73.89 7 | 07,95.0,92.58,93.12,97.78,68.06 8 | 08,95.05,102.99,56.25,99.07,50.0 9 | 07,72.85,86.85,60.0,,56.11 10 | 08,84.26,93.10,47.50,18.52,50.83 11 | 07,90.10,97.55,51.25,88.89,63.61 12 | 07,80.44,90.20,75.0,91.48,39.72 13 | 06,86.26,80.60,74.38,87.59,77.50 14 | 08,97.16,103.71,72.50,93.52,63.33 15 | 07,91.28,83.53,81.25,99.81,92.22 16 | 08,84.80,89.08,44.38,16.91,35.83 17 | 07,93.83,95.43,88.12,80.93,90.0 18 | 08,84.80,89.08,47.50,16.91,53.33 19 | 04,92.01,102.52,38.75,86.11,49.17 20 | 08,55.14,81.85,75.0,56.11,62.50 21 | 08,93.04,82.93,79.38,83.33,91.11 22 | 08,63.40,86.21,63.12,72.78,, 23 | 08,75.27,97.52,63.12,61.11,66.11 24 | 08,63.78,76.21,39.38,42.22,34.44 25 | 07,80.44,90.20,46.25,91.48,72.22 26 | 07,53.36,82.01,74.38,102.59,56.39 27 | 06,91.28,95.24,82.50,97.59,92.78 28 | 08,82.45,86.65,93.12,85.56,89.17 29 | 08,75.27,86.67,69.38,61.11,88.89 30 | 08,91.32,94.89,76.25,107.41,85.56 31 | 07,91.62,65.18,71.88,90.0,45.56 32 | 07,98.58,102.46,67.50,97.59,63.33 33 | 07,86.26,88.57,70.0,87.59,55.0 34 | 08,67.29,95.64,48.12,72.22,43.33 35 | 07,98.58,91.03,101.25,104.26,107.78 36 | 08,85.42,95.67,56.25,103.52,64.72 37 | 05,88.09,63.39,74.38,93.70,50.83 38 | 06,95.05,70.24,52.50,52.41,47.78 39 | 07,89.89,57.97,32.50,85.19,51.67 40 | 06,90.74,89.64,61.25,90.0,, 41 | 07,95.0,94.36,89.38,100.93,85.0 42 | 06,28.14,58.51,72.50,53.70,68.33 43 | 07,95.14,82.67,110.0,89.81,90.83 44 | 07,92.01,112.58,86.25,86.11,83.33 45 | 07,86.26,74.66,85.0,64.07,82.22 46 | 06,57.14,34.09,66.88,51.48,55.83 47 | 07,93.83,57.32,28.12,77.96,45.56 48 | 08,68.95,65.11,44.38,57.41,65.28 49 | 08,85.01,98.47,91.25,83.33,72.22 50 | 08,95.90,99.99,95.62,105.56,102.22 51 | 08,92.46,95.75,61.88,83.33,48.89 52 | 08,96.73,88.11,71.88,97.41,65.56 53 | 08,83.70,83.17,60.62,63.15,57.78 54 | 07,95.14,94.01,99.38,100.0,95.0 55 | 07,98.58,88.30,90.62,100.93,99.17 56 | 08,71.79,102.87,54.37,21.53,36.11 57 | 08,71.79,101.68,75.0,21.53,49.44 58 | 08,87.93,106.53,37.50,97.41,28.06 59 | 08,87.93,108.97,28.75,87.96,47.78 60 | 08,68.95,65.11,40.0,57.41,78.89 61 | 07,72.85,86.85,41.25,60.37,46.67 62 | 08,71.79,102.87,41.88,24.77,, 63 | 08,92.02,97.76,46.25,47.22,60.56 64 | 07,90.33,87.56,68.75,77.96,58.33 65 | 07,95.0,94.36,90.62,100.93,101.11 66 | 07,91.28,108.71,96.25,99.81,88.89 67 | 08,97.0,103.02,93.12,106.48,94.44 68 | 08,93.01,104.18,55.0,96.85,67.22 69 | 08,92.02,100.58,54.37,63.89,63.89 70 | 07,100.83,105.57,101.25,104.44,108.89 71 | 08,80.53,92.80,51.25,72.78,66.67 72 | 08,90.98,97.55,86.25,88.89,90.0 73 | 08,93.59,103.83,92.50,96.85,87.22 74 | 08,97.33,100.42,69.38,102.59,83.06 75 | 07,84.26,91.31,63.12,83.33,75.56 76 | 08,84.26,96.66,52.50,83.33,50.0 77 | 07,93.83,102.19,106.25,94.44,102.78 78 | 08,75.27,86.67,70.0,71.85,80.0 79 | 08,92.02,100.58,73.12,63.89,65.28 80 | 08,97.16,103.71,83.75,95.93,78.89 81 | 08,66.17,93.68,71.88,42.22,61.39 82 | 08,81.22,91.95,79.38,105.93,90.0 83 | 07,74.29,65.70,78.75,103.52,55.0 84 | 08,97.33,106.74,76.88,108.89,83.89 85 | 04,86.86,62.64,92.50,85.19,62.78 86 | 06,95.60,61.40,64.38,99.81,42.78 87 | 04,87.93,99.47,53.12,87.96,61.11 88 | 06,98.49,95.43,42.50,24.77,39.44 89 | 07,74.35,92.93,86.25,78.70,73.89 90 | 07,86.29,88.81,83.12,77.96,75.83 91 | 08,97.0,100.52,64.38,90.74,58.61 92 | 08,97.33,106.74,81.25,108.89,71.11 93 | 08,96.41,103.71,56.25,95.93,66.39 94 | 07,95.60,82.28,76.88,108.33,78.33 95 | 08,87.52,91.58,56.25,71.85,85.0 96 | 08,96.73,103.71,45.0,93.52,61.94 97 | 07,85.34,80.54,41.25,93.70,39.72 98 | 08,89.94,102.77,87.50,90.74,87.78 99 | 07,95.60,76.13,66.25,99.81,85.56 100 | 08,63.40,97.37,73.12,72.78,77.22 -------------------------------------------------------------------------------- /Chapter07/08. データフレームの同一性を判定する/grades.csv: -------------------------------------------------------------------------------- 1 | Prefix,Assignment,Tutorial,Midterm,TakeHome,Final 2 | 05,57.14,34.09,64.38,51.48,52.50 3 | 08,95.05,105.49,67.50,99.07,68.33 4 | 08,83.70,83.17,30.0,63.15,48.89 5 | 07,81.22,96.06,49.38,105.93,80.56 6 | 08,91.32,93.64,95.0,107.41,73.89 7 | 07,95.0,92.58,93.12,97.78,68.06 8 | 08,95.05,102.99,56.25,99.07,50.0 9 | 07,72.85,86.85,60.0,,56.11 10 | 08,84.26,93.10,47.50,18.52,50.83 11 | 07,90.10,97.55,51.25,88.89,63.61 12 | 07,80.44,90.20,75.0,91.48,39.72 13 | 06,86.26,80.60,74.38,87.59,77.50 14 | 08,97.16,103.71,72.50,93.52,63.33 15 | 07,91.28,83.53,81.25,99.81,92.22 16 | 08,84.80,89.08,44.38,16.91,35.83 17 | 07,93.83,95.43,88.12,80.93,90.0 18 | 08,84.80,89.08,47.50,16.91,53.33 19 | 04,92.01,102.52,38.75,86.11,49.17 20 | 08,55.14,81.85,75.0,56.11,62.50 21 | 08,93.04,82.93,79.38,83.33,91.11 22 | 08,63.40,86.21,63.12,72.78,, 23 | 08,75.27,97.52,63.12,61.11,66.11 24 | 08,63.78,76.21,39.38,42.22,34.44 25 | 07,80.44,90.20,46.25,91.48,72.22 26 | 07,53.36,82.01,74.38,102.59,56.39 27 | 06,91.28,95.24,82.50,97.59,92.78 28 | 08,82.45,86.65,93.12,85.56,89.17 29 | 08,75.27,86.67,69.38,61.11,88.89 30 | 08,91.32,94.89,76.25,107.41,85.56 31 | 07,91.62,65.18,71.88,90.0,45.56 32 | 07,98.58,102.46,67.50,97.59,63.33 33 | 07,86.26,88.57,70.0,87.59,55.0 34 | 08,67.29,95.64,48.12,72.22,43.33 35 | 07,98.58,91.03,101.25,104.26,107.78 36 | 08,85.42,95.67,56.25,103.52,64.72 37 | 05,88.09,63.39,74.38,93.70,50.83 38 | 06,95.05,70.24,52.50,52.41,47.78 39 | 07,89.89,57.97,32.50,85.19,51.67 40 | 06,90.74,89.64,61.25,90.0,, 41 | 07,95.0,94.36,89.38,100.93,85.0 42 | 06,28.14,58.51,72.50,53.70,68.33 43 | 07,95.14,82.67,110.0,89.81,90.83 44 | 07,92.01,112.58,86.25,86.11,83.33 45 | 07,86.26,74.66,85.0,64.07,82.22 46 | 06,57.14,34.09,66.88,51.48,55.83 47 | 07,93.83,57.32,28.12,77.96,45.56 48 | 08,68.95,65.11,44.38,57.41,65.28 49 | 08,85.01,98.47,91.25,83.33,72.22 50 | 08,95.90,99.99,95.62,105.56,102.22 51 | 08,92.46,95.75,61.88,83.33,48.89 52 | 08,96.73,88.11,71.88,97.41,65.56 53 | 08,83.70,83.17,60.62,63.15,57.78 54 | 07,95.14,94.01,99.38,100.0,95.0 55 | 07,98.58,88.30,90.62,100.93,99.17 56 | 08,71.79,102.87,54.37,21.53,36.11 57 | 08,71.79,101.68,75.0,21.53,49.44 58 | 08,87.93,106.53,37.50,97.41,28.06 59 | 08,87.93,108.97,28.75,87.96,47.78 60 | 08,68.95,65.11,40.0,57.41,78.89 61 | 07,72.85,86.85,41.25,60.37,46.67 62 | 08,71.79,102.87,41.88,24.77,, 63 | 08,92.02,97.76,46.25,47.22,60.56 64 | 07,90.33,87.56,68.75,77.96,58.33 65 | 07,95.0,94.36,90.62,100.93,101.11 66 | 07,91.28,108.71,96.25,99.81,88.89 67 | 08,97.0,103.02,93.12,106.48,94.44 68 | 08,93.01,104.18,55.0,96.85,67.22 69 | 08,92.02,100.58,54.37,63.89,63.89 70 | 07,100.83,105.57,101.25,104.44,108.89 71 | 08,80.53,92.80,51.25,72.78,66.67 72 | 08,90.98,97.55,86.25,88.89,90.0 73 | 08,93.59,103.83,92.50,96.85,87.22 74 | 08,97.33,100.42,69.38,102.59,83.06 75 | 07,84.26,91.31,63.12,83.33,75.56 76 | 08,84.26,96.66,52.50,83.33,50.0 77 | 07,93.83,102.19,106.25,94.44,102.78 78 | 08,75.27,86.67,70.0,71.85,80.0 79 | 08,92.02,100.58,73.12,63.89,65.28 80 | 08,97.16,103.71,83.75,95.93,78.89 81 | 08,66.17,93.68,71.88,42.22,61.39 82 | 08,81.22,91.95,79.38,105.93,90.0 83 | 07,74.29,65.70,78.75,103.52,55.0 84 | 08,97.33,106.74,76.88,108.89,83.89 85 | 04,86.86,62.64,92.50,85.19,62.78 86 | 06,95.60,61.40,64.38,99.81,42.78 87 | 04,87.93,99.47,53.12,87.96,61.11 88 | 06,98.49,95.43,42.50,24.77,39.44 89 | 07,74.35,92.93,86.25,78.70,73.89 90 | 07,86.29,88.81,83.12,77.96,75.83 91 | 08,97.0,100.52,64.38,90.74,58.61 92 | 08,97.33,106.74,81.25,108.89,71.11 93 | 08,96.41,103.71,56.25,95.93,66.39 94 | 07,95.60,82.28,76.88,108.33,78.33 95 | 08,87.52,91.58,56.25,71.85,85.0 96 | 08,96.73,103.71,45.0,93.52,61.94 97 | 07,85.34,80.54,41.25,93.70,39.72 98 | 08,89.94,102.77,87.50,90.74,87.78 99 | 07,95.60,76.13,66.25,99.81,85.56 100 | 08,63.40,97.37,73.12,72.78,77.22 -------------------------------------------------------------------------------- /Julia_Cookbook-Supplement.md: -------------------------------------------------------------------------------- 1 | [B. Kamiński, P. Szufel: Julia 1.0 Programming Cookbook](https://www.packtpub.com/application-development/julia-10-programming-cookbook) 2 | ======================================================================================================================================== 3 | 4 | 5 | # Supplement: Julia installation for macOS 6 | 7 | 8 | In this supplement we explain how to use configure Julia environment on macOS. 9 | The discussed examples have been tested with macOS Mojave, version 10.14.2 10 | but Julia is known to work with any macOS version starting from 10.8. 11 | 12 | ### Installing Julia on macOS 13 | 14 | The recommended way to install Julia on macOS is to use the binary release that 15 | is available at [https://julialang.org/downloads/](https://julialang.org/downloads/). 16 | The steps for macOS platform are very similar to the previously presented examples for Linux. 17 | 18 | Julia installer comes as a standard macOS `*.dmg` file. 19 | Hence, you need to simply download it (for example with the Safari browser), 20 | double-click the downloaded file and drag the *Julia-1.0* icon onto the *Applications* icon. 21 | 22 | Once installed you should check what is the actual location of Julia. Use the following command: 23 | 24 | ```bash 25 | $ ls -d /Applications/Julia*/ 26 | ``` 27 | 28 | This will show the location of your Julia installation (for example `/Applications/Julia-1.0.app`). 29 | In the following steps we assume that Julia 1.0 has been installed in the above folder. 30 | 31 | Julia is often used in the command line console (Terminal). It can be started with the following command: 32 | 33 | ```bash 34 | $ /Applications/Julia-1.0.app/Contents/Resources/julia/bin/julia 35 | ``` 36 | 37 | Typing such a long command is not convenient. 38 | Hence we recommend creating a symbolic link to the `julia` executable file in the `/usr/local/bin` folder: 39 | 40 | ```bash 41 | $ sudo mkdir /usr/local/bin 42 | $ sudo ln -fs /Applications/Julia-1.0.app/Contents/Resources/julia/bin/julia /usr/local/bin/julia 43 | ``` 44 | 45 | Note that on some macOS installations the folder `/usr/local/bin` does not exist by default and hence we create it. 46 | 47 | Once the above two commands have been run, Julia REPL can be started in the terminal with the command `julia` rather than typing the entire path. 48 | 49 | When installing packages in Julia a complex compilation process takes place. 50 | Hence, compiling some of Julia packages (such as `Cbc.jl`) requires the `Xcode` developer tools. 51 | Their installation can be started by running the command below 52 | (note that this will start the graphical user interface installer): 53 | 54 | ```bash 55 | $ xcode-select --install 56 | ``` 57 | 58 | Additionally, the developer Command Line Tools are also required and can be 59 | installed with the following command (you might need to change the macOS version 60 | number at the end of the `pkg` file name): 61 | 62 | ```bash 63 | $ sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target / 64 | ``` 65 | 66 | ### Installing Juno IDE on macOS 67 | 68 | Juno is the recommended Julia development environment for macOS (as well as for Linux and Windows platforms). 69 | Once the link to Julia executable (`/usr/local/bin/julia`) is created the installation is straightforward. 70 | Simply go to the Atom website at [https://atom.io/](https://atom.io/), download the installer and double click the `pkg` file. 71 | Start Atom and add the Juno plugin - for details see the Juno paragraph at Julia IDEs recipe in the Chapter 1 of the book. 72 | If you follow the discussed Julia installation steps, Juno will just work out-of-the-box. 73 | 74 | ### Integration of other tools on macOS 75 | 76 | For configuration of other tools including Anaconda Python and Gnu R have a look at 77 | the Linux comments at relevant recipes - there are no differences between Linux and macOS Julia configuration. 78 | -------------------------------------------------------------------------------- /Chapter09/01. Juliaでデータベースを使う/commands.txt: -------------------------------------------------------------------------------- 1 | julia> ] 2 | (v1.2) pkg> add MySQL 3 | 4 | 5 | julia> ENV["PATH"]="C:\\Program Files\\PostgreSQL\\10\\lib;"*ENV["PATH"] 6 | julia> ] 7 | (v1.2) pkg> add LibPQ 8 | 9 | 10 | $ java -version 11 | 12 | julia> ] 13 | (v1.2) pkg> add JDBC 14 | 15 | 16 | julia> using DataFrames 17 | julia> using MySQL 18 | julia> conn = MySQL.connect("127.0.0.1", "root", "type_password_here",db="sys") 19 | 20 | julia> MySQL.execute!(conn, "CREATE TABLE mytable (col1 INT AUTO_INCREMENT PRIMARY KEY, col2 VARCHAR(50), col3 INT)") 21 | 22 | julia> st = MySQL.Stmt(conn, "INSERT INTO mytable(col2, col3) VALUES (?,?)"); 23 | julia> MySQL.execute!(st, ["testdata",7]); 24 | julia> MySQL.execute!(st, ["testdata2",8]); 25 | 26 | julia> df = MySQL.query(conn, "SELECT * FROM mytable") |> DataFrame 27 | 28 | julia> res = MySQL.query(conn, "SELECT * FROM mytable") 29 | 30 | julia> MySQL.disconnect(conn) 31 | 32 | 33 | julia> using DataFrames 34 | julia> using LibPQ 35 | julia> conn = LibPQ.Connection("host=localhost dbname=postgres user=postgres password="type_password_here") 36 | 37 | julia> LibPQ.execute(conn, "CREATE TABLE mytable (col1 SERIAL PRIMARY KEY NOT NULL, col2 VARCHAR(50), col3 INT)") 38 | 39 | julia> st = LibPQ.prepare(conn, "INSERT INTO MYTABLE(col2, col3) VALUES (\$1,\$2)") 40 | julia> LibPQ.execute(st,["testdata",7]) 41 | julia> LibPQ.execute(st,["testdata2",8]) 42 | 43 | julia> df = LibPQ.fetch!(DataFrame, LibPQ.execute(conn, "SELECT * FROM mytable")) 44 | 45 | julia> nt = LibPQ.fetch!(NamedTuple, LibPQ.execute(conn, "SELECT * FROM mytable")) 46 | 47 | julia> LibPQ.close(conn) 48 | 49 | 50 | using DataFrames 51 | using JDBC 52 | JDBC.usedriver("ojdbc8.jar") 53 | JDBC.init() 54 | 55 | conn = JDBC.DriverManager.getConnection("jdbc:oracle:thin:@ora.cez1pkekt7fj.us-east-2.rds.amazonaws.com:1521:ORCL",Dict("user"=>"orauser","password"=>"type_password_here")); 56 | 57 | st = JDBC.createStatement(conn); 58 | JDBC.execute(st, "CREATE TABLE mytable (col1 INT GENERATED ALWAYS AS IDENTITY NOT NULL," 59 | * " col2 VARCHAR2(50), col3 INT, CONSTRAINT col1 PRIMARY KEY (col1))"); 60 | 61 | pst = JDBC.prepareStatement(conn, "INSERT INTO mytable (col2, col3) VALUES (:1,:2)") 62 | JDBC.setString(pst,1,"testdata") 63 | JDBC.setInt(pst,2,7) 64 | JDBC.executeUpdate(pst) 65 | JDBC.setString(pst,1,"testdata") 66 | JDBC.setInt(pst,2,8) 67 | JDBC.executeUpdate(pst) 68 | JDBC.commit(conn) 69 | 70 | julia> rs = executeQuery(st, "select * from mytable"); 71 | julia> for r in rs 72 | println(JDBC.getInt(r,1),"|", 73 | JDBC.getString(r,2),"|",JDBC.getInt(r,3)) 74 | end 75 | 76 | julia> close(conn) 77 | 78 | julia> conn = JDBC.Connection("jdbc:oracle:thin:@ora.cez1pkekt7fj.us-east-2.rds.amazonaws.com:1521:ORCL", 79 | props=Dict("user"=>"orauser", 80 | "password"=>"type_password_here")); 81 | julia> csr=JDBC.Cursor(conn); 82 | julia> df = JDBC.load(DataFrame, csr, "select * from mytable") 83 | julia> nt = JDBC.load(NamedTuple, csr, "select * from mytable") 84 | julia> JDBC.close(csr) 85 | 86 | julia> JDBC.close(conn) 87 | 88 | 89 | using JDBC 90 | JDBC.usedriver("postgresql-42.2.4.jar") 91 | JDBC.init() 92 | conn = DriverManager.getConnection("jdbc:postgresql://localhost/postgres", 93 | Dict("user"=>"postgres", 94 | "password"=>"type_password_here")) 95 | 96 | 97 | using PyCall 98 | elasticsearch = pyimport("elasticsearch") 99 | 100 | es = elasticsearch.Elasticsearch() 101 | 102 | julia> es.info() 103 | 104 | dat = Dict("col1"=>"some text", "col2"=>"more text") 105 | res = es.index(index="data", doc_type="data", id="1", body=dat) 106 | 107 | q = Dict("query"=>Dict("match"=>Dict("col1"=>Dict("query"=>"some text")))) 108 | 109 | julia> es.search("data",body=q)["hits"]["hits"] 110 | -------------------------------------------------------------------------------- /Chapter06/05. イントロスペクションを使ってJuliaの数値型の構成を調べる/6.05 イントロスペクションを使ってJuliaの数値型の構成を調べる.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "supertypes (generic function with 1 method)" 12 | ] 13 | }, 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "function printsubtypes(T, indent=0)\n", 21 | " sT = subtypes(T)\n", 22 | " println(\" \"^indent, T, isempty(sT) ? \"\" : \":\")\n", 23 | " for S in sT\n", 24 | " printsubtypes(S, indent + 1)\n", 25 | " end\n", 26 | "end\n", 27 | "\n", 28 | "function supertypes(T)\n", 29 | " print(T)\n", 30 | " if T != Any\n", 31 | " print(\" <: \")\n", 32 | " S = supertype(T)\n", 33 | " supertypes(S)\n", 34 | " end\n", 35 | "end" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 2, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "Number:\n", 48 | " Complex\n", 49 | " Real:\n", 50 | " AbstractFloat:\n", 51 | " BigFloat\n", 52 | " Float16\n", 53 | " Float32\n", 54 | " Float64\n", 55 | " AbstractIrrational:\n", 56 | " Irrational\n", 57 | " Integer:\n", 58 | " Bool\n", 59 | " Signed:\n", 60 | " BigInt\n", 61 | " Int128\n", 62 | " Int16\n", 63 | " Int32\n", 64 | " Int64\n", 65 | " Int8\n", 66 | " Unsigned:\n", 67 | " UInt128\n", 68 | " UInt16\n", 69 | " UInt32\n", 70 | " UInt64\n", 71 | " UInt8\n", 72 | " Rational\n" 73 | ] 74 | } 75 | ], 76 | "source": [ 77 | "printsubtypes(Number)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 3, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "name": "stdout", 87 | "output_type": "stream", 88 | "text": [ 89 | "Bool <: Integer <: Real <: Number <: Any" 90 | ] 91 | } 92 | ], 93 | "source": [ 94 | "supertypes(Bool)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 4, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "Complex{Int64}" 106 | ] 107 | }, 108 | "execution_count": 4, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "typeof(1 + 1im)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 5, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "data": { 124 | "text/plain": [ 125 | "Complex{Float64}" 126 | ] 127 | }, 128 | "execution_count": 5, 129 | "metadata": {}, 130 | "output_type": "execute_result" 131 | } 132 | ], 133 | "source": [ 134 | "typeof(1.0 + 1.0im)" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": null, 140 | "metadata": { 141 | "collapsed": true 142 | }, 143 | "outputs": [], 144 | "source": [] 145 | } 146 | ], 147 | "metadata": { 148 | "@webio": { 149 | "lastCommId": null, 150 | "lastKernelId": null 151 | }, 152 | "kernelspec": { 153 | "display_name": "Julia 1.2.0", 154 | "language": "julia", 155 | "name": "julia-1.2" 156 | }, 157 | "language_info": { 158 | "file_extension": ".jl", 159 | "mimetype": "application/julia", 160 | "name": "julia", 161 | "version": "1.2.0" 162 | } 163 | }, 164 | "nbformat": 4, 165 | "nbformat_minor": 1 166 | } 167 | -------------------------------------------------------------------------------- /Chapter03/04. 簡単なRESTfulサービスを作ってみる/3.04 簡単なRESTfulサービスを作ってみる-ZeroMQ Web Server.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m registry at `~/.julia/registries/General`\n", 13 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m git-repo `https://github.com/JuliaRegistries/General.git`\n", 14 | "\u001b[?25l\u001b[2K\u001b[?25h\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n", 15 | "\u001b[32m\u001b[1m Installed\u001b[22m\u001b[39m JuliaWebAPI ─ v0.6.0\n", 16 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/environments/v1.2/Project.toml`\n", 17 | " \u001b[90m [480116ec]\u001b[39m\u001b[92m + JuliaWebAPI v0.6.0\u001b[39m\n", 18 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/environments/v1.2/Manifest.toml`\n", 19 | " \u001b[90m [480116ec]\u001b[39m\u001b[92m + JuliaWebAPI v0.6.0\u001b[39m\n", 20 | "\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n", 21 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/environments/v1.2/Project.toml`\n", 22 | " \u001b[90m [c2297ded]\u001b[39m\u001b[92m + ZMQ v1.0.0\u001b[39m\n", 23 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/environments/v1.2/Manifest.toml`\n", 24 | "\u001b[90m [no changes]\u001b[39m\n", 25 | "\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n", 26 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/environments/v1.2/Project.toml`\n", 27 | "\u001b[90m [no changes]\u001b[39m\n", 28 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/environments/v1.2/Manifest.toml`\n", 29 | "\u001b[90m [no changes]\u001b[39m\n" 30 | ] 31 | } 32 | ], 33 | "source": [ 34 | "#using Pkg;Pkg.add(\"JuliaWebAPI\"); Pkg.add(\"ZMQ\"); Pkg.add(\"HTTP\")" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 1, 40 | "metadata": {}, 41 | "outputs": [], 42 | "source": [ 43 | "using JuliaWebAPI" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 2, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "const apiclnt =JuliaWebAPI.APIInvoker(\"tcp://127.0.0.1:9999\");\n" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "name": "stderr", 62 | "output_type": "stream", 63 | "text": [ 64 | "┌ Info: running HTTP RPC server...\n", 65 | "└ @ JuliaWebAPI /home/nakada/.julia/packages/JuliaWebAPI/9VrIU/src/http_rpc_server.jl:230\n", 66 | "┌ Info: processing\n", 67 | "│ target = /testfn/5/9?optarg1=100&optarg2=1000\n", 68 | "└ @ JuliaWebAPI /home/nakada/.julia/packages/JuliaWebAPI/9VrIU/src/http_rpc_server.jl:157\n", 69 | "┌ Info: waiting for a handler\n", 70 | "└ @ JuliaWebAPI /home/nakada/.julia/packages/JuliaWebAPI/9VrIU/src/http_rpc_server.jl:182\n" 71 | ] 72 | } 73 | ], 74 | "source": [ 75 | "JuliaWebAPI.run_http(apiclnt, 8008)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [] 84 | } 85 | ], 86 | "metadata": { 87 | "@webio": { 88 | "lastCommId": null, 89 | "lastKernelId": null 90 | }, 91 | "kernelspec": { 92 | "display_name": "Julia 1.2.0", 93 | "language": "julia", 94 | "name": "julia-1.2" 95 | }, 96 | "language_info": { 97 | "file_extension": ".jl", 98 | "mimetype": "application/julia", 99 | "name": "julia", 100 | "version": "1.2.0" 101 | } 102 | }, 103 | "nbformat": 4, 104 | "nbformat_minor": 2 105 | } 106 | -------------------------------------------------------------------------------- /Chapter04/03. 完全実施要因計画の生成/4.03 完全実施要因計画の生成.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "expandgrid (generic function with 1 method)" 12 | ] 13 | }, 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "function expandgrid(levels...)\n", 21 | " lengths = length.(levels)\n", 22 | " inner = 1\n", 23 | " outer = prod(lengths)\n", 24 | " grid = []\n", 25 | " for i in 1:length(levels)\n", 26 | " outer = div(outer, lengths[i])\n", 27 | " push!(grid, repeat(levels[i], inner=inner, outer=outer))\n", 28 | " inner *= lengths[i]\n", 29 | " end\n", 30 | " Tuple(grid)\n", 31 | "end" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 2, 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "data": { 41 | "text/plain": [ 42 | "([1, 2, 1, 2], ['a', 'a', 'b', 'b'])" 43 | ] 44 | }, 45 | "execution_count": 2, 46 | "metadata": {}, 47 | "output_type": "execute_result" 48 | } 49 | ], 50 | "source": [ 51 | "expandgrid(1:2, 'a':'b')" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 3, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "data": { 61 | "text/plain": [ 62 | "12×3 Array{Any,2}:\n", 63 | " 1 true 'a'\n", 64 | " 2 true 'a'\n", 65 | " 3 true 'a'\n", 66 | " 1 false 'a'\n", 67 | " 2 false 'a'\n", 68 | " 3 false 'a'\n", 69 | " 1 true 'b'\n", 70 | " 2 true 'b'\n", 71 | " 3 true 'b'\n", 72 | " 1 false 'b'\n", 73 | " 2 false 'b'\n", 74 | " 3 false 'b'" 75 | ] 76 | }, 77 | "execution_count": 3, 78 | "metadata": {}, 79 | "output_type": "execute_result" 80 | } 81 | ], 82 | "source": [ 83 | "hcat(expandgrid(1:3, [true, false], 'a':'b')...)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 4, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "12×3 Array{Any,2}:\n", 95 | " 1 1 'a'\n", 96 | " 2 1 'a'\n", 97 | " 3 1 'a'\n", 98 | " 1 0 'a'\n", 99 | " 2 0 'a'\n", 100 | " 3 0 'a'\n", 101 | " 1 1 'b'\n", 102 | " 2 1 'b'\n", 103 | " 3 1 'b'\n", 104 | " 1 0 'b'\n", 105 | " 2 0 'b'\n", 106 | " 3 0 'b'" 107 | ] 108 | }, 109 | "execution_count": 4, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "reduce(hcat, expandgrid(1:3, [true, false], 'a':'b'))" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 5, 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "data": { 125 | "text/plain": [ 126 | "12-element Array{Int64,1}:\n", 127 | " 1\n", 128 | " 1\n", 129 | " 2\n", 130 | " 2\n", 131 | " 1\n", 132 | " 1\n", 133 | " 2\n", 134 | " 2\n", 135 | " 1\n", 136 | " 1\n", 137 | " 2\n", 138 | " 2" 139 | ] 140 | }, 141 | "execution_count": 5, 142 | "metadata": {}, 143 | "output_type": "execute_result" 144 | } 145 | ], 146 | "source": [ 147 | "repeat([1,2], inner=2, outer=3)" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": null, 153 | "metadata": { 154 | "collapsed": true 155 | }, 156 | "outputs": [], 157 | "source": [] 158 | } 159 | ], 160 | "metadata": { 161 | "kernelspec": { 162 | "display_name": "Julia 1.2.0", 163 | "language": "julia", 164 | "name": "julia-1.2" 165 | }, 166 | "language_info": { 167 | "file_extension": ".jl", 168 | "mimetype": "application/julia", 169 | "name": "julia", 170 | "version": "1.2.0" 171 | } 172 | }, 173 | "nbformat": 4, 174 | "nbformat_minor": 1 175 | } 176 | -------------------------------------------------------------------------------- /Chapter04/01. 行列処理を高速化する/4.01 行列処理を高速化する.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m registry at `~/.julia/registries/General`\n", 13 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m git-repo `https://github.com/JuliaRegistries/General.git`\n", 14 | "\u001b[?25l\u001b[2K\u001b[?25h\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n", 15 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/environments/v1.2/Project.toml`\n", 16 | "\u001b[90m [no changes]\u001b[39m\n", 17 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/environments/v1.2/Manifest.toml`\n", 18 | "\u001b[90m [no changes]\u001b[39m\n" 19 | ] 20 | } 21 | ], 22 | "source": [ 23 | "# using Pkg; Pkg.add(\"BenchmarkTools\")" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "sum_by_row (generic function with 1 method)" 35 | ] 36 | }, 37 | "execution_count": 2, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "function sum_by_col(x)\n", 44 | " s = zero(eltype(x))\n", 45 | " for j in 1:size(x, 2)\n", 46 | " for i in 1:size(x, 1)\n", 47 | " s += x[i, j]\n", 48 | " end \n", 49 | " end\n", 50 | " s\n", 51 | "end\n", 52 | "\n", 53 | "function sum_by_row(x)\n", 54 | " s = zero(eltype(x))\n", 55 | " for i in 1:size(x, 1)\n", 56 | " for j in 1:size(x, 2)\n", 57 | " s += x[i, j]\n", 58 | " end\n", 59 | " end\n", 60 | " s\n", 61 | "end\n" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 3, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "using BenchmarkTools" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 4, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "x = rand(10^4, 10^4);" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 5, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "name": "stdout", 89 | "output_type": "stream", 90 | "text": [ 91 | " 1.751 s (1 allocation: 16 bytes)\n" 92 | ] 93 | }, 94 | { 95 | "data": { 96 | "text/plain": [ 97 | "4.999789142513404e7" 98 | ] 99 | }, 100 | "execution_count": 5, 101 | "metadata": {}, 102 | "output_type": "execute_result" 103 | } 104 | ], 105 | "source": [ 106 | "@btime sum_by_row(x)" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 6, 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | " 105.205 ms (1 allocation: 16 bytes)\n" 119 | ] 120 | }, 121 | { 122 | "data": { 123 | "text/plain": [ 124 | "4.9997891425104626e7" 125 | ] 126 | }, 127 | "execution_count": 6, 128 | "metadata": {}, 129 | "output_type": "execute_result" 130 | } 131 | ], 132 | "source": [ 133 | "@btime sum_by_col(x)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 7, 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "name": "stdout", 143 | "output_type": "stream", 144 | "text": [ 145 | " 45.358 ms (1 allocation: 16 bytes)\n" 146 | ] 147 | }, 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "4.999789142511876e7" 152 | ] 153 | }, 154 | "execution_count": 7, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "@btime sum(x)" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "metadata": { 167 | "collapsed": true 168 | }, 169 | "outputs": [], 170 | "source": [] 171 | } 172 | ], 173 | "metadata": { 174 | "kernelspec": { 175 | "display_name": "Julia 1.2.0", 176 | "language": "julia", 177 | "name": "julia-1.2" 178 | }, 179 | "language_info": { 180 | "file_extension": ".jl", 181 | "mimetype": "application/julia", 182 | "name": "julia", 183 | "version": "1.2.0" 184 | } 185 | }, 186 | "nbformat": 4, 187 | "nbformat_minor": 1 188 | } 189 | -------------------------------------------------------------------------------- /Chapter07/03. インターネット上のCSVデータを読み込む/iris.csv: -------------------------------------------------------------------------------- 1 | 5.1,3.5,1.4,0.2,Iris-setosa 2 | 4.9,3.0,1.4,0.2,Iris-setosa 3 | 4.7,3.2,1.3,0.2,Iris-setosa 4 | 4.6,3.1,1.5,0.2,Iris-setosa 5 | 5.0,3.6,1.4,0.2,Iris-setosa 6 | 5.4,3.9,1.7,0.4,Iris-setosa 7 | 4.6,3.4,1.4,0.3,Iris-setosa 8 | 5.0,3.4,1.5,0.2,Iris-setosa 9 | 4.4,2.9,1.4,0.2,Iris-setosa 10 | 4.9,3.1,1.5,0.1,Iris-setosa 11 | 5.4,3.7,1.5,0.2,Iris-setosa 12 | 4.8,3.4,1.6,0.2,Iris-setosa 13 | 4.8,3.0,1.4,0.1,Iris-setosa 14 | 4.3,3.0,1.1,0.1,Iris-setosa 15 | 5.8,4.0,1.2,0.2,Iris-setosa 16 | 5.7,4.4,1.5,0.4,Iris-setosa 17 | 5.4,3.9,1.3,0.4,Iris-setosa 18 | 5.1,3.5,1.4,0.3,Iris-setosa 19 | 5.7,3.8,1.7,0.3,Iris-setosa 20 | 5.1,3.8,1.5,0.3,Iris-setosa 21 | 5.4,3.4,1.7,0.2,Iris-setosa 22 | 5.1,3.7,1.5,0.4,Iris-setosa 23 | 4.6,3.6,1.0,0.2,Iris-setosa 24 | 5.1,3.3,1.7,0.5,Iris-setosa 25 | 4.8,3.4,1.9,0.2,Iris-setosa 26 | 5.0,3.0,1.6,0.2,Iris-setosa 27 | 5.0,3.4,1.6,0.4,Iris-setosa 28 | 5.2,3.5,1.5,0.2,Iris-setosa 29 | 5.2,3.4,1.4,0.2,Iris-setosa 30 | 4.7,3.2,1.6,0.2,Iris-setosa 31 | 4.8,3.1,1.6,0.2,Iris-setosa 32 | 5.4,3.4,1.5,0.4,Iris-setosa 33 | 5.2,4.1,1.5,0.1,Iris-setosa 34 | 5.5,4.2,1.4,0.2,Iris-setosa 35 | 4.9,3.1,1.5,0.1,Iris-setosa 36 | 5.0,3.2,1.2,0.2,Iris-setosa 37 | 5.5,3.5,1.3,0.2,Iris-setosa 38 | 4.9,3.1,1.5,0.1,Iris-setosa 39 | 4.4,3.0,1.3,0.2,Iris-setosa 40 | 5.1,3.4,1.5,0.2,Iris-setosa 41 | 5.0,3.5,1.3,0.3,Iris-setosa 42 | 4.5,2.3,1.3,0.3,Iris-setosa 43 | 4.4,3.2,1.3,0.2,Iris-setosa 44 | 5.0,3.5,1.6,0.6,Iris-setosa 45 | 5.1,3.8,1.9,0.4,Iris-setosa 46 | 4.8,3.0,1.4,0.3,Iris-setosa 47 | 5.1,3.8,1.6,0.2,Iris-setosa 48 | 4.6,3.2,1.4,0.2,Iris-setosa 49 | 5.3,3.7,1.5,0.2,Iris-setosa 50 | 5.0,3.3,1.4,0.2,Iris-setosa 51 | 7.0,3.2,4.7,1.4,Iris-versicolor 52 | 6.4,3.2,4.5,1.5,Iris-versicolor 53 | 6.9,3.1,4.9,1.5,Iris-versicolor 54 | 5.5,2.3,4.0,1.3,Iris-versicolor 55 | 6.5,2.8,4.6,1.5,Iris-versicolor 56 | 5.7,2.8,4.5,1.3,Iris-versicolor 57 | 6.3,3.3,4.7,1.6,Iris-versicolor 58 | 4.9,2.4,3.3,1.0,Iris-versicolor 59 | 6.6,2.9,4.6,1.3,Iris-versicolor 60 | 5.2,2.7,3.9,1.4,Iris-versicolor 61 | 5.0,2.0,3.5,1.0,Iris-versicolor 62 | 5.9,3.0,4.2,1.5,Iris-versicolor 63 | 6.0,2.2,4.0,1.0,Iris-versicolor 64 | 6.1,2.9,4.7,1.4,Iris-versicolor 65 | 5.6,2.9,3.6,1.3,Iris-versicolor 66 | 6.7,3.1,4.4,1.4,Iris-versicolor 67 | 5.6,3.0,4.5,1.5,Iris-versicolor 68 | 5.8,2.7,4.1,1.0,Iris-versicolor 69 | 6.2,2.2,4.5,1.5,Iris-versicolor 70 | 5.6,2.5,3.9,1.1,Iris-versicolor 71 | 5.9,3.2,4.8,1.8,Iris-versicolor 72 | 6.1,2.8,4.0,1.3,Iris-versicolor 73 | 6.3,2.5,4.9,1.5,Iris-versicolor 74 | 6.1,2.8,4.7,1.2,Iris-versicolor 75 | 6.4,2.9,4.3,1.3,Iris-versicolor 76 | 6.6,3.0,4.4,1.4,Iris-versicolor 77 | 6.8,2.8,4.8,1.4,Iris-versicolor 78 | 6.7,3.0,5.0,1.7,Iris-versicolor 79 | 6.0,2.9,4.5,1.5,Iris-versicolor 80 | 5.7,2.6,3.5,1.0,Iris-versicolor 81 | 5.5,2.4,3.8,1.1,Iris-versicolor 82 | 5.5,2.4,3.7,1.0,Iris-versicolor 83 | 5.8,2.7,3.9,1.2,Iris-versicolor 84 | 6.0,2.7,5.1,1.6,Iris-versicolor 85 | 5.4,3.0,4.5,1.5,Iris-versicolor 86 | 6.0,3.4,4.5,1.6,Iris-versicolor 87 | 6.7,3.1,4.7,1.5,Iris-versicolor 88 | 6.3,2.3,4.4,1.3,Iris-versicolor 89 | 5.6,3.0,4.1,1.3,Iris-versicolor 90 | 5.5,2.5,4.0,1.3,Iris-versicolor 91 | 5.5,2.6,4.4,1.2,Iris-versicolor 92 | 6.1,3.0,4.6,1.4,Iris-versicolor 93 | 5.8,2.6,4.0,1.2,Iris-versicolor 94 | 5.0,2.3,3.3,1.0,Iris-versicolor 95 | 5.6,2.7,4.2,1.3,Iris-versicolor 96 | 5.7,3.0,4.2,1.2,Iris-versicolor 97 | 5.7,2.9,4.2,1.3,Iris-versicolor 98 | 6.2,2.9,4.3,1.3,Iris-versicolor 99 | 5.1,2.5,3.0,1.1,Iris-versicolor 100 | 5.7,2.8,4.1,1.3,Iris-versicolor 101 | 6.3,3.3,6.0,2.5,Iris-virginica 102 | 5.8,2.7,5.1,1.9,Iris-virginica 103 | 7.1,3.0,5.9,2.1,Iris-virginica 104 | 6.3,2.9,5.6,1.8,Iris-virginica 105 | 6.5,3.0,5.8,2.2,Iris-virginica 106 | 7.6,3.0,6.6,2.1,Iris-virginica 107 | 4.9,2.5,4.5,1.7,Iris-virginica 108 | 7.3,2.9,6.3,1.8,Iris-virginica 109 | 6.7,2.5,5.8,1.8,Iris-virginica 110 | 7.2,3.6,6.1,2.5,Iris-virginica 111 | 6.5,3.2,5.1,2.0,Iris-virginica 112 | 6.4,2.7,5.3,1.9,Iris-virginica 113 | 6.8,3.0,5.5,2.1,Iris-virginica 114 | 5.7,2.5,5.0,2.0,Iris-virginica 115 | 5.8,2.8,5.1,2.4,Iris-virginica 116 | 6.4,3.2,5.3,2.3,Iris-virginica 117 | 6.5,3.0,5.5,1.8,Iris-virginica 118 | 7.7,3.8,6.7,2.2,Iris-virginica 119 | 7.7,2.6,6.9,2.3,Iris-virginica 120 | 6.0,2.2,5.0,1.5,Iris-virginica 121 | 6.9,3.2,5.7,2.3,Iris-virginica 122 | 5.6,2.8,4.9,2.0,Iris-virginica 123 | 7.7,2.8,6.7,2.0,Iris-virginica 124 | 6.3,2.7,4.9,1.8,Iris-virginica 125 | 6.7,3.3,5.7,2.1,Iris-virginica 126 | 7.2,3.2,6.0,1.8,Iris-virginica 127 | 6.2,2.8,4.8,1.8,Iris-virginica 128 | 6.1,3.0,4.9,1.8,Iris-virginica 129 | 6.4,2.8,5.6,2.1,Iris-virginica 130 | 7.2,3.0,5.8,1.6,Iris-virginica 131 | 7.4,2.8,6.1,1.9,Iris-virginica 132 | 7.9,3.8,6.4,2.0,Iris-virginica 133 | 6.4,2.8,5.6,2.2,Iris-virginica 134 | 6.3,2.8,5.1,1.5,Iris-virginica 135 | 6.1,2.6,5.6,1.4,Iris-virginica 136 | 7.7,3.0,6.1,2.3,Iris-virginica 137 | 6.3,3.4,5.6,2.4,Iris-virginica 138 | 6.4,3.1,5.5,1.8,Iris-virginica 139 | 6.0,3.0,4.8,1.8,Iris-virginica 140 | 6.9,3.1,5.4,2.1,Iris-virginica 141 | 6.7,3.1,5.6,2.4,Iris-virginica 142 | 6.9,3.1,5.1,2.3,Iris-virginica 143 | 5.8,2.7,5.1,1.9,Iris-virginica 144 | 6.8,3.2,5.9,2.3,Iris-virginica 145 | 6.7,3.3,5.7,2.5,Iris-virginica 146 | 6.7,3.0,5.2,2.3,Iris-virginica 147 | 6.3,2.5,5.0,1.9,Iris-virginica 148 | 6.5,3.0,5.2,2.0,Iris-virginica 149 | 6.2,3.4,5.4,2.3,Iris-virginica 150 | 5.9,3.0,5.1,1.8,Iris-virginica 151 | 152 | -------------------------------------------------------------------------------- /Chapter07/06. データフレームを使って分割‐適用‐結合を行う/iris.csv: -------------------------------------------------------------------------------- 1 | 5.1,3.5,1.4,0.2,Iris-setosa 2 | 4.9,3.0,1.4,0.2,Iris-setosa 3 | 4.7,3.2,1.3,0.2,Iris-setosa 4 | 4.6,3.1,1.5,0.2,Iris-setosa 5 | 5.0,3.6,1.4,0.2,Iris-setosa 6 | 5.4,3.9,1.7,0.4,Iris-setosa 7 | 4.6,3.4,1.4,0.3,Iris-setosa 8 | 5.0,3.4,1.5,0.2,Iris-setosa 9 | 4.4,2.9,1.4,0.2,Iris-setosa 10 | 4.9,3.1,1.5,0.1,Iris-setosa 11 | 5.4,3.7,1.5,0.2,Iris-setosa 12 | 4.8,3.4,1.6,0.2,Iris-setosa 13 | 4.8,3.0,1.4,0.1,Iris-setosa 14 | 4.3,3.0,1.1,0.1,Iris-setosa 15 | 5.8,4.0,1.2,0.2,Iris-setosa 16 | 5.7,4.4,1.5,0.4,Iris-setosa 17 | 5.4,3.9,1.3,0.4,Iris-setosa 18 | 5.1,3.5,1.4,0.3,Iris-setosa 19 | 5.7,3.8,1.7,0.3,Iris-setosa 20 | 5.1,3.8,1.5,0.3,Iris-setosa 21 | 5.4,3.4,1.7,0.2,Iris-setosa 22 | 5.1,3.7,1.5,0.4,Iris-setosa 23 | 4.6,3.6,1.0,0.2,Iris-setosa 24 | 5.1,3.3,1.7,0.5,Iris-setosa 25 | 4.8,3.4,1.9,0.2,Iris-setosa 26 | 5.0,3.0,1.6,0.2,Iris-setosa 27 | 5.0,3.4,1.6,0.4,Iris-setosa 28 | 5.2,3.5,1.5,0.2,Iris-setosa 29 | 5.2,3.4,1.4,0.2,Iris-setosa 30 | 4.7,3.2,1.6,0.2,Iris-setosa 31 | 4.8,3.1,1.6,0.2,Iris-setosa 32 | 5.4,3.4,1.5,0.4,Iris-setosa 33 | 5.2,4.1,1.5,0.1,Iris-setosa 34 | 5.5,4.2,1.4,0.2,Iris-setosa 35 | 4.9,3.1,1.5,0.1,Iris-setosa 36 | 5.0,3.2,1.2,0.2,Iris-setosa 37 | 5.5,3.5,1.3,0.2,Iris-setosa 38 | 4.9,3.1,1.5,0.1,Iris-setosa 39 | 4.4,3.0,1.3,0.2,Iris-setosa 40 | 5.1,3.4,1.5,0.2,Iris-setosa 41 | 5.0,3.5,1.3,0.3,Iris-setosa 42 | 4.5,2.3,1.3,0.3,Iris-setosa 43 | 4.4,3.2,1.3,0.2,Iris-setosa 44 | 5.0,3.5,1.6,0.6,Iris-setosa 45 | 5.1,3.8,1.9,0.4,Iris-setosa 46 | 4.8,3.0,1.4,0.3,Iris-setosa 47 | 5.1,3.8,1.6,0.2,Iris-setosa 48 | 4.6,3.2,1.4,0.2,Iris-setosa 49 | 5.3,3.7,1.5,0.2,Iris-setosa 50 | 5.0,3.3,1.4,0.2,Iris-setosa 51 | 7.0,3.2,4.7,1.4,Iris-versicolor 52 | 6.4,3.2,4.5,1.5,Iris-versicolor 53 | 6.9,3.1,4.9,1.5,Iris-versicolor 54 | 5.5,2.3,4.0,1.3,Iris-versicolor 55 | 6.5,2.8,4.6,1.5,Iris-versicolor 56 | 5.7,2.8,4.5,1.3,Iris-versicolor 57 | 6.3,3.3,4.7,1.6,Iris-versicolor 58 | 4.9,2.4,3.3,1.0,Iris-versicolor 59 | 6.6,2.9,4.6,1.3,Iris-versicolor 60 | 5.2,2.7,3.9,1.4,Iris-versicolor 61 | 5.0,2.0,3.5,1.0,Iris-versicolor 62 | 5.9,3.0,4.2,1.5,Iris-versicolor 63 | 6.0,2.2,4.0,1.0,Iris-versicolor 64 | 6.1,2.9,4.7,1.4,Iris-versicolor 65 | 5.6,2.9,3.6,1.3,Iris-versicolor 66 | 6.7,3.1,4.4,1.4,Iris-versicolor 67 | 5.6,3.0,4.5,1.5,Iris-versicolor 68 | 5.8,2.7,4.1,1.0,Iris-versicolor 69 | 6.2,2.2,4.5,1.5,Iris-versicolor 70 | 5.6,2.5,3.9,1.1,Iris-versicolor 71 | 5.9,3.2,4.8,1.8,Iris-versicolor 72 | 6.1,2.8,4.0,1.3,Iris-versicolor 73 | 6.3,2.5,4.9,1.5,Iris-versicolor 74 | 6.1,2.8,4.7,1.2,Iris-versicolor 75 | 6.4,2.9,4.3,1.3,Iris-versicolor 76 | 6.6,3.0,4.4,1.4,Iris-versicolor 77 | 6.8,2.8,4.8,1.4,Iris-versicolor 78 | 6.7,3.0,5.0,1.7,Iris-versicolor 79 | 6.0,2.9,4.5,1.5,Iris-versicolor 80 | 5.7,2.6,3.5,1.0,Iris-versicolor 81 | 5.5,2.4,3.8,1.1,Iris-versicolor 82 | 5.5,2.4,3.7,1.0,Iris-versicolor 83 | 5.8,2.7,3.9,1.2,Iris-versicolor 84 | 6.0,2.7,5.1,1.6,Iris-versicolor 85 | 5.4,3.0,4.5,1.5,Iris-versicolor 86 | 6.0,3.4,4.5,1.6,Iris-versicolor 87 | 6.7,3.1,4.7,1.5,Iris-versicolor 88 | 6.3,2.3,4.4,1.3,Iris-versicolor 89 | 5.6,3.0,4.1,1.3,Iris-versicolor 90 | 5.5,2.5,4.0,1.3,Iris-versicolor 91 | 5.5,2.6,4.4,1.2,Iris-versicolor 92 | 6.1,3.0,4.6,1.4,Iris-versicolor 93 | 5.8,2.6,4.0,1.2,Iris-versicolor 94 | 5.0,2.3,3.3,1.0,Iris-versicolor 95 | 5.6,2.7,4.2,1.3,Iris-versicolor 96 | 5.7,3.0,4.2,1.2,Iris-versicolor 97 | 5.7,2.9,4.2,1.3,Iris-versicolor 98 | 6.2,2.9,4.3,1.3,Iris-versicolor 99 | 5.1,2.5,3.0,1.1,Iris-versicolor 100 | 5.7,2.8,4.1,1.3,Iris-versicolor 101 | 6.3,3.3,6.0,2.5,Iris-virginica 102 | 5.8,2.7,5.1,1.9,Iris-virginica 103 | 7.1,3.0,5.9,2.1,Iris-virginica 104 | 6.3,2.9,5.6,1.8,Iris-virginica 105 | 6.5,3.0,5.8,2.2,Iris-virginica 106 | 7.6,3.0,6.6,2.1,Iris-virginica 107 | 4.9,2.5,4.5,1.7,Iris-virginica 108 | 7.3,2.9,6.3,1.8,Iris-virginica 109 | 6.7,2.5,5.8,1.8,Iris-virginica 110 | 7.2,3.6,6.1,2.5,Iris-virginica 111 | 6.5,3.2,5.1,2.0,Iris-virginica 112 | 6.4,2.7,5.3,1.9,Iris-virginica 113 | 6.8,3.0,5.5,2.1,Iris-virginica 114 | 5.7,2.5,5.0,2.0,Iris-virginica 115 | 5.8,2.8,5.1,2.4,Iris-virginica 116 | 6.4,3.2,5.3,2.3,Iris-virginica 117 | 6.5,3.0,5.5,1.8,Iris-virginica 118 | 7.7,3.8,6.7,2.2,Iris-virginica 119 | 7.7,2.6,6.9,2.3,Iris-virginica 120 | 6.0,2.2,5.0,1.5,Iris-virginica 121 | 6.9,3.2,5.7,2.3,Iris-virginica 122 | 5.6,2.8,4.9,2.0,Iris-virginica 123 | 7.7,2.8,6.7,2.0,Iris-virginica 124 | 6.3,2.7,4.9,1.8,Iris-virginica 125 | 6.7,3.3,5.7,2.1,Iris-virginica 126 | 7.2,3.2,6.0,1.8,Iris-virginica 127 | 6.2,2.8,4.8,1.8,Iris-virginica 128 | 6.1,3.0,4.9,1.8,Iris-virginica 129 | 6.4,2.8,5.6,2.1,Iris-virginica 130 | 7.2,3.0,5.8,1.6,Iris-virginica 131 | 7.4,2.8,6.1,1.9,Iris-virginica 132 | 7.9,3.8,6.4,2.0,Iris-virginica 133 | 6.4,2.8,5.6,2.2,Iris-virginica 134 | 6.3,2.8,5.1,1.5,Iris-virginica 135 | 6.1,2.6,5.6,1.4,Iris-virginica 136 | 7.7,3.0,6.1,2.3,Iris-virginica 137 | 6.3,3.4,5.6,2.4,Iris-virginica 138 | 6.4,3.1,5.5,1.8,Iris-virginica 139 | 6.0,3.0,4.8,1.8,Iris-virginica 140 | 6.9,3.1,5.4,2.1,Iris-virginica 141 | 6.7,3.1,5.6,2.4,Iris-virginica 142 | 6.9,3.1,5.1,2.3,Iris-virginica 143 | 5.8,2.7,5.1,1.9,Iris-virginica 144 | 6.8,3.2,5.9,2.3,Iris-virginica 145 | 6.7,3.3,5.7,2.5,Iris-virginica 146 | 6.7,3.0,5.2,2.3,Iris-virginica 147 | 6.3,2.5,5.0,1.9,Iris-virginica 148 | 6.5,3.0,5.2,2.0,Iris-virginica 149 | 6.2,3.4,5.4,2.3,Iris-virginica 150 | 5.9,3.0,5.1,1.8,Iris-virginica 151 | 152 | -------------------------------------------------------------------------------- /Chapter07/07. 縦型データフレームと横型データフレームを変換する/iris.csv: -------------------------------------------------------------------------------- 1 | 5.1,3.5,1.4,0.2,Iris-setosa 2 | 4.9,3.0,1.4,0.2,Iris-setosa 3 | 4.7,3.2,1.3,0.2,Iris-setosa 4 | 4.6,3.1,1.5,0.2,Iris-setosa 5 | 5.0,3.6,1.4,0.2,Iris-setosa 6 | 5.4,3.9,1.7,0.4,Iris-setosa 7 | 4.6,3.4,1.4,0.3,Iris-setosa 8 | 5.0,3.4,1.5,0.2,Iris-setosa 9 | 4.4,2.9,1.4,0.2,Iris-setosa 10 | 4.9,3.1,1.5,0.1,Iris-setosa 11 | 5.4,3.7,1.5,0.2,Iris-setosa 12 | 4.8,3.4,1.6,0.2,Iris-setosa 13 | 4.8,3.0,1.4,0.1,Iris-setosa 14 | 4.3,3.0,1.1,0.1,Iris-setosa 15 | 5.8,4.0,1.2,0.2,Iris-setosa 16 | 5.7,4.4,1.5,0.4,Iris-setosa 17 | 5.4,3.9,1.3,0.4,Iris-setosa 18 | 5.1,3.5,1.4,0.3,Iris-setosa 19 | 5.7,3.8,1.7,0.3,Iris-setosa 20 | 5.1,3.8,1.5,0.3,Iris-setosa 21 | 5.4,3.4,1.7,0.2,Iris-setosa 22 | 5.1,3.7,1.5,0.4,Iris-setosa 23 | 4.6,3.6,1.0,0.2,Iris-setosa 24 | 5.1,3.3,1.7,0.5,Iris-setosa 25 | 4.8,3.4,1.9,0.2,Iris-setosa 26 | 5.0,3.0,1.6,0.2,Iris-setosa 27 | 5.0,3.4,1.6,0.4,Iris-setosa 28 | 5.2,3.5,1.5,0.2,Iris-setosa 29 | 5.2,3.4,1.4,0.2,Iris-setosa 30 | 4.7,3.2,1.6,0.2,Iris-setosa 31 | 4.8,3.1,1.6,0.2,Iris-setosa 32 | 5.4,3.4,1.5,0.4,Iris-setosa 33 | 5.2,4.1,1.5,0.1,Iris-setosa 34 | 5.5,4.2,1.4,0.2,Iris-setosa 35 | 4.9,3.1,1.5,0.1,Iris-setosa 36 | 5.0,3.2,1.2,0.2,Iris-setosa 37 | 5.5,3.5,1.3,0.2,Iris-setosa 38 | 4.9,3.1,1.5,0.1,Iris-setosa 39 | 4.4,3.0,1.3,0.2,Iris-setosa 40 | 5.1,3.4,1.5,0.2,Iris-setosa 41 | 5.0,3.5,1.3,0.3,Iris-setosa 42 | 4.5,2.3,1.3,0.3,Iris-setosa 43 | 4.4,3.2,1.3,0.2,Iris-setosa 44 | 5.0,3.5,1.6,0.6,Iris-setosa 45 | 5.1,3.8,1.9,0.4,Iris-setosa 46 | 4.8,3.0,1.4,0.3,Iris-setosa 47 | 5.1,3.8,1.6,0.2,Iris-setosa 48 | 4.6,3.2,1.4,0.2,Iris-setosa 49 | 5.3,3.7,1.5,0.2,Iris-setosa 50 | 5.0,3.3,1.4,0.2,Iris-setosa 51 | 7.0,3.2,4.7,1.4,Iris-versicolor 52 | 6.4,3.2,4.5,1.5,Iris-versicolor 53 | 6.9,3.1,4.9,1.5,Iris-versicolor 54 | 5.5,2.3,4.0,1.3,Iris-versicolor 55 | 6.5,2.8,4.6,1.5,Iris-versicolor 56 | 5.7,2.8,4.5,1.3,Iris-versicolor 57 | 6.3,3.3,4.7,1.6,Iris-versicolor 58 | 4.9,2.4,3.3,1.0,Iris-versicolor 59 | 6.6,2.9,4.6,1.3,Iris-versicolor 60 | 5.2,2.7,3.9,1.4,Iris-versicolor 61 | 5.0,2.0,3.5,1.0,Iris-versicolor 62 | 5.9,3.0,4.2,1.5,Iris-versicolor 63 | 6.0,2.2,4.0,1.0,Iris-versicolor 64 | 6.1,2.9,4.7,1.4,Iris-versicolor 65 | 5.6,2.9,3.6,1.3,Iris-versicolor 66 | 6.7,3.1,4.4,1.4,Iris-versicolor 67 | 5.6,3.0,4.5,1.5,Iris-versicolor 68 | 5.8,2.7,4.1,1.0,Iris-versicolor 69 | 6.2,2.2,4.5,1.5,Iris-versicolor 70 | 5.6,2.5,3.9,1.1,Iris-versicolor 71 | 5.9,3.2,4.8,1.8,Iris-versicolor 72 | 6.1,2.8,4.0,1.3,Iris-versicolor 73 | 6.3,2.5,4.9,1.5,Iris-versicolor 74 | 6.1,2.8,4.7,1.2,Iris-versicolor 75 | 6.4,2.9,4.3,1.3,Iris-versicolor 76 | 6.6,3.0,4.4,1.4,Iris-versicolor 77 | 6.8,2.8,4.8,1.4,Iris-versicolor 78 | 6.7,3.0,5.0,1.7,Iris-versicolor 79 | 6.0,2.9,4.5,1.5,Iris-versicolor 80 | 5.7,2.6,3.5,1.0,Iris-versicolor 81 | 5.5,2.4,3.8,1.1,Iris-versicolor 82 | 5.5,2.4,3.7,1.0,Iris-versicolor 83 | 5.8,2.7,3.9,1.2,Iris-versicolor 84 | 6.0,2.7,5.1,1.6,Iris-versicolor 85 | 5.4,3.0,4.5,1.5,Iris-versicolor 86 | 6.0,3.4,4.5,1.6,Iris-versicolor 87 | 6.7,3.1,4.7,1.5,Iris-versicolor 88 | 6.3,2.3,4.4,1.3,Iris-versicolor 89 | 5.6,3.0,4.1,1.3,Iris-versicolor 90 | 5.5,2.5,4.0,1.3,Iris-versicolor 91 | 5.5,2.6,4.4,1.2,Iris-versicolor 92 | 6.1,3.0,4.6,1.4,Iris-versicolor 93 | 5.8,2.6,4.0,1.2,Iris-versicolor 94 | 5.0,2.3,3.3,1.0,Iris-versicolor 95 | 5.6,2.7,4.2,1.3,Iris-versicolor 96 | 5.7,3.0,4.2,1.2,Iris-versicolor 97 | 5.7,2.9,4.2,1.3,Iris-versicolor 98 | 6.2,2.9,4.3,1.3,Iris-versicolor 99 | 5.1,2.5,3.0,1.1,Iris-versicolor 100 | 5.7,2.8,4.1,1.3,Iris-versicolor 101 | 6.3,3.3,6.0,2.5,Iris-virginica 102 | 5.8,2.7,5.1,1.9,Iris-virginica 103 | 7.1,3.0,5.9,2.1,Iris-virginica 104 | 6.3,2.9,5.6,1.8,Iris-virginica 105 | 6.5,3.0,5.8,2.2,Iris-virginica 106 | 7.6,3.0,6.6,2.1,Iris-virginica 107 | 4.9,2.5,4.5,1.7,Iris-virginica 108 | 7.3,2.9,6.3,1.8,Iris-virginica 109 | 6.7,2.5,5.8,1.8,Iris-virginica 110 | 7.2,3.6,6.1,2.5,Iris-virginica 111 | 6.5,3.2,5.1,2.0,Iris-virginica 112 | 6.4,2.7,5.3,1.9,Iris-virginica 113 | 6.8,3.0,5.5,2.1,Iris-virginica 114 | 5.7,2.5,5.0,2.0,Iris-virginica 115 | 5.8,2.8,5.1,2.4,Iris-virginica 116 | 6.4,3.2,5.3,2.3,Iris-virginica 117 | 6.5,3.0,5.5,1.8,Iris-virginica 118 | 7.7,3.8,6.7,2.2,Iris-virginica 119 | 7.7,2.6,6.9,2.3,Iris-virginica 120 | 6.0,2.2,5.0,1.5,Iris-virginica 121 | 6.9,3.2,5.7,2.3,Iris-virginica 122 | 5.6,2.8,4.9,2.0,Iris-virginica 123 | 7.7,2.8,6.7,2.0,Iris-virginica 124 | 6.3,2.7,4.9,1.8,Iris-virginica 125 | 6.7,3.3,5.7,2.1,Iris-virginica 126 | 7.2,3.2,6.0,1.8,Iris-virginica 127 | 6.2,2.8,4.8,1.8,Iris-virginica 128 | 6.1,3.0,4.9,1.8,Iris-virginica 129 | 6.4,2.8,5.6,2.1,Iris-virginica 130 | 7.2,3.0,5.8,1.6,Iris-virginica 131 | 7.4,2.8,6.1,1.9,Iris-virginica 132 | 7.9,3.8,6.4,2.0,Iris-virginica 133 | 6.4,2.8,5.6,2.2,Iris-virginica 134 | 6.3,2.8,5.1,1.5,Iris-virginica 135 | 6.1,2.6,5.6,1.4,Iris-virginica 136 | 7.7,3.0,6.1,2.3,Iris-virginica 137 | 6.3,3.4,5.6,2.4,Iris-virginica 138 | 6.4,3.1,5.5,1.8,Iris-virginica 139 | 6.0,3.0,4.8,1.8,Iris-virginica 140 | 6.9,3.1,5.4,2.1,Iris-virginica 141 | 6.7,3.1,5.6,2.4,Iris-virginica 142 | 6.9,3.1,5.1,2.3,Iris-virginica 143 | 5.8,2.7,5.1,1.9,Iris-virginica 144 | 6.8,3.2,5.9,2.3,Iris-virginica 145 | 6.7,3.3,5.7,2.5,Iris-virginica 146 | 6.7,3.0,5.2,2.3,Iris-virginica 147 | 6.3,2.5,5.0,1.9,Iris-virginica 148 | 6.5,3.0,5.2,2.0,Iris-virginica 149 | 6.2,3.4,5.4,2.3,Iris-virginica 150 | 5.9,3.0,5.1,1.8,Iris-virginica 151 | 152 | -------------------------------------------------------------------------------- /Chapter07/10. データフレーム変換を繰り返してピボットテーブルを作成する/iris.csv: -------------------------------------------------------------------------------- 1 | 5.1,3.5,1.4,0.2,Iris-setosa 2 | 4.9,3.0,1.4,0.2,Iris-setosa 3 | 4.7,3.2,1.3,0.2,Iris-setosa 4 | 4.6,3.1,1.5,0.2,Iris-setosa 5 | 5.0,3.6,1.4,0.2,Iris-setosa 6 | 5.4,3.9,1.7,0.4,Iris-setosa 7 | 4.6,3.4,1.4,0.3,Iris-setosa 8 | 5.0,3.4,1.5,0.2,Iris-setosa 9 | 4.4,2.9,1.4,0.2,Iris-setosa 10 | 4.9,3.1,1.5,0.1,Iris-setosa 11 | 5.4,3.7,1.5,0.2,Iris-setosa 12 | 4.8,3.4,1.6,0.2,Iris-setosa 13 | 4.8,3.0,1.4,0.1,Iris-setosa 14 | 4.3,3.0,1.1,0.1,Iris-setosa 15 | 5.8,4.0,1.2,0.2,Iris-setosa 16 | 5.7,4.4,1.5,0.4,Iris-setosa 17 | 5.4,3.9,1.3,0.4,Iris-setosa 18 | 5.1,3.5,1.4,0.3,Iris-setosa 19 | 5.7,3.8,1.7,0.3,Iris-setosa 20 | 5.1,3.8,1.5,0.3,Iris-setosa 21 | 5.4,3.4,1.7,0.2,Iris-setosa 22 | 5.1,3.7,1.5,0.4,Iris-setosa 23 | 4.6,3.6,1.0,0.2,Iris-setosa 24 | 5.1,3.3,1.7,0.5,Iris-setosa 25 | 4.8,3.4,1.9,0.2,Iris-setosa 26 | 5.0,3.0,1.6,0.2,Iris-setosa 27 | 5.0,3.4,1.6,0.4,Iris-setosa 28 | 5.2,3.5,1.5,0.2,Iris-setosa 29 | 5.2,3.4,1.4,0.2,Iris-setosa 30 | 4.7,3.2,1.6,0.2,Iris-setosa 31 | 4.8,3.1,1.6,0.2,Iris-setosa 32 | 5.4,3.4,1.5,0.4,Iris-setosa 33 | 5.2,4.1,1.5,0.1,Iris-setosa 34 | 5.5,4.2,1.4,0.2,Iris-setosa 35 | 4.9,3.1,1.5,0.1,Iris-setosa 36 | 5.0,3.2,1.2,0.2,Iris-setosa 37 | 5.5,3.5,1.3,0.2,Iris-setosa 38 | 4.9,3.1,1.5,0.1,Iris-setosa 39 | 4.4,3.0,1.3,0.2,Iris-setosa 40 | 5.1,3.4,1.5,0.2,Iris-setosa 41 | 5.0,3.5,1.3,0.3,Iris-setosa 42 | 4.5,2.3,1.3,0.3,Iris-setosa 43 | 4.4,3.2,1.3,0.2,Iris-setosa 44 | 5.0,3.5,1.6,0.6,Iris-setosa 45 | 5.1,3.8,1.9,0.4,Iris-setosa 46 | 4.8,3.0,1.4,0.3,Iris-setosa 47 | 5.1,3.8,1.6,0.2,Iris-setosa 48 | 4.6,3.2,1.4,0.2,Iris-setosa 49 | 5.3,3.7,1.5,0.2,Iris-setosa 50 | 5.0,3.3,1.4,0.2,Iris-setosa 51 | 7.0,3.2,4.7,1.4,Iris-versicolor 52 | 6.4,3.2,4.5,1.5,Iris-versicolor 53 | 6.9,3.1,4.9,1.5,Iris-versicolor 54 | 5.5,2.3,4.0,1.3,Iris-versicolor 55 | 6.5,2.8,4.6,1.5,Iris-versicolor 56 | 5.7,2.8,4.5,1.3,Iris-versicolor 57 | 6.3,3.3,4.7,1.6,Iris-versicolor 58 | 4.9,2.4,3.3,1.0,Iris-versicolor 59 | 6.6,2.9,4.6,1.3,Iris-versicolor 60 | 5.2,2.7,3.9,1.4,Iris-versicolor 61 | 5.0,2.0,3.5,1.0,Iris-versicolor 62 | 5.9,3.0,4.2,1.5,Iris-versicolor 63 | 6.0,2.2,4.0,1.0,Iris-versicolor 64 | 6.1,2.9,4.7,1.4,Iris-versicolor 65 | 5.6,2.9,3.6,1.3,Iris-versicolor 66 | 6.7,3.1,4.4,1.4,Iris-versicolor 67 | 5.6,3.0,4.5,1.5,Iris-versicolor 68 | 5.8,2.7,4.1,1.0,Iris-versicolor 69 | 6.2,2.2,4.5,1.5,Iris-versicolor 70 | 5.6,2.5,3.9,1.1,Iris-versicolor 71 | 5.9,3.2,4.8,1.8,Iris-versicolor 72 | 6.1,2.8,4.0,1.3,Iris-versicolor 73 | 6.3,2.5,4.9,1.5,Iris-versicolor 74 | 6.1,2.8,4.7,1.2,Iris-versicolor 75 | 6.4,2.9,4.3,1.3,Iris-versicolor 76 | 6.6,3.0,4.4,1.4,Iris-versicolor 77 | 6.8,2.8,4.8,1.4,Iris-versicolor 78 | 6.7,3.0,5.0,1.7,Iris-versicolor 79 | 6.0,2.9,4.5,1.5,Iris-versicolor 80 | 5.7,2.6,3.5,1.0,Iris-versicolor 81 | 5.5,2.4,3.8,1.1,Iris-versicolor 82 | 5.5,2.4,3.7,1.0,Iris-versicolor 83 | 5.8,2.7,3.9,1.2,Iris-versicolor 84 | 6.0,2.7,5.1,1.6,Iris-versicolor 85 | 5.4,3.0,4.5,1.5,Iris-versicolor 86 | 6.0,3.4,4.5,1.6,Iris-versicolor 87 | 6.7,3.1,4.7,1.5,Iris-versicolor 88 | 6.3,2.3,4.4,1.3,Iris-versicolor 89 | 5.6,3.0,4.1,1.3,Iris-versicolor 90 | 5.5,2.5,4.0,1.3,Iris-versicolor 91 | 5.5,2.6,4.4,1.2,Iris-versicolor 92 | 6.1,3.0,4.6,1.4,Iris-versicolor 93 | 5.8,2.6,4.0,1.2,Iris-versicolor 94 | 5.0,2.3,3.3,1.0,Iris-versicolor 95 | 5.6,2.7,4.2,1.3,Iris-versicolor 96 | 5.7,3.0,4.2,1.2,Iris-versicolor 97 | 5.7,2.9,4.2,1.3,Iris-versicolor 98 | 6.2,2.9,4.3,1.3,Iris-versicolor 99 | 5.1,2.5,3.0,1.1,Iris-versicolor 100 | 5.7,2.8,4.1,1.3,Iris-versicolor 101 | 6.3,3.3,6.0,2.5,Iris-virginica 102 | 5.8,2.7,5.1,1.9,Iris-virginica 103 | 7.1,3.0,5.9,2.1,Iris-virginica 104 | 6.3,2.9,5.6,1.8,Iris-virginica 105 | 6.5,3.0,5.8,2.2,Iris-virginica 106 | 7.6,3.0,6.6,2.1,Iris-virginica 107 | 4.9,2.5,4.5,1.7,Iris-virginica 108 | 7.3,2.9,6.3,1.8,Iris-virginica 109 | 6.7,2.5,5.8,1.8,Iris-virginica 110 | 7.2,3.6,6.1,2.5,Iris-virginica 111 | 6.5,3.2,5.1,2.0,Iris-virginica 112 | 6.4,2.7,5.3,1.9,Iris-virginica 113 | 6.8,3.0,5.5,2.1,Iris-virginica 114 | 5.7,2.5,5.0,2.0,Iris-virginica 115 | 5.8,2.8,5.1,2.4,Iris-virginica 116 | 6.4,3.2,5.3,2.3,Iris-virginica 117 | 6.5,3.0,5.5,1.8,Iris-virginica 118 | 7.7,3.8,6.7,2.2,Iris-virginica 119 | 7.7,2.6,6.9,2.3,Iris-virginica 120 | 6.0,2.2,5.0,1.5,Iris-virginica 121 | 6.9,3.2,5.7,2.3,Iris-virginica 122 | 5.6,2.8,4.9,2.0,Iris-virginica 123 | 7.7,2.8,6.7,2.0,Iris-virginica 124 | 6.3,2.7,4.9,1.8,Iris-virginica 125 | 6.7,3.3,5.7,2.1,Iris-virginica 126 | 7.2,3.2,6.0,1.8,Iris-virginica 127 | 6.2,2.8,4.8,1.8,Iris-virginica 128 | 6.1,3.0,4.9,1.8,Iris-virginica 129 | 6.4,2.8,5.6,2.1,Iris-virginica 130 | 7.2,3.0,5.8,1.6,Iris-virginica 131 | 7.4,2.8,6.1,1.9,Iris-virginica 132 | 7.9,3.8,6.4,2.0,Iris-virginica 133 | 6.4,2.8,5.6,2.2,Iris-virginica 134 | 6.3,2.8,5.1,1.5,Iris-virginica 135 | 6.1,2.6,5.6,1.4,Iris-virginica 136 | 7.7,3.0,6.1,2.3,Iris-virginica 137 | 6.3,3.4,5.6,2.4,Iris-virginica 138 | 6.4,3.1,5.5,1.8,Iris-virginica 139 | 6.0,3.0,4.8,1.8,Iris-virginica 140 | 6.9,3.1,5.4,2.1,Iris-virginica 141 | 6.7,3.1,5.6,2.4,Iris-virginica 142 | 6.9,3.1,5.1,2.3,Iris-virginica 143 | 5.8,2.7,5.1,1.9,Iris-virginica 144 | 6.8,3.2,5.9,2.3,Iris-virginica 145 | 6.7,3.3,5.7,2.5,Iris-virginica 146 | 6.7,3.0,5.2,2.3,Iris-virginica 147 | 6.3,2.5,5.0,1.9,Iris-virginica 148 | 6.5,3.0,5.2,2.0,Iris-virginica 149 | 6.2,3.4,5.4,2.3,Iris-virginica 150 | 5.9,3.0,5.1,1.8,Iris-virginica 151 | 152 | -------------------------------------------------------------------------------- /Chapter02/02. 行列乗算を高速に行う/2.2 行列乗算を高速に行う.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "domul (generic function with 1 method)" 12 | ] 13 | }, 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "# 一連の行列を引数としてとり、最適な順番で乗算する\n", 21 | "function fastmatmul(args::AbstractMatrix...)\n", 22 | " length(args) ≤ 1 && return *(args...)\n", 23 | " sizes = size.(args)\n", 24 | " if !all(sizes[i][2] == sizes[i+1][1] for i in 1:length(sizes)-1)\n", 25 | " throw(ArgumentError(\"matrix dimensions mismatch\"))\n", 26 | " end\n", 27 | " partcost = Dict{Tuple{Int,Int}, Tuple{Int, Int}}()\n", 28 | " from, to = 1, length(sizes)\n", 29 | " solvemul(sizes, partcost, from, to)\n", 30 | " domul(args, partcost, from, to)\n", 31 | "end\n", 32 | "\n", 33 | "# 最適な乗算順序を見つける\n", 34 | "function solvemul(sizes, partcost, from, to)\n", 35 | " if from == to\n", 36 | " partcost[(from, to)] = (0, from)\n", 37 | " return\n", 38 | " end\n", 39 | " mincost = typemax(Int)\n", 40 | " minj = -1\n", 41 | " for j in from:to-1\n", 42 | " haskey(partcost, (from, j)) || solvemul(sizes, partcost, from, j)\n", 43 | " haskey(partcost, (j+1, to)) || solvemul(sizes, partcost, j+1, to)\n", 44 | " curcost = sizes[from][1]*sizes[j][2]*sizes[to][2] +\n", 45 | " partcost[(from, j)][1] + partcost[(j+1, to)][1]\n", 46 | " if curcost < mincost\n", 47 | " minj = j\n", 48 | " mincost = curcost\n", 49 | " end\n", 50 | " end\n", 51 | " partcost[(from, to)] = (mincost, minj)\n", 52 | "end\n", 53 | "\n", 54 | "# 事前に計算した最適な順番で乗算を行う\n", 55 | "function domul(args, partcost, from, to)\n", 56 | " from == to && return args[from]\n", 57 | " from+1 == to && return args[from]*args[to]\n", 58 | " j = partcost[(from, to)][2]\n", 59 | " domul(args, partcost, from, j) * domul(args, partcost, j+1, to)\n", 60 | "end\n" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 2, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stderr", 70 | "output_type": "stream", 71 | "text": [ 72 | "┌ Info: Recompiling stale cache file /home/nakada/.julia/compiled/v1.2/BenchmarkTools/ZXPQo.ji for BenchmarkTools [6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf]\n", 73 | "└ @ Base loading.jl:1240\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "using BenchmarkTools\n", 79 | "A = ones(5, 5000);\n", 80 | "B = ones(5000, 5);" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 3, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | " 635.300 μs (33 allocations: 1.72 MiB)\n" 93 | ] 94 | } 95 | ], 96 | "source": [ 97 | "@btime *(repeat([A,B], outer=10)...);" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 4, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | " 401.600 μs (43 allocations: 52.25 KiB)\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "@btime fastmatmul(repeat([A,B], outer=10)...);" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 5, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "data": { 124 | "text/plain": [ 125 | "@fastmatmul (macro with 1 method)" 126 | ] 127 | }, 128 | "execution_count": 5, 129 | "metadata": {}, 130 | "output_type": "execute_result" 131 | } 132 | ], 133 | "source": [ 134 | "macro fastmatmul(ex::Expr)\n", 135 | " ex.head == :call || throw(ArgumentError(\"expression must be a call\"))\n", 136 | " ex.args[1] == :(*) || throw(ArgumentError(\"only multiplication is allowed\"))\n", 137 | " new_ex = deepcopy(ex)\n", 138 | " new_ex.args[1] = :fastmatmul\n", 139 | " esc(new_ex)\n", 140 | "end\n" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 6, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "2×5 Array{Float64,2}:\n", 152 | " 12.0 12.0 12.0 12.0 12.0\n", 153 | " 12.0 12.0 12.0 12.0 12.0" 154 | ] 155 | }, 156 | "execution_count": 6, 157 | "metadata": {}, 158 | "output_type": "execute_result" 159 | } 160 | ], 161 | "source": [ 162 | "@fastmatmul ones(2,3)*ones(3,4)*ones(4,5)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": null, 168 | "metadata": {}, 169 | "outputs": [], 170 | "source": [] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": null, 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [] 178 | } 179 | ], 180 | "metadata": { 181 | "@webio": { 182 | "lastCommId": null, 183 | "lastKernelId": null 184 | }, 185 | "kernelspec": { 186 | "display_name": "Julia 1.2.0", 187 | "language": "julia", 188 | "name": "julia-1.2" 189 | }, 190 | "language_info": { 191 | "file_extension": ".jl", 192 | "mimetype": "application/julia", 193 | "name": "julia", 194 | "version": "1.2.0" 195 | } 196 | }, 197 | "nbformat": 4, 198 | "nbformat_minor": 2 199 | } 200 | -------------------------------------------------------------------------------- /Chapter09/03. 最尤推定を行う/9.3 最尤推定を行う.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m registry at `~/.julia/registries/General`\n", 13 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m git-repo `https://github.com/JuliaRegistries/General.git`\n", 14 | "\u001b[?25l\u001b[2K\u001b[?25h\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n", 15 | "\u001b[32m\u001b[1m Installed\u001b[22m\u001b[39m ArrayInterface ───────── v1.2.1\n", 16 | "\u001b[32m\u001b[1m Installed\u001b[22m\u001b[39m RecursiveArrayTools ──── v0.18.6\n", 17 | "\u001b[32m\u001b[1m Installed\u001b[22m\u001b[39m DiffEqDiffTools ──────── v1.3.0\n", 18 | "\u001b[32m\u001b[1m Installed\u001b[22m\u001b[39m NLSolversBase ────────── v7.4.1\n", 19 | "\u001b[32m\u001b[1m Installed\u001b[22m\u001b[39m Parameters ───────────── v0.10.3\n", 20 | "\u001b[32m\u001b[1m Installed\u001b[22m\u001b[39m FillArrays ───────────── v0.6.4\n", 21 | "\u001b[32m\u001b[1m Installed\u001b[22m\u001b[39m Optim ────────────────── v0.19.2\n", 22 | "\u001b[32m\u001b[1m Installed\u001b[22m\u001b[39m LineSearches ─────────── v7.0.1\n", 23 | "\u001b[32m\u001b[1m Installed\u001b[22m\u001b[39m PositiveFactorizations ─ v0.2.2\n", 24 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/environments/v1.2/Project.toml`\n", 25 | " \u001b[90m [429524aa]\u001b[39m\u001b[92m + Optim v0.19.2\u001b[39m\n", 26 | "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/environments/v1.2/Manifest.toml`\n", 27 | " \u001b[90m [4fba245c]\u001b[39m\u001b[93m ↑ ArrayInterface v0.1.1 ⇒ v1.2.1\u001b[39m\n", 28 | " \u001b[90m [01453d9d]\u001b[39m\u001b[92m + DiffEqDiffTools v1.3.0\u001b[39m\n", 29 | " \u001b[90m [1a297f60]\u001b[39m\u001b[92m + FillArrays v0.6.4\u001b[39m\n", 30 | " \u001b[90m [d3d80556]\u001b[39m\u001b[92m + LineSearches v7.0.1\u001b[39m\n", 31 | " \u001b[90m [d41bc354]\u001b[39m\u001b[92m + NLSolversBase v7.4.1\u001b[39m\n", 32 | " \u001b[90m [429524aa]\u001b[39m\u001b[92m + Optim v0.19.2\u001b[39m\n", 33 | " \u001b[90m [d96e819e]\u001b[39m\u001b[95m ↓ Parameters v0.11.0 ⇒ v0.10.3\u001b[39m\n", 34 | " \u001b[90m [85a6dd25]\u001b[39m\u001b[92m + PositiveFactorizations v0.2.2\u001b[39m\n", 35 | " \u001b[90m [731186ca]\u001b[39m\u001b[95m ↓ RecursiveArrayTools v0.20.0 ⇒ v0.18.6\u001b[39m\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "#using Pkg; Pkg.add(\"Optim\")" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 2, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "name": "stderr", 50 | "output_type": "stream", 51 | "text": [ 52 | "┌ Info: Precompiling Optim [429524aa-4258-5aef-a3af-852621145aeb]\n", 53 | "└ @ Base loading.jl:1242\n" 54 | ] 55 | }, 56 | { 57 | "data": { 58 | "text/plain": [ 59 | "testoptim (generic function with 1 method)" 60 | ] 61 | }, 62 | "execution_count": 2, 63 | "metadata": {}, 64 | "output_type": "execute_result" 65 | } 66 | ], 67 | "source": [ 68 | "using Optim\n", 69 | "using Distributions\n", 70 | "\n", 71 | "function loglik(x, μ, logσ)\n", 72 | " nd = Normal(μ, exp(logσ))\n", 73 | " -sum(logpdf(nd, v) for v in x)\n", 74 | "end\n", 75 | "\n", 76 | "function testoptim(x)\n", 77 | " res = optimize(par -> loglik(x, par[1], par[2]), zeros(2))\n", 78 | " display(res)\n", 79 | " res.minimizer[1], exp(res.minimizer[2])\n", 80 | "end" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 3, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "data": { 90 | "text/plain": [ 91 | " * Status: success\n", 92 | "\n", 93 | " * Candidate solution\n", 94 | " Minimizer: [-4.33e-02, 1.50e-02]\n", 95 | " Minimum: 1.433977e+02\n", 96 | "\n", 97 | " * Found with\n", 98 | " Algorithm: Nelder-Mead\n", 99 | " Initial Point: [0.00e+00, 0.00e+00]\n", 100 | "\n", 101 | " * Convergence measures\n", 102 | " √(Σ(yᵢ-ȳ)²)/n ≤ 1.0e-08\n", 103 | "\n", 104 | " * Work counters\n", 105 | " Iterations: 27\n", 106 | " f(x) calls: 56\n" 107 | ] 108 | }, 109 | "metadata": {}, 110 | "output_type": "display_data" 111 | }, 112 | { 113 | "data": { 114 | "text/plain": [ 115 | "(-0.043288979878934694, 1.0151538648696379)" 116 | ] 117 | }, 118 | "execution_count": 3, 119 | "metadata": {}, 120 | "output_type": "execute_result" 121 | } 122 | ], 123 | "source": [ 124 | "using Random\n", 125 | "Random.seed!(1)\n", 126 | "x = randn(100)\n", 127 | "testoptim(x)" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 4, 133 | "metadata": {}, 134 | "outputs": [ 135 | { 136 | "data": { 137 | "text/plain": [ 138 | "(-0.04329512736686613, 1.0151523821496027)" 139 | ] 140 | }, 141 | "execution_count": 4, 142 | "metadata": {}, 143 | "output_type": "execute_result" 144 | } 145 | ], 146 | "source": [ 147 | "mean(x), std(x)*sqrt(99/100)" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": null, 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [] 156 | } 157 | ], 158 | "metadata": { 159 | "@webio": { 160 | "lastCommId": null, 161 | "lastKernelId": null 162 | }, 163 | "kernelspec": { 164 | "display_name": "Julia 1.2.0", 165 | "language": "julia", 166 | "name": "julia-1.2" 167 | }, 168 | "language_info": { 169 | "file_extension": ".jl", 170 | "mimetype": "application/julia", 171 | "name": "julia", 172 | "version": "1.2.0" 173 | } 174 | }, 175 | "nbformat": 4, 176 | "nbformat_minor": 1 177 | } 178 | -------------------------------------------------------------------------------- /Chapter03/02. IOBufferを使って効率的なインメモリストリームを作る/3.02 IOBufferを使って効率的なインメモリストリームを作る.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "splitstring (generic function with 1 method)" 12 | ] 13 | }, 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "function splitstring(s::AbstractString)\n", 21 | " bufs = [IOBuffer() for i in 1:2]\n", 22 | " idx = 1\n", 23 | " for c in s\n", 24 | " write(bufs[idx], c)\n", 25 | " idx = 3 - idx\n", 26 | " end\n", 27 | " @. String(take!(bufs))\n", 28 | "end" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 2, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "data": { 38 | "text/plain": [ 39 | "\"1-2-3-4-5-6-7-8-9\"" 40 | ] 41 | }, 42 | "execution_count": 2, 43 | "metadata": {}, 44 | "output_type": "execute_result" 45 | } 46 | ], 47 | "source": [ 48 | "s = join('1':'9', \"-\")" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 3, 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "data": { 58 | "text/plain": [ 59 | "2-element Array{String,1}:\n", 60 | " \"123456789\"\n", 61 | " \"--------\" " 62 | ] 63 | }, 64 | "execution_count": 3, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "splitstring(s)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 4, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "splitstring1 (generic function with 1 method)" 82 | ] 83 | }, 84 | "execution_count": 4, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "function splitstring1(s::AbstractString)\n", 91 | " bufs = [Char[] for i in 1:2]\n", 92 | " idx = 1\n", 93 | " for c in s\n", 94 | " push!(bufs[idx], c)\n", 95 | " idx = 3 - idx\n", 96 | " end\n", 97 | " join.(bufs)\n", 98 | "end" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 5, 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "data": { 108 | "text/plain": [ 109 | "splitstring2 (generic function with 1 method)" 110 | ] 111 | }, 112 | "execution_count": 5, 113 | "metadata": {}, 114 | "output_type": "execute_result" 115 | } 116 | ], 117 | "source": [ 118 | "function splitstring2(s::AbstractString)\n", 119 | " bufs = [\"\" for i in 1:2]\n", 120 | " idx = 1\n", 121 | " for c in s\n", 122 | " bufs[idx] *= c\n", 123 | " idx = 3 - idx\n", 124 | " end\n", 125 | " bufs\n", 126 | "end" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 6, 132 | "metadata": {}, 133 | "outputs": [], 134 | "source": [ 135 | "using BenchmarkTools" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 7, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "data": { 145 | "text/plain": [ 146 | "BenchmarkTools.Trial: \n", 147 | " memory estimate: 704 bytes\n", 148 | " allocs estimate: 10\n", 149 | " --------------\n", 150 | " minimum time: 310.040 ns (0.00% GC)\n", 151 | " median time: 332.129 ns (0.00% GC)\n", 152 | " mean time: 412.910 ns (11.24% GC)\n", 153 | " maximum time: 157.963 μs (99.56% GC)\n", 154 | " --------------\n", 155 | " samples: 10000\n", 156 | " evals/sample: 249" 157 | ] 158 | }, 159 | "execution_count": 7, 160 | "metadata": {}, 161 | "output_type": "execute_result" 162 | } 163 | ], 164 | "source": [ 165 | "@benchmark splitstring($s)" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 8, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "data": { 175 | "text/plain": [ 176 | "BenchmarkTools.Trial: \n", 177 | " memory estimate: 1.00 KiB\n", 178 | " allocs estimate: 18\n", 179 | " --------------\n", 180 | " minimum time: 888.889 ns (0.00% GC)\n", 181 | " median time: 965.079 ns (0.00% GC)\n", 182 | " mean time: 1.205 μs (13.38% GC)\n", 183 | " maximum time: 668.351 μs (99.74% GC)\n", 184 | " --------------\n", 185 | " samples: 10000\n", 186 | " evals/sample: 63" 187 | ] 188 | }, 189 | "execution_count": 8, 190 | "metadata": {}, 191 | "output_type": "execute_result" 192 | } 193 | ], 194 | "source": [ 195 | "@benchmark splitstring1($s)" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 9, 201 | "metadata": {}, 202 | "outputs": [ 203 | { 204 | "data": { 205 | "text/plain": [ 206 | "BenchmarkTools.Trial: \n", 207 | " memory estimate: 1.16 KiB\n", 208 | " allocs estimate: 35\n", 209 | " --------------\n", 210 | " minimum time: 1.310 μs (0.00% GC)\n", 211 | " median time: 2.080 μs (0.00% GC)\n", 212 | " mean time: 2.693 μs (20.03% GC)\n", 213 | " maximum time: 3.940 ms (99.90% GC)\n", 214 | " --------------\n", 215 | " samples: 10000\n", 216 | " evals/sample: 10" 217 | ] 218 | }, 219 | "execution_count": 9, 220 | "metadata": {}, 221 | "output_type": "execute_result" 222 | } 223 | ], 224 | "source": [ 225 | "@benchmark splitstring2($s)" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": null, 231 | "metadata": {}, 232 | "outputs": [], 233 | "source": [] 234 | } 235 | ], 236 | "metadata": { 237 | "@webio": { 238 | "lastCommId": null, 239 | "lastKernelId": null 240 | }, 241 | "kernelspec": { 242 | "display_name": "Julia 1.2.0", 243 | "language": "julia", 244 | "name": "julia-1.2" 245 | }, 246 | "language_info": { 247 | "file_extension": ".jl", 248 | "mimetype": "application/julia", 249 | "name": "julia", 250 | "version": "1.2.0" 251 | } 252 | }, 253 | "nbformat": 4, 254 | "nbformat_minor": 2 255 | } 256 | -------------------------------------------------------------------------------- /Chapter04/13. 配列ビューを使って使用メモリ量を減らす/4.13 配列ビューを使って使用メモリ量を減らす.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "main (generic function with 1 method)" 12 | ] 13 | }, 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "blockvalid(x, v) = count(isequal(v), x) ≤ 1\n", 21 | "function backtrack!(x)\n", 22 | " pos = findfirst(isequal(0), x)\n", 23 | " isa(pos, Nothing) && return true\n", 24 | " iloc = 3div(pos[1]-1, 3) .+ (1:3)\n", 25 | " jloc = 3div(pos[2]-1, 3) .+ (1:3)\n", 26 | " for k in 1:9\n", 27 | " x[pos] = k\n", 28 | " blockvalid(view(x, pos[1], :), k) || continue\n", 29 | " blockvalid(view(x, :, pos[2]), k) || continue\n", 30 | " blockvalid(view(x, iloc, jloc), k) || continue\n", 31 | " backtrack!(x) && return true\n", 32 | " end\n", 33 | " x[pos] = 0\n", 34 | " return false\n", 35 | "end\n", 36 | "\n", 37 | "function ssolve(lines, i)\n", 38 | " t = [lines[10i-j][k] - '0' for j in 8:-1:0, k in 1:9]\n", 39 | " backtrack!(t)\n", 40 | " sum([100, 10, 1] .* t[1, 1:3])\n", 41 | "end\n", 42 | "\n", 43 | "function main() \n", 44 | " lines = readlines(\"p096_sudoku.txt\")\n", 45 | " @time sum(ssolve(lines, i) for i in 1:50)\n", 46 | " @time sum(ssolve(lines, i) for i in 1:50)\n", 47 | "end" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 2, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | " 0.126539 seconds (402 allocations: 60.984 KiB)\n", 60 | " 0.131216 seconds (402 allocations: 60.984 KiB)\n" 61 | ] 62 | }, 63 | { 64 | "data": { 65 | "text/plain": [ 66 | "24702" 67 | ] 68 | }, 69 | "execution_count": 2, 70 | "metadata": {}, 71 | "output_type": "execute_result" 72 | } 73 | ], 74 | "source": [ 75 | "main()" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 3, 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "data": { 85 | "text/plain": [ 86 | "backtrack! (generic function with 1 method)" 87 | ] 88 | }, 89 | "execution_count": 3, 90 | "metadata": {}, 91 | "output_type": "execute_result" 92 | } 93 | ], 94 | "source": [ 95 | "function backtrack!(x)\n", 96 | " pos = findfirst(isequal(0), x)\n", 97 | " isa(pos, Nothing) && return true\n", 98 | " iloc = 3div(pos[1]-1, 3) .+ (1:3)\n", 99 | " jloc = 3div(pos[2]-1, 3) .+ (1:3)\n", 100 | " for k in 1:9\n", 101 | " x[pos] = k\n", 102 | " #blockvalid(view(x, pos[1], :), k) || continue\n", 103 | " #blockvalid(view(x, :, pos[2]), k) || continue\n", 104 | " #blockvalid(view(x, iloc, jloc), k) || continue\n", 105 | " blockvalid(x[pos[1], :], k) || continue\n", 106 | " blockvalid(x[:, pos[2]], k) || continue\n", 107 | " blockvalid(x[iloc, jloc], k) || continue\n", 108 | " backtrack!(x) && return true\n", 109 | " end\n", 110 | " x[pos] = 0\n", 111 | " return false\n", 112 | "end" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 4, 118 | "metadata": {}, 119 | "outputs": [ 120 | { 121 | "name": "stdout", 122 | "output_type": "stream", 123 | "text": [ 124 | " 0.580746 seconds (10.03 M allocations: 1.494 GiB, 7.08% gc time)\n", 125 | " 0.558670 seconds (10.03 M allocations: 1.494 GiB, 6.61% gc time)\n" 126 | ] 127 | }, 128 | { 129 | "data": { 130 | "text/plain": [ 131 | "24702" 132 | ] 133 | }, 134 | "execution_count": 4, 135 | "metadata": {}, 136 | "output_type": "execute_result" 137 | } 138 | ], 139 | "source": [ 140 | "main()" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 5, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "ssolve (generic function with 1 method)" 152 | ] 153 | }, 154 | "execution_count": 5, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "function backtrack!(x, z, idx)\n", 161 | " idx > length(z) && return true\n", 162 | " pos = z[idx]\n", 163 | " iloc = 3div(pos[1]-1, 3)\n", 164 | " jloc = 3div(pos[2]-1, 3)\n", 165 | " filled = 0\n", 166 | " @inbounds for k in 1:9\n", 167 | " filled |= x[pos[1], k] | x[k, pos[2]]\n", 168 | " end\n", 169 | " @inbounds for k1 in 1:3, k2 in 1:3\n", 170 | " filled |= x[iloc+k1, jloc+k2]\n", 171 | " end\n", 172 | " @inbounds for i in 1:9\n", 173 | " k = 1< 0)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 5, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "data": { 64 | "text/plain": [ 65 | "possum1 (generic function with 1 method)" 66 | ] 67 | }, 68 | "execution_count": 5, 69 | "metadata": {}, 70 | "output_type": "execute_result" 71 | } 72 | ], 73 | "source": [ 74 | "function possum1(x)\n", 75 | " s = zero(eltype(x))\n", 76 | " for v in x\n", 77 | " if v > 0\n", 78 | " s += v \n", 79 | " end\n", 80 | " end\n", 81 | " s \n", 82 | "end" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 6, 88 | "metadata": {}, 89 | "outputs": [ 90 | { 91 | "name": "stdout", 92 | "output_type": "stream", 93 | "text": [ 94 | " 1.457 ms (1 allocation: 16 bytes)\n" 95 | ] 96 | }, 97 | { 98 | "data": { 99 | "text/plain": [ 100 | "398244.60749279766" 101 | ] 102 | }, 103 | "execution_count": 6, 104 | "metadata": {}, 105 | "output_type": "execute_result" 106 | } 107 | ], 108 | "source": [ 109 | "@btime possum1(x)" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 7, 115 | "metadata": {}, 116 | "outputs": [ 117 | { 118 | "data": { 119 | "text/plain": [ 120 | "possum2a (generic function with 1 method)" 121 | ] 122 | }, 123 | "execution_count": 7, 124 | "metadata": {}, 125 | "output_type": "execute_result" 126 | } 127 | ], 128 | "source": [ 129 | "function possum2a(x)\n", 130 | " s = zero(eltype(x))\n", 131 | " for v in x\n", 132 | " s += ifelse(v > 0, v, zero(s))\n", 133 | " end\n", 134 | " s \n", 135 | "end" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 8, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "name": "stdout", 145 | "output_type": "stream", 146 | "text": [ 147 | " 1.001 ms (1 allocation: 16 bytes)\n" 148 | ] 149 | }, 150 | { 151 | "data": { 152 | "text/plain": [ 153 | "398244.60749279766" 154 | ] 155 | }, 156 | "execution_count": 8, 157 | "metadata": {}, 158 | "output_type": "execute_result" 159 | } 160 | ], 161 | "source": [ 162 | "@btime possum2a(x)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 9, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "data": { 172 | "text/plain": [ 173 | "possum2b (generic function with 1 method)" 174 | ] 175 | }, 176 | "execution_count": 9, 177 | "metadata": {}, 178 | "output_type": "execute_result" 179 | } 180 | ], 181 | "source": [ 182 | "function possum2b(x)\n", 183 | " s = zero(eltype(x))\n", 184 | " @simd for v in x\n", 185 | " s += ifelse(v > 0, v, zero(s))\n", 186 | " end\n", 187 | " s\n", 188 | "end" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 10, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "name": "stdout", 198 | "output_type": "stream", 199 | "text": [ 200 | " 218.000 μs (1 allocation: 16 bytes)\n" 201 | ] 202 | }, 203 | { 204 | "data": { 205 | "text/plain": [ 206 | "398244.6074928036" 207 | ] 208 | }, 209 | "execution_count": 10, 210 | "metadata": {}, 211 | "output_type": "execute_result" 212 | } 213 | ], 214 | "source": [ 215 | "@btime possum2b(x)" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 11, 221 | "metadata": {}, 222 | "outputs": [ 223 | { 224 | "data": { 225 | "text/plain": [ 226 | "possum2c (generic function with 1 method)" 227 | ] 228 | }, 229 | "execution_count": 11, 230 | "metadata": {}, 231 | "output_type": "execute_result" 232 | } 233 | ], 234 | "source": [ 235 | "function possum2c(x) \n", 236 | " s = 0\n", 237 | " for v in x\n", 238 | " s += ifelse(v > 0, v, 0)\n", 239 | " end\n", 240 | " s\n", 241 | "end" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": null, 247 | "metadata": {}, 248 | "outputs": [], 249 | "source": [ 250 | "@btime possum2c(x)" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": null, 256 | "metadata": {}, 257 | "outputs": [], 258 | "source": [ 259 | "function possum2d(x::AbstractArray{T}) where T\n", 260 | " s = zero(T)\n", 261 | " @simd for v in x\n", 262 | " s += ifelse(v > 0, v, zero(T))\n", 263 | " end\n", 264 | " s \n", 265 | "end" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": null, 271 | "metadata": {}, 272 | "outputs": [], 273 | "source": [ 274 | "@btime possum2d(x)" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": null, 280 | "metadata": { 281 | "collapsed": true 282 | }, 283 | "outputs": [], 284 | "source": [] 285 | } 286 | ], 287 | "metadata": { 288 | "@webio": { 289 | "lastCommId": null, 290 | "lastKernelId": null 291 | }, 292 | "kernelspec": { 293 | "display_name": "Julia 1.2.0", 294 | "language": "julia", 295 | "name": "julia-1.2" 296 | }, 297 | "language_info": { 298 | "file_extension": ".jl", 299 | "mimetype": "application/julia", 300 | "name": "julia", 301 | "version": "1.2.0" 302 | } 303 | }, 304 | "nbformat": 4, 305 | "nbformat_minor": 1 306 | } 307 | -------------------------------------------------------------------------------- /Chapter03/11. CSVファイルとFWFファイルを読み込む/3.11 CSVファイルとFWFファイルを読み込む.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "\"a,b,c\\n11,2,3\\n4,555,6\\n7,8,9999\"" 12 | ] 13 | }, 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "csv = \"\"\"a,b,c\n", 21 | " 11,2,3\n", 22 | " 4,555,6\n", 23 | " 7,8,9999\"\"\"" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=29, maxsize=Inf, ptr=1, mark=-1)" 35 | ] 36 | }, 37 | "execution_count": 2, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "iocsv = IOBuffer(csv)" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 3, 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "data": { 53 | "text/plain": [ 54 | "\"a b c\\n11 2 3\\n4 555 66\\n7 8 9999\"" 55 | ] 56 | }, 57 | "execution_count": 3, 58 | "metadata": {}, 59 | "output_type": "execute_result" 60 | } 61 | ], 62 | "source": [ 63 | "fwf = \"\"\"a b c\n", 64 | " 11 2 3\n", 65 | " 4 555 66\n", 66 | " 7 8 9999\"\"\"" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 4, 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "data": { 76 | "text/plain": [ 77 | "IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=47, maxsize=Inf, ptr=1, mark=-1)" 78 | ] 79 | }, 80 | "execution_count": 4, 81 | "metadata": {}, 82 | "output_type": "execute_result" 83 | } 84 | ], 85 | "source": [ 86 | "iofwf = IOBuffer(fwf)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 5, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "using DelimitedFiles" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 6, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "([11.0 2.0 3.0; 4.0 555.0 6.0; 7.0 8.0 9999.0], AbstractString[\"a\" \"b\" \"c\"])" 107 | ] 108 | }, 109 | "execution_count": 6, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "datacsv, headercsv = readdlm(iocsv, ',', header=true)" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 7, 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "data": { 125 | "text/plain": [ 126 | "1×3 Array{AbstractString,2}:\n", 127 | " \"a\" \"b\" \"c\"" 128 | ] 129 | }, 130 | "execution_count": 7, 131 | "metadata": {}, 132 | "output_type": "execute_result" 133 | } 134 | ], 135 | "source": [ 136 | "headercsv" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 8, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "data": { 146 | "text/plain": [ 147 | "3×3 Array{Float64,2}:\n", 148 | " 11.0 2.0 3.0\n", 149 | " 4.0 555.0 6.0\n", 150 | " 7.0 8.0 9999.0" 151 | ] 152 | }, 153 | "execution_count": 8, 154 | "metadata": {}, 155 | "output_type": "execute_result" 156 | } 157 | ], 158 | "source": [ 159 | "datacsv" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 9, 165 | "metadata": {}, 166 | "outputs": [ 167 | { 168 | "data": { 169 | "text/plain": [ 170 | "getsubstring (generic function with 1 method)" 171 | ] 172 | }, 173 | "execution_count": 9, 174 | "metadata": {}, 175 | "output_type": "execute_result" 176 | } 177 | ], 178 | "source": [ 179 | "function getsubstring(s::AbstractString, charfrom::Int, charto::Int)\n", 180 | " SubString(s, nextind.(s, 0, (charfrom, charto))...)\n", 181 | "end" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 10, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "data": { 191 | "text/plain": [ 192 | "readfwf (generic function with 1 method)" 193 | ] 194 | }, 195 | "execution_count": 10, 196 | "metadata": {}, 197 | "output_type": "execute_result" 198 | } 199 | ], 200 | "source": [ 201 | "function readfwf(io, ranges::AbstractVector{<:Pair})\n", 202 | " datafwf = []\n", 203 | " starts = first.(ranges)\n", 204 | " ends = last.(ranges)\n", 205 | " while !eof(io)\n", 206 | " line = readline(io)\n", 207 | " push!(datafwf, getsubstring.(line, starts, ends))\n", 208 | " end\n", 209 | " [datafwf[i][j] for i in 1:length(datafwf), j in 1:length(ranges)]\n", 210 | "end" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 11, 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "data": { 220 | "text/plain": [ 221 | "4×3 Array{SubString{String},2}:\n", 222 | " \"a \" \"b \" \" c\"\n", 223 | " \"11\" \"2 \" \" 3\"\n", 224 | " \"4 \" \"555\" \" 66\"\n", 225 | " \"7 \" \"8 \" \"9999\"" 226 | ] 227 | }, 228 | "execution_count": 11, 229 | "metadata": {}, 230 | "output_type": "execute_result" 231 | } 232 | ], 233 | "source": [ 234 | "datafwf = readfwf(iofwf, [1=>2, 4=>6, 8=>11])" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 12, 240 | "metadata": {}, 241 | "outputs": [ 242 | { 243 | "data": { 244 | "text/plain": [ 245 | "3×3 Array{Int64,2}:\n", 246 | " 11 2 3\n", 247 | " 4 555 66\n", 248 | " 7 8 9999" 249 | ] 250 | }, 251 | "execution_count": 12, 252 | "metadata": {}, 253 | "output_type": "execute_result" 254 | } 255 | ], 256 | "source": [ 257 | "parse.(Int, datafwf[2:end,:])" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": null, 263 | "metadata": {}, 264 | "outputs": [], 265 | "source": [] 266 | } 267 | ], 268 | "metadata": { 269 | "@webio": { 270 | "lastCommId": null, 271 | "lastKernelId": null 272 | }, 273 | "kernelspec": { 274 | "display_name": "Julia 1.2.0", 275 | "language": "julia", 276 | "name": "julia-1.2" 277 | }, 278 | "language_info": { 279 | "file_extension": ".jl", 280 | "mimetype": "application/julia", 281 | "name": "julia", 282 | "version": "1.2.0" 283 | } 284 | }, 285 | "nbformat": 4, 286 | "nbformat_minor": 2 287 | } 288 | --------------------------------------------------------------------------------