Explain generates a Python program to explain a trained model.
(explainStmt *ir.ExplainStmt, session *pb.Session)
| 29 | |
| 30 | // Explain generates a Python program to explain a trained model. |
| 31 | func Explain(explainStmt *ir.ExplainStmt, session *pb.Session) (string, error) { |
| 32 | summaryParams := resolveParams(explainStmt.Attributes, shapSummaryAttrPrefix) |
| 33 | jsonSummary, err := json.Marshal(summaryParams) |
| 34 | if err != nil { |
| 35 | return "", err |
| 36 | } |
| 37 | |
| 38 | featureColumnCode, xs, y, err := deriveFeatureColumnCodeAndFieldDescs(explainStmt.TrainStmt.Features["feature_columns"], explainStmt.TrainStmt.Label) |
| 39 | if err != nil { |
| 40 | return "", err |
| 41 | } |
| 42 | f, fs, err := resolveFeatureMeta(xs) |
| 43 | if err != nil { |
| 44 | return "", err |
| 45 | } |
| 46 | |
| 47 | l, err := json.Marshal(resolveFieldMeta(&y)) |
| 48 | if err != nil { |
| 49 | return "", err |
| 50 | } |
| 51 | |
| 52 | fr := &explainFiller{ |
| 53 | DataSource: session.DbConnStr, |
| 54 | DatasetSQL: explainStmt.Select, |
| 55 | ShapSummaryParams: string(jsonSummary), |
| 56 | FeatureFieldMetaJSON: string(f), |
| 57 | FeatureColumnNames: fs, |
| 58 | FeatureColumnCode: featureColumnCode, |
| 59 | LabelJSON: string(l), |
| 60 | ResultTable: explainStmt.Into, |
| 61 | IsPAI: tf.IsPAI(), |
| 62 | PAIExplainTable: explainStmt.TmpExplainTable, |
| 63 | } |
| 64 | var analysis bytes.Buffer |
| 65 | if err := explainTemplate.Execute(&analysis, fr); err != nil { |
| 66 | return "", err |
| 67 | } |
| 68 | return analysis.String(), nil |
| 69 | } |
| 70 | |
| 71 | func resolveParams(attrs map[string]interface{}, group string) map[string]interface{} { |
| 72 | sp := make(map[string]interface{}) |