Skip to content

Commit b838897

Browse files
committed
binary targets updated for batch scoring
1 parent dfe033c commit b838897

File tree

1 file changed

+50
-25
lines changed

1 file changed

+50
-25
lines changed

src/sasctl/pzmm/write_score_code.py

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ def _binary_target(
12811281
# For h2o models with only one metric provided, return the classification
12821282
if h2o_model:
12831283
cls.score_code += (
1284-
f"{'':4}if input_array.shape[0] == 1\n"
1284+
f"{'':4}if input_array.shape[0] == 1:\n"
12851285
f"{'':8}if prediction[1][2] > {threshold}:\n"
12861286
f"{'':12}{metrics} = \"{target_values[0]}\"\n"
12871287
f"{'':8}else:\n"
@@ -1303,7 +1303,7 @@ def _binary_target(
13031303
# One return that is the classification
13041304
elif len(returns) == 1 and returns[0]:
13051305
cls.score_code += (
1306-
f"{'':4}if input_array.shape[0] == 1\n"
1306+
f"{'':4}if input_array.shape[0] == 1:\n"
13071307
f"{'':8}return prediction\n"
13081308
f"{'':4}else:\n"
13091309
f"{'':8}return pd.DataFrame({{'{metrics}': prediction}})")
@@ -1313,7 +1313,7 @@ def _binary_target(
13131313
# One return that is a probability
13141314
elif len(returns) == 1 and not returns[0]:
13151315
cls.score_code += (
1316-
f"{'':4}if input_array.shape[0] == 1\n"
1316+
f"{'':4}if input_array.shape[0] == 1:\n"
13171317
f"{'':8}if prediction > {threshold}:\n"
13181318
f"{'':12}{metrics} = \"{target_values[0]}\"\n"
13191319
f"{'':8}else:\n"
@@ -1334,7 +1334,7 @@ def _binary_target(
13341334
elif len(returns) == 2 and sum(returns) == 0:
13351335
# Only probabilities returned; return classification for larger value
13361336
cls.score_code += (
1337-
f"{'':4}if input_array.shape[0] == 1\n"
1337+
f"{'':4}if input_array.shape[0] == 1:\n"
13381338
f"{'':8}if prediction[0] > prediction[1]:\n"
13391339
f"{'':12}{metrics} = \"{target_values[0]}\"\n"
13401340
f"{'':8}else:\n"
@@ -1357,7 +1357,7 @@ def _binary_target(
13571357
# Determine which return is the classification value
13581358
class_index = [i for i, x in enumerate(returns) if x][0]
13591359
cls.score_code += (
1360-
f"{'':4}if input_array.shape[0] == 1\n"
1360+
f"{'':4}if input_array.shape[0] == 1:\n"
13611361
f"{'':8}{metrics} = prediction[{class_index}]\n\nreturn {metrics}\n"
13621362
f"{'':4}else:\n"
13631363
f"{'':8}output_table = pd.DataFrame({{'{metrics}': [p[{class_index}] for p in prediction]}})\n"
@@ -1379,7 +1379,7 @@ def _binary_target(
13791379
"the target event to occur."
13801380
)
13811381
cls.score_code += (
1382-
f"{'':4}if input_array.shape[0] == 1\n"
1382+
f"{'':4}if input_array.shape[0] == 1:\n"
13831383
f"{'':8}return prediction[1][0], float(prediction[1][2])"
13841384
f"{'':4}else:\n"
13851385
f"{'':8}output_table = pd.DataFrame(prediction[1:], columns=[{',drop,'.join(metrics)}])\n"
@@ -1396,7 +1396,7 @@ def _binary_target(
13961396
"metric is returned first."
13971397
)
13981398
cls.score_code += (
1399-
f"{'':4}if input_array.shape[0] == 1\n"
1399+
f"{'':4}if input_array.shape[0] == 1:\n"
14001400
f"{'':8}if prediction > {threshold}:\n"
14011401
f"{'':12}{metrics[0]} = \"{target_values[0]}\"\n"
14021402
f"{'':8}else:\n"
@@ -1422,7 +1422,7 @@ def _binary_target(
14221422
"target event probability should be returned."
14231423
)
14241424
cls.score_code += (
1425-
f"{'':4}if input_array.shape[0] == 1\n"
1425+
f"{'':4}if input_array.shape[0] == 1:\n"
14261426
f"{'':8}if prediction[0] > prediction[1]:\n"
14271427
f"{'':12}{metrics[0]} = \"{target_values[0]}\"\n"
14281428
f"{'':8}else:\n"
@@ -1443,7 +1443,7 @@ def _binary_target(
14431443
# Return classification and probability value
14441444
elif sum(returns) == 1 and len(returns) == 2:
14451445
cls.score_code += (
1446-
f"{'':4}if input_array.shape[0] == 1\n"
1446+
f"{'':4}if input_array.shape[0] == 1:\n"
14471447
f"{'':8}return prediction[0], prediction[1]\n"
14481448
f"{'':4}else:\n"
14491449
f"{'':8}return pd.DataFrame(prediction, columns=[{','.join(metrics)}])")
@@ -1460,7 +1460,7 @@ def _binary_target(
14601460
class_index = [i for i, x in enumerate(returns) if x][0]
14611461
if class_index == 0:
14621462
cls.score_code += (
1463-
f"{'':4}if input_array.shape[0] == 1\n"
1463+
f"{'':4}if input_array.shape[0] == 1:\n"
14641464
f"{'':8}return prediction[0], prediction[1]\n"
14651465
f"{'':4}else:\n"
14661466
f"{'':8}output_table = pd.DataFrame(prediction, columns=[{','.join(metrics)},drop])\n"
@@ -1471,7 +1471,7 @@ def _binary_target(
14711471
"""
14721472
else:
14731473
cls.score_code += (
1474-
f"{'':4}if input_array.shape[0] == 1\n"
1474+
f"{'':4}if input_array.shape[0] == 1:\n"
14751475
f"{'':8}return prediction[{class_index}], prediction[0]\n"
14761476
f"{'':4}else:\n"
14771477
f"{'':8}output_table = pd.DataFrame(prediction, columns=[{',drop,'.join(metrics[::-1])}])\n"
@@ -1484,7 +1484,7 @@ def _binary_target(
14841484
elif len(metrics) == 3:
14851485
if h2o_model:
14861486
cls.score_code += (
1487-
f"{'':4}if input_array.shape[0] == 1\n"
1487+
f"{'':4}if input_array.shape[0] == 1:\n"
14881488
f"{'':8}return prediction[1][0], float(prediction[1][1]), "
14891489
f"float(prediction[1][2])\n"
14901490
f"{'':4}else:\n"
@@ -1500,12 +1500,16 @@ def _binary_target(
15001500
"[classification, probability of event, probability of no event]."
15011501
)
15021502
cls.score_code += (
1503-
f"{'':4}if prediction > {threshold}:\n"
1504-
f"{'':8}{metrics[0]} = \"{target_values[0]}\"\n"
1503+
f"{'':4}if input_array.shape[0] == 1:\n"
1504+
f"{'':8}if prediction > {threshold}:\n"
1505+
f"{'':12}{metrics[0]} = \"{target_values[0]}\"\n"
1506+
f"{'':8}else:\n"
1507+
f"{'':12}{metrics[0]} = \"{target_values[1]}\"\n"
1508+
f"{'':8}return {metrics[0]}, prediction, 1 - prediction\n"
15051509
f"{'':4}else:\n"
1506-
f"{'':8}{metrics[0]} = \"{target_values[1]}\"\n\n"
1507-
f"{'':4}return {metrics[0]}, prediction, 1 - prediction"
1508-
1510+
f"{'':8}classifications = ['{target_values[0]}' if p > {threshold} else '{target_values[1]}' for p in prediction]\n"
1511+
f"{'':8}complement = [1 - p for p in prediction]\n"
1512+
f"{'':8}return pd.DataFrame({{'{metrics[0]}': classifications, '{metrics[1]}': prediction, '{metrics[2]}': complement}})"
15091513
)
15101514
"""
15111515
if prediction > 0.5:
@@ -1522,14 +1526,20 @@ def _binary_target(
15221526
"[classification, probability of event, probability of no event]."
15231527
)
15241528
cls.score_code += (
1525-
f"{'':4}if prediction[0] > prediction[1]:\n"
1526-
f"{'':8}{metrics[0]} = \"{target_values[0]}\"\n"
1529+
f"{'':4}if input_array.shape[0] == 1:\n"
1530+
f"{'':8}if prediction[0] > prediction[1]:\n"
1531+
f"{'':12}{metrics[0]} = \"{target_values[0]}\"\n"
1532+
f"{'':8}else:\n"
1533+
f"{'':12}{metrics[0]} = \"{target_values[1]}\"\n"
1534+
f"{'':8}return {metrics[0]}, prediction[0], prediction[1]\n"
15271535
f"{'':4}else:\n"
1528-
f"{'':8}{metrics[0]} = \"{target_values[1]}\"\n\n"
1529-
f"{'':4}return {metrics[0]}, prediction[0], prediction[1]"
1536+
f"{'':8}classifications = ['{target_values[0]}' if p[0] > p[1] else '{target_values[1]}' for p in prediction]\n"
1537+
f"{'':8}output_table = pd.DataFrame(prediction, columns=[{','.join(metrics[1:])}])\n"
1538+
f"{'':8}output_table = output_table.insert(loc = 0, column = '{metrics[0]}', value = classifications)\n"
1539+
f"{'':8}return output_table"
15301540
)
15311541
"""
1532-
if prediction > 0.5:
1542+
if prediction[0] > prediction[1]:
15331543
classification_variable = 1
15341544
else:
15351545
classification_variable = 0
@@ -1542,22 +1552,37 @@ def _binary_target(
15421552
class_index = [i for i, x in enumerate(returns) if x][0]
15431553
if class_index == 0:
15441554
cls.score_code += (
1545-
f"{'':4}return prediction[0], prediction[1], 1 - prediction[1]"
1555+
f"{'':4}if input_array.shape[0] == 1:\n"
1556+
f"{'':8}return prediction[0], prediction[1], 1 - prediction[1]\n"
1557+
f"{'':4}else:\n"
1558+
f"{'':8}complement = [1 - p[1] for p in prediction]\n"
1559+
f"{'':8}output_table = pd.DataFrame(prediction, columns=[{','.join(metrics[:2])}])\n"
1560+
f"{'':8}output_table = output_table.insert(loc = 2, column = '{metrics[2]}', value = complement)\n"
1561+
f"{'':8}return output_table"
15461562
)
15471563
"""
15481564
return prediction[0], prediction[1], 1 - prediction[1]
15491565
"""
15501566
else:
15511567
cls.score_code += (
1552-
f"{'':4}return prediction[1], prediction[0], 1 - prediction[0]"
1568+
f"{'':4}if input_array.shape[0] == 1:\n"
1569+
f"{'':8}return prediction[1], prediction[0], 1 - prediction[0]\n"
1570+
f"{'':4}else:\n"
1571+
f"{'':8}complement = [1 - p[0] for p in prediction]\n"
1572+
f"{'':8}output_table = pd.DataFrame(prediction, columns=[{','.join(metrics[1::-1])}])\n"
1573+
f"{'':8}output_table = output_table.insert(loc = 2, column = '{metrics[2]}', value = complement)\n"
1574+
f"{'':8}return output_table"
15531575
)
15541576
"""
15551577
return prediction[1], prediction[0], 1 - prediction[0]
15561578
"""
15571579
# Return all values from prediction method
15581580
elif sum(returns) == 1 and len(returns) == 3:
15591581
cls.score_code += (
1560-
f"{'':4}return prediction[0], prediction[1], prediction[2]"
1582+
f"{'':4}if input_array.shape[0] == 1:\n"
1583+
f"{'':8}return prediction[0], prediction[1], prediction[2]"
1584+
f"{'':4}else:\n"
1585+
f"{'':8}output_table = pd.DataFrame(prediction, columns=[{','.join(metrics)}])"
15611586
)
15621587
"""
15631588
return prediction[0], prediction[1], prediction[2]

0 commit comments

Comments
 (0)