Skip to content

Commit

Permalink
use xgboost regressor
Browse files Browse the repository at this point in the history
Signed-off-by: Huamin Chen <[email protected]>
  • Loading branch information
rootfs committed Nov 21, 2023
1 parent a33d681 commit 90cc7d5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
37 changes: 29 additions & 8 deletions pkg/model/estimator/local/lr.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,37 @@ func (weights ModelWeights) predict(usageMetricNames []string, usageMetricValues
basePower += coeffMap[systemMetaDataFeatureValues[index]].Weight
}
var powers []float64
for _, vals := range usageMetricValues {
power := basePower
for index, coeff := range numericalWeights {
if coeff.Weight == 0 {
continue
switch weights.RegressorType {
case types.LinearRegressor:
for _, vals := range usageMetricValues {
power := basePower
for index, coeff := range numericalWeights {
if coeff.Weight == 0 {
continue
}
normalizedX := vals[index] / coeff.Scale
power += coeff.Weight * normalizedX
}
normalizedX := vals[index] / coeff.Scale
power += coeff.Weight * normalizedX
powers = append(powers, power)
}
powers = append(powers, power)
case types.XGBoostRegressor:
for _, vals := range usageMetricValues {
data := make([]float32, len(vals))
for index, coeff := range numericalWeights {
if coeff.Weight == 0 {
continue
}
data[index] = float32(vals[index] / coeff.Scale)
}
power, err := weights.XGBoostModel.PredictFromData(data)
if err != nil {
klog.Errorf("XGBoostModel.PredictFromData failed: %v", err)
return []float64{}
}
powers = append(powers, power[0])
}
default:
klog.Errorf("RegressorType %v is not supported", weights.RegressorType)
}
return powers
}
Expand Down
24 changes: 0 additions & 24 deletions pkg/model/estimator/local/xgboost_model_weight.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,27 +125,3 @@ func (m *XGBoostModelWeight) PredictFromData(data []float32) ([]float64, error)
C.free(unsafe.Pointer(output))
return output_array, nil
}

func (m XGBoostModelWeight) predict(usageMetricNames []string, usageMetricValues [][]float64, systemMetaDataFeatureNames, systemMetaDataFeatureValues []string) []float64 {
output := make([]float64, len(usageMetricValues))
// FIXME: how to use systmeMetaDataFeatureNames and systemMetaDataFeatureValues?
for i, usageMetricValue := range usageMetricValues {
if m.num_features != len(usageMetricValue) {
panic("xgboost model features and usageMetricValues length not equal")
}
// TODO we should really make a float32 array since xgboost only support float32
data := make([]float32, len(usageMetricValue))
for i, v := range usageMetricValue {
data[i] = float32(v)
}
val, err := m.PredictFromData(data)
if err != nil {
panic(err)
}
if len(val) != 1 {
panic("xgboost model predict length not equal to 1")
}
output[i] = val[0]
}
return output
}

0 comments on commit 90cc7d5

Please sign in to comment.