投稿

8月, 2018の投稿を表示しています

julia では println() 内の変数では、$を使って、"," を減らす方が早いようだ

今さらですが、 println() 内の変数では、$を使って、"," を減らす方が早いようです。 #1 julia> @time for animal in ["dog", "cat", "mouse"] println(animal," is a mammal") end dog is a mammal cat is a mammal mouse is a mammal 0.000147 seconds (20 allocations: 848 bytes) julia> @time for animal in ["dog", "cat", "mouse"] println("$animal is a mammal") end dog is a mammal cat is a mammal mouse is a mammal 0.000082 seconds (17 allocations: 864 bytes) #2 julia> @time for (k, v) in Dict("dog" => "mammal", "cat" => "mammal", "mouse" => "mammal") println("$k is a $v") end mouse is a mammal cat is a mammal dog is a mammal 0.000082 seconds (23 allocations: 1.063 KiB) julia> @time for (k, v) in Dict("dog" => "mammal", "cat" => "mammal", ...

julia では、collect(1:10) と、[1:10;] が同義らしい

今までは、せっせと collect(1:10) の様に書いていましたが、[1:10;] と同義のようです。少し文字数が減ります。 julia> collect(1:10) == [1:10;] true julia> collect(1:10) == [1:10] false julia> collect(1:10) .== [1:10;] 10-element BitArray{1}: true true true true true true true true true true

julia 1.0.0 + DataFrames v0.13.0 の the dot operator

julia が 1.0 になったことで、the dot operator というやつが導入・強化されているようです。 version は下記のとおりです。 (v1.0) pkg> status Status `~/.julia/environments/v1.0/Project.toml` [a93c6f00] DataFrames v0.13.0 以下略 まずは、普通にデータフレームを組みます。 julia> df = DataFrame(a=randn(10^5),b=randn(10^5)) 100000×2 DataFrame │ Row │ a │ b │ ├────────┼───────────┼───────────┤ │ 1 │ 1.06429 │ -0.665591 │ │ 2 │ 1.34957 │ 1.5088 │ │ 3 │ 1.26694 │ -1.61644 │ ...略 julia> df[:c] = "hoge".* string.(collect(1:10^5)) 100000-element Array{String,1}: "hoge1" "hoge2" "hoge3" ...略 df の a の column を表示する方法は、df[:a] でしたが、df.a という書式が許されるようになりました。 julia> df[:a] 100000-element Array{Float64,1}: 1.0642945299080955 1.3495717535723946 1.2669443981981892 ...略 julia> df.a 100000-element Array{Float64,1}: 1.0642945299080955 1.3495717535723946 1.2669443981981892 ...略 R だったら df$a で良かったものを、df[:a]と少しタイプする量が増えていまし...

julia1.0 でのパッケージのインストール

はじめに、祝 1.0 なわけですが、いきなり、つまずきました。 _ _ _(_)_ | Documentation: https://docs.julialang.org (_) | (_) (_) | _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help. | | | | | | |/ _` | | | | |_| | | | (_| | | Version 1.0.0 (2018-08-08) _/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release |__/ | julia> Pkg.add() ERROR: UndefVarError: Pkg not defined Stacktrace: [1] top-level scope at none:0 julia> quit() ERROR: UndefVarError: quit not defined Stacktrace: [1] top-level scope at none:0 素の julia の機能しか使えないに加えて、終了もできないのです。 Pkg.jl のマニュアル を読むことになります。 Getting Started The Pkg REPL-mode is entered from the Julia REPL using the key ]. Pkg REPL モード なるものが、導入ですか!! "]"を押すのですか!! 何も書いていませんが、Pkg.add() は、使えなくなるのですね!!! (v1.0) pkg> add("DataFrames") ERROR: expected command. instead got [add(] (v1.0) pkg> got("DataFrames") ERROR: expected command. instead got...

julia でギリシア文字を入力するの方法

"\" に続いて、"pi", "alpha",...などと入力して、タブを押すと変換される仕様になっていたそうです。 julia> \pi#ここでタブを押すと "π" に変換される julia の場合、π は、円周率の近似値が既定値で格納されています。 julia> π π = 3.1415926535897... 今頃、知りましたが、便利です。

julia で t 分布に従う 95% 信頼区間を求めてみよう

イメージ
お題のとおり、julia で t 分布に従う平均の 95% 信頼区間を求めてみます。 Distributions.jl を使います。少々、矛盾しますが、100 個の正規分布の乱数を観測値 (ベクトル) とします。 julia> using Distributions julia> x = randn(100) 100-element Array{Float64,1}: -0.307257 -0.074242 0.485464 1.47269 -0.0526559 -1.533 -0.123655 0.3673 -1.62462 -1.09255 ⋮ -0.313221 -0.655702 -0.933212 0.553407 -1.17567 -0.305231 1.8448 0.0459129 1.88275 -0.764762 julia> using PyCall, PyPlot julia> @pyimport seaborn as sns julia> sns.distplot(x, kde= false) PyObject <matplotlib.axes._subplots.AxesSubplot object at 0x130be8940> x = randn(100) は、x2 = rand(Normal(),100) としても良いです。 julia> x2 = rand(Normal(),100); julia> sns.distplot(x2, kde= false) PyObject <matplotlib.axes._subplots.AxesSubplot object at 0x130be8940> 薄い茶色が後の x2 の方です。 自由度 99 の T 分布 TDist(99) における 2.5% と、97.5% の確率密度関数の x 軸の値は、 quantile() を使って求めることができます。 julia> quantile.(TDist(99), [0.025, 0.975]) 2-element Array{Float64,1}: -1.98...