Skip to content

Commit 40e27e3

Browse files
EssamWisamjuliohm
andauthored
Fix custom transforms docs example (#191)
* add docs for custom transforms * Update docs/make.jl Co-authored-by: Júlio Hoffimann <[email protected]> * Update docs/src/custom.md Co-authored-by: Júlio Hoffimann <[email protected]> * Update docs/src/custom.md Co-authored-by: Júlio Hoffimann <[email protected]> * Update docs/src/custom.md Co-authored-by: Júlio Hoffimann <[email protected]> * Update docs/src/custom.md Co-authored-by: Júlio Hoffimann <[email protected]> * Update docs/src/custom.md Co-authored-by: Júlio Hoffimann <[email protected]> * Update docs/src/custom.md Co-authored-by: Júlio Hoffimann <[email protected]> * Update docs/src/custom.md Co-authored-by: Júlio Hoffimann <[email protected]> * Update docs/src/custom.md Co-authored-by: Júlio Hoffimann <[email protected]> * Update docs/src/custom.md Co-authored-by: Júlio Hoffimann <[email protected]> * improve markdown for devguide * Fix docs * fix doc examples * Revert "fix doc examples" This reverts commit c10e448. * reapply fix docs example * Update docs/make.jl --------- Co-authored-by: Júlio Hoffimann <[email protected]>
1 parent d2334a1 commit 40e27e3

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

docs/src/devguide.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,20 @@ be implemented as follows:
6565
using Statistics
6666

6767
function TransformsBase.apply(transform::Standardize, X)
68-
# convert the table to a matrix
68+
# convert the table to a matrix and get col names
6969
Xm = Tables.matrix(X)
70+
names = Tables.columnnames(X)
7071
# compute the means and stds
7172
μ = transform.center ? mean(Xm, dims=1) : zeros(1, size(Xm, 2))
7273
σ = transform.scale ? std(Xm, dims=1) : ones(1, size(Xm, 2))
7374
# standardize the data
7475
Xm = (Xm .- μ) ./ σ
75-
# convert back to table
76-
Xm = X |> Tables.materializer(Xm)
76+
# convert matrix to column table
77+
Xc = (; zip(names, eachcol(Xm))...)
78+
# convert back to original table type
79+
X = Xc |> Tables.materializer(X)
7780
# return the table and cache that may help reapply or revert later
78-
return Xm, (μ, σ)
81+
return X, (μ, σ)
7982
end
8083
```
8184

@@ -100,15 +103,18 @@ the cache and doesn't return it.
100103

101104
```julia
102105
function TransformsBase.reapply(transform::Standardize, X, cache)
103-
# convert the table to a matrix
106+
# convert the table to a matrix and get col names
104107
Xm = Tables.matrix(X)
108+
names = Tables.columnnames(X)
105109
# no need to recompute means and stds
106110
μ, σ = cache
107111
# standardize the data
108112
Xm = (Xm .- μ) ./ σ
109-
# convert back to table
110-
Xm = X |> Tables.materializer(Xm)
111-
return Xm
113+
# convert matrix to column table
114+
Xc = (; zip(names, eachcol(Xm))...)
115+
# convert back to original table type
116+
X = Xc |> Tables.materializer(X)
117+
return X
112118
end
113119
```
114120

@@ -129,15 +135,18 @@ implemented in that case. Now we follow up by implementing the `revert` method:
129135

130136
```julia
131137
function TransformsBase.revert(transform::Standardize, X, cache)
132-
# convert the table to a matrix
138+
# convert the table to a matrix and get col names
133139
Xm = Tables.matrix(X)
140+
names = Tables.columnnames(X)
134141
# extract the mean and std
135142
μ, σ = cache
136143
# revert the transform
137144
Xm = Xm .* σ .+ μ
138-
# convert back to table
139-
Xm = X |> Tables.materializer(Xm)
140-
return Xm
145+
# convert matrix to column table
146+
Xc = (; zip(names, eachcol(Xm))...)
147+
# convert back to original table type
148+
X = Xc |> Tables.materializer(X)
149+
return X
141150
end
142151
```
143152

0 commit comments

Comments
 (0)