@@ -65,17 +65,20 @@ be implemented as follows:
65
65
using Statistics
66
66
67
67
function TransformsBase. apply (transform:: Standardize , X)
68
- # convert the table to a matrix
68
+ # convert the table to a matrix and get col names
69
69
Xm = Tables. matrix (X)
70
+ names = Tables. columnnames (X)
70
71
# compute the means and stds
71
72
μ = transform. center ? mean (Xm, dims= 1 ) : zeros (1 , size (Xm, 2 ))
72
73
σ = transform. scale ? std (Xm, dims= 1 ) : ones (1 , size (Xm, 2 ))
73
74
# standardize the data
74
75
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)
77
80
# return the table and cache that may help reapply or revert later
78
- return Xm , (μ, σ)
81
+ return X , (μ, σ)
79
82
end
80
83
```
81
84
@@ -100,15 +103,18 @@ the cache and doesn't return it.
100
103
101
104
``` julia
102
105
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
104
107
Xm = Tables. matrix (X)
108
+ names = Tables. columnnames (X)
105
109
# no need to recompute means and stds
106
110
μ, σ = cache
107
111
# standardize the data
108
112
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
112
118
end
113
119
```
114
120
@@ -129,15 +135,18 @@ implemented in that case. Now we follow up by implementing the `revert` method:
129
135
130
136
``` julia
131
137
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
133
139
Xm = Tables. matrix (X)
140
+ names = Tables. columnnames (X)
134
141
# extract the mean and std
135
142
μ, σ = cache
136
143
# revert the transform
137
144
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
141
150
end
142
151
```
143
152
0 commit comments