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

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.

主站蜘蛛池模板: 观塘区| 中山市| 青冈县| 威信县| 子长县| 新丰县| 成武县| 即墨市| 松江区| 满洲里市| 阳曲县| 六枝特区| 保靖县| 德惠市| 和静县| 达拉特旗| 永丰县| 阿勒泰市| 平江县| 长武县| 长春市| 梁平县| 哈尔滨市| 海门市| 麻城市| 台北市| 彭阳县| 互助| 松江区| 大港区| 含山县| 资中县| 西吉县| 安徽省| 清镇市| 营山县| 伽师县| 宁国市| 子洲县| 南投县| 门头沟区|