Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions sql/codegen_analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@ import (
"strings"
)

const (
shapSummaryAttributePrefix = "shap_summary"
)

type analyzeFiller struct {
*connectionConfig
X []*FeatureMeta
Label string
AnalyzeDatasetSQL string
PlotType string
ModelFile string // path/to/model_file
X []*FeatureMeta
Label string
AnalyzeDatasetSQL string
PlotType string
ShapSummaryParames map[string]interface{}
ModelFile string // path/to/model_file
}

func newAnalyzeFiller(pr *extendedSelect, db *DB, fms []*FeatureMeta, label, modelPath, plotType string) (*analyzeFiller, error) {
func newAnalyzeFiller(pr *extendedSelect, db *DB, fms []*FeatureMeta, label, modelPath string, summaryAttrs map[string]interface{}) (*analyzeFiller, error) {
conn, err := newConnectionConfig(db)
if err != nil {
return nil, err
Expand All @@ -39,9 +44,9 @@ func newAnalyzeFiller(pr *extendedSelect, db *DB, fms []*FeatureMeta, label, mod
Label: label,
// TODO(weiguo): test if it needs TrimSuffix(SQL, ";") on hive,
// or we trim it in pr(*extendedSelect)
AnalyzeDatasetSQL: pr.standardSelect.String(),
ModelFile: modelPath,
PlotType: plotType,
AnalyzeDatasetSQL: pr.standardSelect.String(),
ModelFile: modelPath,
ShapSummaryParames: summaryAttrs,
}, nil
}

Expand Down Expand Up @@ -71,13 +76,19 @@ func readXGBFeatures(pr *extendedSelect, db *DB) ([]*FeatureMeta, string, error)
return xs, fr.Y.FeatureName, nil
}

func readPlotType(pr *extendedSelect) string {
v, ok := pr.analyzeAttrs["shap.plot_type"]
if !ok {
// using shap default value
return `""`
func resolveAnalyzeSummaryParames(atts *attrs) (map[string]interface{}, error) {
parames, err := resolveAttribute(atts)
if err != nil {
return nil, err
}
return v.val

summaryAttrs := make(map[string]interface{})
for _, v := range parames {
if v.Prefix == shapSummaryAttributePrefix {
summaryAttrs[v.Name] = v.Value
}
}
return summaryAttrs, nil
}

func genAnalyzer(pr *extendedSelect, db *DB, cwd, modelDir string) (*bytes.Buffer, error) {
Expand All @@ -89,13 +100,17 @@ func genAnalyzer(pr *extendedSelect, db *DB, cwd, modelDir string) (*bytes.Buffe
return nil, fmt.Errorf("analyzer: model[%s] not supported", pr.estimator)
}
// We untar the XGBoost.{pr.trainedModel}.tar.gz and get three files.
plotType := readPlotType(pr)
summaryAttrs, err := resolveAnalyzeSummaryParames(&pr.analyzeAttrs)
if err != nil {
return nil, err
}

xs, label, err := readXGBFeatures(pr, db)
if err != nil {
return nil, err
}

fr, err := newAnalyzeFiller(pr, db, xs, label, pr.trainedModel, plotType)
fr, err := newAnalyzeFiller(pr, db, xs, label, pr.trainedModel, summaryAttrs)
if err != nil {
return nil, fmt.Errorf("create analyze filler failed: %v", err)
}
Expand Down
10 changes: 6 additions & 4 deletions sql/template_analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ def analyzer_dataset():

# 2. load the model
model_path = "{{.ModelFile}}"
ptype = {{.PlotType}}
if len(ptype) == 0:
ptype = None

summaryAttrs = {}
{{ range $k, $v := .ShapSummaryParames }}
summaryAttrs["{{$k}}"] = {{$v}}
{{end}}

X,y = analyzer_dataset()

Expand All @@ -77,7 +79,7 @@ bst.load_model(fname=model_path)
explainer = shap.TreeExplainer(bst)
shap_values = explainer.shap_values(X)

shap.summary_plot(shap_values, X, plot_type=ptype)
shap.summary_plot(shap_values, X, **summaryAttrs)
plt.savefig('summary')
`

Expand Down