3-recognizing-handwriting-digits

info
  1. ref:scikit-learn-docs/auto_examples/classification/plot_digits_classification.html#sphx-glr-auto-examples-classification-plot-digits-classification-py
  2. data-processing:load-scikit-digits.jl ->csv

1. load package

    import MLJ:fit!,predict
    using CSV,DataFrames,GLMakie,Random
    using MLJ
    Random.seed!(34343)
TaskLocalRNG()

2. load data

  load_csv(str::AbstractString) =
    str |> d -> CSV.File("./data/$str.csv") |> DataFrame |> dropmissing

  digits = load_csv("scikit_digits")
  digits = coerce(digits, :target => Multiclass)
  y, X = unpack(digits, ==(:target); rng = 123);
  (Xtrain, Xtest), (ytrain, ytest)  = partition((X, y), 0.6, multi=true,  rng=123)
((1078×64 DataFrame
  Row  x1       x2       x3       x4       x5       x6       x7       x8      ⋯
      │ Float64  Float64  Float64  Float64  Float64  Float64  Float64  Float64 ⋯
──────┼─────────────────────────────────────────────────────────────────────────
    1 │     0.0      0.0     10.0     15.0      1.0      0.0      0.0      0.0 ⋯
    2 │     0.0      0.0      4.0     12.0     16.0     14.0      7.0      0.0
    3 │     0.0      0.0      2.0     10.0     15.0      1.0      0.0      0.0
    4 │     0.0      0.0     11.0     16.0     16.0     12.0      0.0      0.0
    5 │     0.0      0.0      5.0     15.0     12.0      4.0      0.0      0.0 ⋯
    6 │     0.0      1.0     11.0     15.0     16.0      9.0      0.0      0.0
    7 │     0.0      1.0     10.0     15.0     15.0      3.0      0.0      0.0
    8 │     0.0      0.0      8.0     14.0      1.0      0.0      0.0      0.0
    9 │     0.0      3.0     15.0     16.0     14.0      1.0      0.0      0.0 ⋯
   10 │     0.0      0.0      2.0     12.0     14.0      4.0      0.0      0.0
   11 │     0.0      0.0      3.0     14.0      9.0      3.0      0.0      0.0
  ⋮   │    ⋮        ⋮        ⋮        ⋮        ⋮        ⋮        ⋮        ⋮    ⋱
 1069 │     0.0      0.0      7.0     16.0     15.0      4.0      0.0      0.0
 1070 │     0.0      0.0      0.0      9.0     16.0      9.0      0.0      0.0 ⋯
 1071 │     0.0      0.0      0.0      3.0     16.0      3.0      0.0      0.0
 1072 │     0.0      0.0      5.0      8.0     11.0      5.0      0.0      0.0
 1073 │     0.0      2.0      9.0     15.0     14.0      9.0      3.0      0.0
 1074 │     0.0      1.0     12.0     16.0     13.0      7.0      0.0      0.0 ⋯
 1075 │     0.0      0.0      0.0      2.0     16.0      3.0      0.0      0.0
 1076 │     0.0      0.0     14.0     16.0     15.0      3.0      0.0      0.0
 1077 │     0.0      0.0      2.0     11.0     15.0      2.0      0.0      0.0
 1078 │     0.0      0.0      0.0      4.0     15.0      6.0      0.0      0.0 ⋯
                                                56 columns and 1057 rows omitted, 719×64 DataFrame
 Row  x1       x2       x3       x4       x5       x6       x7       x8       ⋯
     │ Float64  Float64  Float64  Float64  Float64  Float64  Float64  Float64  ⋯
─────┼──────────────────────────────────────────────────────────────────────────
   1 │     0.0      0.0      3.0     12.0      3.0      0.0      0.0      0.0  ⋯
   2 │     0.0      0.0      1.0     12.0     14.0      3.0      0.0      0.0
   3 │     0.0      0.0      8.0     13.0     11.0      1.0      0.0      0.0
   4 │     0.0      0.0      2.0     13.0      0.0      0.0      0.0      0.0
   5 │     0.0      1.0     13.0     16.0     16.0     12.0      1.0      0.0  ⋯
   6 │     0.0      0.0      7.0     16.0     13.0      2.0      0.0      0.0
   7 │     0.0      0.0     11.0     16.0      9.0      8.0      0.0      0.0
   8 │     0.0      0.0      0.0      1.0      9.0     14.0      7.0      0.0
   9 │     0.0      0.0      7.0     15.0     16.0     16.0      1.0      0.0  ⋯
  10 │     0.0      0.0      5.0     10.0     14.0     16.0     11.0      0.0
  11 │     0.0      0.0      7.0     16.0     16.0     16.0      6.0      0.0
  ⋮  │    ⋮        ⋮        ⋮        ⋮        ⋮        ⋮        ⋮        ⋮     ⋱
 710 │     0.0      1.0      9.0     16.0     16.0     15.0      3.0      0.0
 711 │     0.0      0.0      1.0     15.0      7.0      0.0      0.0      0.0  ⋯
 712 │     0.0      0.0      6.0     14.0     13.0      4.0      0.0      0.0
 713 │     0.0      0.0      2.0     13.0     14.0      6.0      0.0      0.0
 714 │     0.0      0.0      2.0     12.0     10.0      0.0      0.0      0.0
 715 │     0.0      0.0      2.0     14.0     15.0      4.0      0.0      0.0  ⋯
 716 │     0.0      0.0      3.0     10.0     14.0      3.0      0.0      0.0
 717 │     0.0      0.0      7.0     16.0     15.0      4.0      0.0      0.0
 718 │     0.0      0.0      6.0     15.0     14.0      2.0      0.0      0.0
 719 │     0.0      0.0      5.0     16.0     12.0      2.0      0.0      0.0  ⋯
                                                 56 columns and 698 rows omitted), (CategoricalArrays.CategoricalValue{Int64, UInt32}[1, 3, 0, 9, 0, 3, 9, 1, 2, 0  …  5, 8, 4, 0, 3, 3, 4, 3, 0, 4], CategoricalArrays.CategoricalValue{Int64, UInt32}[6, 0, 3, 6, 3, 2, 8, 9, 5, 5  …  5, 4, 8, 8, 2, 1, 9, 0, 3, 0]))

3. MLJ workflow

SVC = @load SVC pkg=LIBSVM
    model=SVC()
    mach = machine(model, Xtrain, ytrain)|>fit!
    yhat = predict(mach, Xtest)
    @info "accuracy"=>accuracy(ytest,yhat)
[ Info: For silent loading, specify `verbosity=0`. 
[ Info: Training machine(SVC(kernel = RadialBasis, …), …).
[ Info: "accuracy" => 0.9860917941585535
import MLJLIBSVMInterface ✔

4. plot results

makie plot image
  1. reshape to image matrix img=Xtest[pick,:]|>Vector|>d->reshape(d,8,8)
  2. sometimes need rotl load(patharr[num])|>rotl90
  3. sometimes need reverse y axis local ax = Axis(fig[i, j],yreversed=true)
        function plot_res()
            fig=Figure()
            #rand pick 15 origin picture of  digits
            picks=rand(1:length(Vector(ytest)),15)
            for (idx,pick) in enumerate(picks)
                
                ax=Axis(fig[fldmod1(idx,5)...];title="pred:$(yhat[pick])",yreversed=true)
                img=Xtest[pick,:]|>Vector|>d->reshape(d,8,8)
                image!(ax,img)
                hidedecorations!(ax)
            end
        fig
     end

    fig=plot_res()