官术网_书友最值得收藏!

Standardization

As a last bit of transformation, we would need to standardize our input data. This allow us to compare models to see if one model is better than another. To do so, I wrote two different scaling algorithms:

func scale(a [][]float64, j int) {
l, m, h := iqr(a, 0.25, 0.75, j)
s := h - l
if s == 0 {
s = 1
}

for _, row := range a {
row[j] = (row[j] - m) / s
}
}

func scaleStd(a [][]float64, j int) {
var mean, variance, n float64
for _, row := range a {
mean += row[j]
n++
}
mean /= n
for _, row := range a {
variance += (row[j] - mean) * (row[j] - mean)
}
variance /= (n-1)

for _, row := range a {
row[j] = (row[j] - mean) / variance
}
}

If you come from the Python world of data science, the first scale function is essentially what scikits-learn's RobustScaler does. The second function is essentially StdScaler, but with the variance adapted to work for sample data.

This function takes the values in a given column (j) and scales them in such a way that all the values are constrained to within a certain value. Also, note that the input to both scaling functions is [][]float64. This is where the benefits of the tensor package comes in handy. A *tensor.Dense can be converted to [][]float64 without any extra allocations. An additional beneficial side effect is that you can mutate a and the tensor values will change as well. Essentially, [][]float64 will act as an iterator to the underlying tensor data.

Our transform function now looks like this:

func transform(it [][]float64, hdr []string, hints []bool) []int {
var transformed []int
for i, isCat := range hints {
if isCat {
continue
}
skewness := skew(it, i)
if skewness > 0.75 {
transformed = append(transformed, i)
log1pCol(it, i)
}
}
for i, h := range hints {
if !h {
scale(it, i)
}
}
return transformed
}

Note that we only want to scale the numerical variables. The categorical variables can be scaled, but there isn't really much difference.

主站蜘蛛池模板: 庆安县| 渭南市| 金秀| 阿城市| 横山县| 江口县| 云梦县| 洪湖市| 平山县| 临高县| 太和县| 昂仁县| 左云县| 淮阳县| 内丘县| 永城市| 天全县| 鄂托克前旗| 运城市| 虎林市| 政和县| 城固县| 昌平区| 南川市| 和田县| 固原市| 泊头市| 万荣县| 新竹县| 涞源县| 邯郸市| 湟中县| 六枝特区| 龙里县| 宣恩县| 普安县| 金寨县| 宁夏| 琼海市| 湘乡市| 通州市|