Skip to content

Commit

Permalink
add note
Browse files Browse the repository at this point in the history
add handwriting recognize
  • Loading branch information
math4mad committed Nov 3, 2023
1 parent ffa2905 commit c46a51e
Show file tree
Hide file tree
Showing 4 changed files with 1,875 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"hash": "dc5c07a1d428acb02815231925667cd2",
"result": {
"markdown": "---\ntitle: \"2-recognizing-handwriting-digits\"\ncode-fold: false\n---\n\n:::{.callout-note title=\"info\"}\n1. ref:scikit-learn-docs/auto_examples/classification/plot_digits_classification.html#sphx-glr-auto-examples-classification-plot-digits-classification-py\n2. data-processing:load-scikit-digits.jl ->csv\n:::\n\n\n## 1. load package\n\n::: {.cell execution_count=1}\n``` {.julia .cell-code}\n import MLJ:fit!,predict\n using CSV,DataFrames,GLMakie,Random\n using MLJ\n Random.seed!(34343)\n```\n\n::: {.cell-output .cell-output-display execution_count=8}\n```\nTaskLocalRNG()\n```\n:::\n:::\n\n\n## 2. load data\n\n::: {.cell execution_count=2}\n``` {.julia .cell-code}\n load_csv(str::AbstractString) =\n str |> d -> CSV.File(\"./data/$str.csv\") |> DataFrame |> dropmissing\n\n digits = load_csv(\"scikit_digits\")\n digits = coerce(digits, :target => Multiclass)\n y, X = unpack(digits, ==(:target); rng = 123);\n (Xtrain, Xtest), (ytrain, ytest) = partition((X, y), 0.6, multi=true, rng=123)\n```\n\n::: {.cell-output .cell-output-display execution_count=9}\n\n::: {.ansi-escaped-output}\n```{=html}\n<pre>((<span class=\"ansi-bold\">1078×64 DataFrame</span>\n<span class=\"ansi-bold\"> Row </span>│<span class=\"ansi-bold\"> x1 </span><span class=\"ansi-bold\"> x2 </span><span class=\"ansi-bold\"> x3 </span><span class=\"ansi-bold\"> x4 </span><span class=\"ansi-bold\"> x5 </span><span class=\"ansi-bold\"> x6 </span><span class=\"ansi-bold\"> x7 </span><span class=\"ansi-bold\"> x8 </span> ⋯\n │<span class=\"ansi-bright-black-fg\"> Float64 </span><span class=\"ansi-bright-black-fg\"> Float64 </span><span class=\"ansi-bright-black-fg\"> Float64 </span><span class=\"ansi-bright-black-fg\"> Float64 </span><span class=\"ansi-bright-black-fg\"> Float64 </span><span class=\"ansi-bright-black-fg\"> Float64 </span><span class=\"ansi-bright-black-fg\"> Float64 </span><span class=\"ansi-bright-black-fg\"> Float64</span> ⋯\n──────┼─────────────────────────────────────────────────────────────────────────\n 1 │ 0.0 0.0 10.0 15.0 1.0 0.0 0.0 0.0 ⋯\n 2 │ 0.0 0.0 4.0 12.0 16.0 14.0 7.0 0.0\n 3 │ 0.0 0.0 2.0 10.0 15.0 1.0 0.0 0.0\n 4 │ 0.0 0.0 11.0 16.0 16.0 12.0 0.0 0.0\n 5 │ 0.0 0.0 5.0 15.0 12.0 4.0 0.0 0.0 ⋯\n 6 │ 0.0 1.0 11.0 15.0 16.0 9.0 0.0 0.0\n 7 │ 0.0 1.0 10.0 15.0 15.0 3.0 0.0 0.0\n 8 │ 0.0 0.0 8.0 14.0 1.0 0.0 0.0 0.0\n 9 │ 0.0 3.0 15.0 16.0 14.0 1.0 0.0 0.0 ⋯\n 10 │ 0.0 0.0 2.0 12.0 14.0 4.0 0.0 0.0\n 11 │ 0.0 0.0 3.0 14.0 9.0 3.0 0.0 0.0\n ⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋱\n 1069 │ 0.0 0.0 7.0 16.0 15.0 4.0 0.0 0.0\n 1070 │ 0.0 0.0 0.0 9.0 16.0 9.0 0.0 0.0 ⋯\n 1071 │ 0.0 0.0 0.0 3.0 16.0 3.0 0.0 0.0\n 1072 │ 0.0 0.0 5.0 8.0 11.0 5.0 0.0 0.0\n 1073 │ 0.0 2.0 9.0 15.0 14.0 9.0 3.0 0.0\n 1074 │ 0.0 1.0 12.0 16.0 13.0 7.0 0.0 0.0 ⋯\n 1075 │ 0.0 0.0 0.0 2.0 16.0 3.0 0.0 0.0\n 1076 │ 0.0 0.0 14.0 16.0 15.0 3.0 0.0 0.0\n 1077 │ 0.0 0.0 2.0 11.0 15.0 2.0 0.0 0.0\n 1078 │ 0.0 0.0 0.0 4.0 15.0 6.0 0.0 0.0 ⋯\n<span class=\"ansi-cyan-fg\"> 56 columns and 1057 rows omitted</span>, <span class=\"ansi-bold\">719×64 DataFrame</span>\n<span class=\"ansi-bold\"> Row </span>│<span class=\"ansi-bold\"> x1 </span><span class=\"ansi-bold\"> x2 </span><span class=\"ansi-bold\"> x3 </span><span class=\"ansi-bold\"> x4 </span><span class=\"ansi-bold\"> x5 </span><span class=\"ansi-bold\"> x6 </span><span class=\"ansi-bold\"> x7 </span><span class=\"ansi-bold\"> x8 </span> ⋯\n │<span class=\"ansi-bright-black-fg\"> Float64 </span><span class=\"ansi-bright-black-fg\"> Float64 </span><span class=\"ansi-bright-black-fg\"> Float64 </span><span class=\"ansi-bright-black-fg\"> Float64 </span><span class=\"ansi-bright-black-fg\"> Float64 </span><span class=\"ansi-bright-black-fg\"> Float64 </span><span class=\"ansi-bright-black-fg\"> Float64 </span><span class=\"ansi-bright-black-fg\"> Float64 </span> ⋯\n─────┼──────────────────────────────────────────────────────────────────────────\n 1 │ 0.0 0.0 3.0 12.0 3.0 0.0 0.0 0.0 ⋯\n 2 │ 0.0 0.0 1.0 12.0 14.0 3.0 0.0 0.0\n 3 │ 0.0 0.0 8.0 13.0 11.0 1.0 0.0 0.0\n 4 │ 0.0 0.0 2.0 13.0 0.0 0.0 0.0 0.0\n 5 │ 0.0 1.0 13.0 16.0 16.0 12.0 1.0 0.0 ⋯\n 6 │ 0.0 0.0 7.0 16.0 13.0 2.0 0.0 0.0\n 7 │ 0.0 0.0 11.0 16.0 9.0 8.0 0.0 0.0\n 8 │ 0.0 0.0 0.0 1.0 9.0 14.0 7.0 0.0\n 9 │ 0.0 0.0 7.0 15.0 16.0 16.0 1.0 0.0 ⋯\n 10 │ 0.0 0.0 5.0 10.0 14.0 16.0 11.0 0.0\n 11 │ 0.0 0.0 7.0 16.0 16.0 16.0 6.0 0.0\n ⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋱\n 710 │ 0.0 1.0 9.0 16.0 16.0 15.0 3.0 0.0\n 711 │ 0.0 0.0 1.0 15.0 7.0 0.0 0.0 0.0 ⋯\n 712 │ 0.0 0.0 6.0 14.0 13.0 4.0 0.0 0.0\n 713 │ 0.0 0.0 2.0 13.0 14.0 6.0 0.0 0.0\n 714 │ 0.0 0.0 2.0 12.0 10.0 0.0 0.0 0.0\n 715 │ 0.0 0.0 2.0 14.0 15.0 4.0 0.0 0.0 ⋯\n 716 │ 0.0 0.0 3.0 10.0 14.0 3.0 0.0 0.0\n 717 │ 0.0 0.0 7.0 16.0 15.0 4.0 0.0 0.0\n 718 │ 0.0 0.0 6.0 15.0 14.0 2.0 0.0 0.0\n 719 │ 0.0 0.0 5.0 16.0 12.0 2.0 0.0 0.0 ⋯\n<span class=\"ansi-cyan-fg\"> 56 columns and 698 rows omitted</span>), (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]))</pre>\n```\n:::\n\n:::\n:::\n\n\n## 3. MLJ workflow\n\n::: {.cell execution_count=3}\n``` {.julia .cell-code}\nSVC = @load SVC pkg=LIBSVM\n model=SVC()\n mach = machine(model, Xtrain, ytrain)|>fit!\n yhat = predict(mach, Xtest)\n @info \"accuracy\"=>accuracy(ytest,yhat)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\nimport MLJLIBSVMInterface ✔\n```\n:::\n\n::: {.cell-output .cell-output-stderr}\n```\n[ Info: For silent loading, specify `verbosity=0`. \n[ Info: Training machine(SVC(kernel = RadialBasis, …), …).\n[ Info: \"accuracy\" => 0.9860917941585535\n```\n:::\n:::\n\n\n## 4. plot results\n\n:::{.callout-tip title=\"makie plot image\"}\n 1. reshape to image matrix `img=Xtest[pick,:]|>Vector|>d->reshape(d,8,8)`\n 2. sometimes need rotl `load(patharr[num])|>rotl90`\n 3. sometimes need yreversed=true `local ax = Axis(fig[i, j],yreversed=true)`\n \n:::\n\n::: {.cell execution_count=4}\n``` {.julia .cell-code}\n function plot_res()\n fig=Figure()\n #rand pick 15 origin picture of digits\n picks=rand(1:length(Vector(ytest)),15)\n for (idx,pick) in enumerate(picks)\n \n ax=Axis(fig[fldmod1(idx,5)...];title=\"pred:$(yhat[pick])\",yreversed=true)\n img=Xtest[pick,:]|>Vector|>d->reshape(d,8,8)\n image!(ax,img)\n hidedecorations!(ax)\n end\n fig\n end\n\n fig=plot_res()\n```\n\n::: {.cell-output .cell-output-display execution_count=11}\n![](3-recognizing-handwriting-digits_files/figure-html/cell-5-output-1.png){}\n:::\n:::\n\n\n",
"supporting": [
"3-recognizing-handwriting-digits_files"
],
"filters": [],
"includes": {}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions category/classification/3-recognizing-handwriting-digits.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
title: "2-recognizing-handwriting-digits"
code-fold: false
---

:::{.callout-note title="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
```{julia}
import MLJ:fit!,predict
using CSV,DataFrames,GLMakie,Random
using MLJ
Random.seed!(34343)
```

## 2. load data

```{julia}
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)
```

## 3. MLJ workflow
```{julia}
SVC = @load SVC pkg=LIBSVM
model=SVC()
mach = machine(model, Xtrain, ytrain)|>fit!
yhat = predict(mach, Xtest)
@info "accuracy"=>accuracy(ytest,yhat)
```

## 4. plot results

:::{.callout-tip title="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)`

:::

```{julia}
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()
```
Loading

0 comments on commit c46a51e

Please sign in to comment.