Trucs et astuces Julia

Remplacer des éléments2 d'un Array
replace.(["a","b","c","d","e","f"], r"b|c" => "a", r"e|f" => "d")
replace.(["a","b","c","d","e","f"], Regex("b|c") => "a", Regex("e|f") => "d")
[map(x -> x =>"a", ["b","c"])..., map(x -> x =>"d", ["e","f"])...]
replace.(["a","b","c","d","e","f"], [map(x -> x =>"a", ["b","c"])..., map(x -> x =>"d", ["e","f"])...]...)
[map(x -> x =>"a", ["b","c"])... map(x -> x =>"d", ["e","f"])...]
replace.(["a","b","c","d","e","f"], [map(x -> x =>"a", ["b","c"])... map(x -> x =>"d", ["e","f"])...]...)
julia> replace.(["a","b","c","d","e","f"], r"b|c" => "a", r"e|f" => "d")
6-element Vector{String}:
 "a"
 "a"
 "a"
 "d"
 "d"
 "d"
julia> replace.(["a","b","c","d","e","f"], Regex("b|c") => "a", Regex("e|f") => "d")
6-element Vector{String}:
 "a"
 "a"
 "a"
 "d"
 "d"
 "d"
julia> [map(x -> x =>"a", ["b","c"])..., map(x -> x =>"d", ["e","f"])...]
4-element Vector{Pair{String, String}}:
 "b" => "a"
 "c" => "a"
 "e" => "d"
 "f" => "d"
julia> replace.(["a","b","c","d","e","f"], [map(x -> x =>"a", ["b","c"])..., map(x -> x =>"d", ["e","f"])...]...)
6-element Vector{String}:
 "a"
 "a"
 "a"
 "d"
 "d"
 "d"
julia> [map(x -> x =>"a", ["b","c"])... map(x -> x =>"d", ["e","f"])...]
1×4 Matrix{Pair{String, String}}:
 "b"=>"a"  "c"=>"a"  "e"=>"d"  "f"=>"d"
julia> replace.(["a","b","c","d","e","f"], [map(x -> x =>"a", ["b","c"])... map(x -> x =>"d", ["e","f"])...]...)
6-element Vector{String}:
 "a"
 "a"
 "a"
 "d"
 "d"
 "d"

Transformer les variables de type AbstractString en CategoricalArrays
using DataFrames, CategoricalArrays
df = DataFrame(a=["a","b"],b=["c","d"],c=[1,2])
names(df)
names(df, AbstractString)
names(df, AbstractString) .=> categorical
categorical(df[!,1])
transform(df, names(df, AbstractString) .=> categorical, renamecols=false)
df
transform!(df, names(df, AbstractString) .=> categorical, renamecols=false)
df
julia> using DataFrames, CategoricalArrays
julia> df = DataFrame(a=["a","b"],b=["c","d"],c=[1,2])
2×3 DataFrame
 Row │ a       b       c
     │ String  String  Int64
─────┼───────────────────────
   1 │ a       c           1
   2 │ b       d           2
julia> names(df)
3-element Vector{String}:
 "a"
 "b"
 "c"
julia> names(df, AbstractString)
2-element Vector{String}:
 "a"
 "b"
julia> names(df, AbstractString) .=> categorical
2-element Vector{Pair{String, typeof(CategoricalArrays.categorical)}}:
 "a" => CategoricalArrays.categorical
 "b" => CategoricalArrays.categorical
julia> categorical(df[!,1])
2-element CategoricalArrays.CategoricalArray{String,1,UInt32}:
 "a"
 "b"
julia> transform(df, names(df, AbstractString) .=> categorical, renamecols=false)
2×3 DataFrame
 Row │ a     b     c
     │ Cat…  Cat…  Int64
─────┼───────────────────
   1 │ a     c         1
   2 │ b     d         2
julia> df
2×3 DataFrame
 Row │ a       b       c
     │ String  String  Int64
─────┼───────────────────────
   1 │ a       c           1
   2 │ b       d           2
julia> transform!(df, names(df, AbstractString) .=> categorical, renamecols=false)
2×3 DataFrame
 Row │ a     b     c
     │ Cat…  Cat…  Int64
─────┼───────────────────
   1 │ a     c         1
   2 │ b     d         2
julia> df
2×3 DataFrame
 Row │ a     b     c
     │ Cat…  Cat…  Int64
─────┼───────────────────
   1 │ a     c         1
   2 │ b     d         2

Extraire sliced Array de dimension inférieure à celle du Array original
A=randn(2,3,4)
A
A[fill(:,ndims(A)-1)...,2] # ou A[repeat([:],ndims(A)-1)...,2]
selectdim(A,3,2)
view(A,fill(:,ndims(A)-1)...,2) # équivalent à selectdim(A,3,2)
@view  A[fill(:,ndims(A)-1)...,2] # équivalent à selectdim(A,3,2)
A[2,fill(:,ndims(A)-1)...]
selectdim(A,1,2)
A=randn(2,3,4,2)
A[fill(:,ndims(A)-1)...,2]
selectdim(A,4,2)
A[2,fill(:,ndims(A)-1)...]
selectdim(A,1,2)
julia> A=randn(2,3,4)
2×3×4 Array{Float64, 3}:
[:, :, 1] =
 0.805432  -1.99132    -0.924161
 0.328323  -0.0911861   0.612841

[:, :, 2] =
 1.57188   -0.337321  -0.311395
 0.547578  -0.693774  -0.580376

[:, :, 3] =
 -0.368712   -0.139084  0.00401706
  0.0269076   0.865916  0.914049

