25
25
import os
26
26
27
27
28
- def predict (project_id , compute_region , model_display_name , inputs , feature_importance = None ):
28
+ def predict (
29
+ project_id ,
30
+ compute_region ,
31
+ model_display_name ,
32
+ inputs ,
33
+ feature_importance = None ,
34
+ ):
29
35
"""Make a prediction."""
30
36
# [START automl_tables_predict]
31
37
# TODO(developer): Uncomment and set the following variables
@@ -38,13 +44,16 @@ def predict(project_id, compute_region, model_display_name, inputs, feature_impo
38
44
39
45
client = automl .TablesClient (project = project_id , region = compute_region )
40
46
41
- params = {}
42
47
if feature_importance :
43
- params = {"feature_importance" : feature_importance }
44
-
45
- response = client .predict (
46
- model_display_name = model_display_name , inputs = inputs , params = params
47
- )
48
+ response = client .predict (
49
+ model_display_name = model_display_name ,
50
+ inputs = inputs ,
51
+ feature_importance = True ,
52
+ )
53
+ else :
54
+ response = client .predict (
55
+ model_display_name = model_display_name , inputs = inputs
56
+ )
48
57
49
58
print ("Prediction results:" )
50
59
for result in response .payload :
@@ -53,20 +62,21 @@ def predict(project_id, compute_region, model_display_name, inputs, feature_impo
53
62
)
54
63
print ("Predicted class score: {}" .format (result .tables .score ))
55
64
56
- # get features of top importance
57
- feat_list = [
58
- (column .feature_importance , column .column_display_name )
59
- for column in result .tables .tables_model_column_info
60
- ]
61
- feat_list .sort (reverse = True )
62
- if len (feat_list ) < 10 :
63
- feat_to_show = len (feat_list )
64
- else :
65
- feat_to_show = 10
66
-
67
- print ("Features of top importance:" )
68
- for feat in feat_list [:feat_to_show ]:
69
- print (feat )
65
+ if feature_importance :
66
+ # get features of top importance
67
+ feat_list = [
68
+ (column .feature_importance , column .column_display_name )
69
+ for column in result .tables .tables_model_column_info
70
+ ]
71
+ feat_list .sort (reverse = True )
72
+ if len (feat_list ) < 10 :
73
+ feat_to_show = len (feat_list )
74
+ else :
75
+ feat_to_show = 10
76
+
77
+ print ("Features of top importance:" )
78
+ for feat in feat_list [:feat_to_show ]:
79
+ print (feat )
70
80
71
81
# [END automl_tables_predict]
72
82
0 commit comments