[:, :, 4] =
 -0.145264  -0.233612  -0.0871394
  1.15895    0.840884   0.117644
julia> A
2×3×4 Array{Float64, 3}:
[:, :, 1] =
 0.805432  -1.99132    -0.924161
 0.328323  -0.0911861   0.612841

[:, :, 2] =
 1.57188   -0.337321  -0.311395
 0.547578  -0.693774  -0.580376

[:, :, 3] =
 -0.368712   -0.139084  0.00401706
  0.0269076   0.865916  0.914049

[:, :, 4] =
 -0.145264  -0.233612  -0.0871394
  1.15895    0.840884   0.117644
julia> A[fill(:,ndims(A)-1)...,2] # ou A[repeat([:],ndims(A)-1)...,2]
2×3 Matrix{Float64}:
 1.57188   -0.337321  -0.311395
 0.547578  -0.693774  -0.580376
julia> selectdim(A,3,2)
2×3 view(::Array{Float64, 3}, :, :, 2) with eltype Float64:
 1.57188   -0.337321  -0.311395
 0.547578  -0.693774  -0.580376
julia> view(A,fill(:,ndims(A)-1)...,2) # équivalent à selectdim(A,3,2)
2×3 view(::Array{Float64, 3}, :, :, 2) with eltype Float64:
 1.57188   -0.337321  -0.311395
 0.547578  -0.693774  -0.580376
julia> @view  A[fill(:,ndims(A)-1)...,2] # équivalent à selectdim(A,3,2)
2×3 view(::Array{Float64, 3}, :, :, 2) with eltype Float64:
 1.57188   -0.337321  -0.311395
 0.547578  -0.693774  -0.580376
julia> A[2,fill(:,ndims(A)-1)...]
3×4 Matrix{Float64}:
  0.328323    0.547578  0.0269076  1.15895
 -0.0911861  -0.693774  0.865916   0.840884
  0.612841   -0.580376  0.914049   0.117644
julia> selectdim(A,1,2)
3×4 view(::Array{Float64, 3}, 2, :, :) with eltype Float64:
  0.328323    0.547578  0.0269076  1.15895
 -0.0911861  -0.693774  0.865916   0.840884
  0.612841   -0.580376  0.914049   0.117644
julia> A=randn(2,3,4,2)
2×3×4×2 Array{Float64, 4}:
[:, :, 1, 1] =
 -0.660556  1.32898  0.283157
  0.19922   2.13099  0.039278

[:, :, 2, 1] =
 -0.111894  -0.768979  0.382002
 -1.2256     0.201696  1.74168

[:, :, 3, 1] =
  1.17518  -0.287279  -1.71337
 -1.33192  -0.692452  -0.859028

[:, :, 4, 1] =
  0.240421  0.0638988  -0.974078
 -0.779252  0.0545527  -0.522599

[:, :, 1, 2] =
  1.40288   0.426945    0.00464084
 -0.160773  0.0643719  -0.0358803

[:, :, 2, 2] =
  1.66437   0.498307  -1.0145
 -0.557767  1.31562   -1.86125

[:, :, 3, 2] =
 -1.38932    0.429232  -0.65331
  0.0455178  0.488717   0.558956

[:, :, 4, 2] =
 0.202175    0.470603  0.818058
 0.0673545  -1.19355   0.150915
julia> A[fill(:,ndims(A)-1)...,2]
2×3×4 Array{Float64, 3}:
[:, :, 1] =
  1.40288   0.426945    0.00464084
 -0.160773  0.0643719  -0.0358803

[:, :, 2] =
  1.66437   0.498307  -1.0145
 -0.557767  1.31562   -1.86125

[:, :, 3] =
 -1.38932    0.429232  -0.65331
  0.0455178  0.488717   0.558956

[:, :, 4] =
 0.202175    0.470603  0.818058
 0.0673545  -1.19355   0.150915
julia> selectdim(A,4,2)
2×3×4 view(::Array{Float64, 4}, :, :, :, 2) with eltype Float64:
[:, :, 1] =
  1.40288   0.426945    0.00464084
 -0.160773  0.0643719  -0.0358803

[:, :, 2] =
  1.66437   0.498307  -1.0145
 -0.557767  1.31562   -1.86125

[:, :, 3] =
 -1.38932    0.429232  -0.65331
  0.0455178  0.488717   0.558956

[:, :, 4] =
 0.202175    0.470603  0.818058
 0.0673545  -1.19355   0.150915
julia> A[2,fill(:,ndims(A)-1)...]
3×4×2 Array{Float64, 3}:
[:, :, 1] =
 0.19922   -1.2256    -1.33192   -0.779252
 2.13099    0.201696  -0.692452   0.0545527
 0.039278   1.74168   -0.859028  -0.522599

[:, :, 2] =
 -0.160773   -0.557767  0.0455178   0.0673545
  0.0643719   1.31562   0.488717   -1.19355
 -0.0358803  -1.86125   0.558956    0.150915
julia> selectdim(A,1,2)
3×4×2 view(::Array{Float64, 4}, 2, :, :, :) with eltype Float64:
[:, :, 1] =
 0.19922   -1.2256    -1.33192   -0.779252
 2.13099    0.201696  -0.692452   0.0545527
 0.039278   1.74168   -0.859028  -0.522599

[:, :, 2] =
 -0.160773   -0.557767  0.0455178   0.0673545
  0.0643719   1.31562   0.488717   -1.19355
 -0.0358803  -1.86125   0.558956    0.150915