@@ -1200,9 +1200,10 @@ def _no_targets_no_thresholds(
1200
1200
cls .score_code += (
1201
1201
f"{ '' :8} { metrics [i + 1 ]} = float(prediction[1][{ i + 1 } ])\n "
1202
1202
)
1203
+ metric_list = '"' + '","' .join (metrics ) + '"'
1203
1204
cls .score_code += (
1204
1205
f"{ '' :4} else:\n "
1205
- f"{ '' :8} output_table = pd.DataFrame(prediction[1:], columns=[{ ',' . join ( metrics ) } ])\n "
1206
+ f"{ '' :8} output_table = pd.DataFrame(prediction[1:], columns=[{ metric_list } ])\n "
1206
1207
f"{ '' :8} return output_table\n "
1207
1208
)
1208
1209
"""
@@ -1218,10 +1219,10 @@ def _no_targets_no_thresholds(
1218
1219
cls .score_code += f"{ '' :4} if input_array.shape[0] == 1"
1219
1220
for i in range (len (metrics )):
1220
1221
cls .score_code += f"{ '' :8} { metrics [i ]} = prediction[{ i } ]\n "
1221
- #TODO: What is the use case for this? unsure of how batched scoring would work here
1222
+ metric_list = '"' + '","' . join ( metrics ) + '"'
1222
1223
cls .score_code += (
1223
1224
f"{ '' :4} else:\n "
1224
- f"{ '' :8} output_table = pd.DataFrame(prediction, columns=[{ ',' . join ( metrics ) } ])\n "
1225
+ f"{ '' :8} output_table = pd.DataFrame(prediction, columns=[{ metric_list } ])\n "
1225
1226
f"{ '' :8} return output_table\n "
1226
1227
)
1227
1228
cls .score_code += f"\n { '' :4} return { ', ' .join (metrics )} "
@@ -1378,11 +1379,12 @@ def _binary_target(
1378
1379
"score code should output the classification and probability for "
1379
1380
"the target event to occur."
1380
1381
)
1382
+ metric_list = '"' + '","drop","' .join (metrics ) + '"'
1381
1383
cls .score_code += (
1382
1384
f"{ '' :4} if input_array.shape[0] == 1:\n "
1383
1385
f"{ '' :8} return prediction[1][0], float(prediction[1][2])"
1384
1386
f"{ '' :4} else:\n "
1385
- f"{ '' :8} output_table = pd.DataFrame(prediction[1:], columns=[{ ',drop,' . join ( metrics ) } ])\n "
1387
+ f"{ '' :8} output_table = pd.DataFrame(prediction[1:], columns=[{ metric_list } ])\n "
1386
1388
f"{ '' :8} return output_table.drop('drop', axis=1)"
1387
1389
)
1388
1390
"""
@@ -1442,11 +1444,12 @@ def _binary_target(
1442
1444
"""
1443
1445
# Return classification and probability value
1444
1446
elif sum (returns ) == 1 and len (returns ) == 2 :
1447
+ metric_list = '"' + '","' .join (metrics ) + '"'
1445
1448
cls .score_code += (
1446
1449
f"{ '' :4} if input_array.shape[0] == 1:\n "
1447
1450
f"{ '' :8} return prediction[0], prediction[1]\n "
1448
1451
f"{ '' :4} else:\n "
1449
- f"{ '' :8} return pd.DataFrame(prediction, columns=[{ ',' . join ( metrics ) } ])" )
1452
+ f"{ '' :8} return pd.DataFrame(prediction, columns=[{ metric_list } ])" )
1450
1453
"""
1451
1454
return prediction[0], prediction[1]
1452
1455
"""
@@ -1459,22 +1462,24 @@ def _binary_target(
1459
1462
# Determine which return is the classification value
1460
1463
class_index = [i for i , x in enumerate (returns ) if x ][0 ]
1461
1464
if class_index == 0 :
1465
+ metric_list = '"' + '","' .join (metrics ) + '","drop"'
1462
1466
cls .score_code += (
1463
1467
f"{ '' :4} if input_array.shape[0] == 1:\n "
1464
1468
f"{ '' :8} return prediction[0], prediction[1]\n "
1465
1469
f"{ '' :4} else:\n "
1466
- f"{ '' :8} output_table = pd.DataFrame(prediction, columns=[{ ',' . join ( metrics ) } ,drop ])\n "
1470
+ f"{ '' :8} output_table = pd.DataFrame(prediction, columns=[{ metric_list } ])\n "
1467
1471
f"{ '' :8} return output_table.drop('drop', axis=1)" )
1468
1472
1469
1473
"""
1470
1474
return prediction[0], prediction[1]
1471
1475
"""
1472
1476
else :
1477
+ metric_list = '"' + '","drop","' .join (metrics [::- 1 ]) + '"'
1473
1478
cls .score_code += (
1474
1479
f"{ '' :4} if input_array.shape[0] == 1:\n "
1475
1480
f"{ '' :8} return prediction[{ class_index } ], prediction[0]\n "
1476
1481
f"{ '' :4} else:\n "
1477
- f"{ '' :8} output_table = pd.DataFrame(prediction, columns=[{ ',drop,' . join ( metrics [:: - 1 ]) } ])\n "
1482
+ f"{ '' :8} output_table = pd.DataFrame(prediction, columns=[{ metric_list } ])\n "
1478
1483
f"{ '' :8} return output_table.drop('drop', axis=1)" )
1479
1484
"""
1480
1485
return prediction[2], prediction[0]
@@ -1483,12 +1488,13 @@ def _binary_target(
1483
1488
cls ._invalid_predict_config ()
1484
1489
elif len (metrics ) == 3 :
1485
1490
if h2o_model :
1491
+ metric_list = '"' + '","' .join (metrics ) + '"'
1486
1492
cls .score_code += (
1487
1493
f"{ '' :4} if input_array.shape[0] == 1:\n "
1488
1494
f"{ '' :8} return prediction[1][0], float(prediction[1][1]), "
1489
1495
f"float(prediction[1][2])\n "
1490
1496
f"{ '' :4} else:\n "
1491
- f"{ '' :8} return pd.DataFrame(prediction[1:], columns=[{ ',' . join ( metrics ) } ])"
1497
+ f"{ '' :8} return pd.DataFrame(prediction[1:], columns=[{ metric_list } ])"
1492
1498
)
1493
1499
"""
1494
1500
return prediction[1][0], float(prediction[1][1]), float(prediction[1][2])
@@ -1525,6 +1531,7 @@ def _binary_target(
1525
1531
" types, the score code assumes the return order to be: "
1526
1532
"[classification, probability of event, probability of no event]."
1527
1533
)
1534
+ metric_list = '"' + '","' .join (metrics [1 :]) + '"'
1528
1535
cls .score_code += (
1529
1536
f"{ '' :4} if input_array.shape[0] == 1:\n "
1530
1537
f"{ '' :8} if prediction[0] > prediction[1]:\n "
@@ -1534,7 +1541,7 @@ def _binary_target(
1534
1541
f"{ '' :8} return { metrics [0 ]} , prediction[0], prediction[1]\n "
1535
1542
f"{ '' :4} else:\n "
1536
1543
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 "
1544
+ f"{ '' :8} output_table = pd.DataFrame(prediction, columns=[{ metric_list } ])\n "
1538
1545
f"{ '' :8} output_table = output_table.insert(loc = 0, column = '{ metrics [0 ]} ', value = classifications)\n "
1539
1546
f"{ '' :8} return output_table"
1540
1547
)
@@ -1551,25 +1558,27 @@ def _binary_target(
1551
1558
# Determine which return is the classification value
1552
1559
class_index = [i for i , x in enumerate (returns ) if x ][0 ]
1553
1560
if class_index == 0 :
1561
+ metric_list = '"' + '","' .join (metrics [:2 ]) + '"'
1554
1562
cls .score_code += (
1555
1563
f"{ '' :4} if input_array.shape[0] == 1:\n "
1556
1564
f"{ '' :8} return prediction[0], prediction[1], 1 - prediction[1]\n "
1557
1565
f"{ '' :4} else:\n "
1558
1566
f"{ '' :8} complement = [1 - p[1] for p in prediction]\n "
1559
- f"{ '' :8} output_table = pd.DataFrame(prediction, columns=[{ ',' . join ( metrics [: 2 ]) } ])\n "
1567
+ f"{ '' :8} output_table = pd.DataFrame(prediction, columns=[{ metric_list } ])\n "
1560
1568
f"{ '' :8} output_table = output_table.insert(loc = 2, column = '{ metrics [2 ]} ', value = complement)\n "
1561
1569
f"{ '' :8} return output_table"
1562
1570
)
1563
1571
"""
1564
1572
return prediction[0], prediction[1], 1 - prediction[1]
1565
1573
"""
1566
1574
else :
1575
+ metric_list = '"' + '","' .join (metrics [1 ::- 1 ]) + '"'
1567
1576
cls .score_code += (
1568
1577
f"{ '' :4} if input_array.shape[0] == 1:\n "
1569
1578
f"{ '' :8} return prediction[1], prediction[0], 1 - prediction[0]\n "
1570
1579
f"{ '' :4} else:\n "
1571
1580
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 "
1581
+ f"{ '' :8} output_table = pd.DataFrame(prediction, columns=[{ metric_list } ])\n "
1573
1582
f"{ '' :8} output_table = output_table.insert(loc = 2, column = '{ metrics [2 ]} ', value = complement)\n "
1574
1583
f"{ '' :8} return output_table"
1575
1584
)
@@ -1578,11 +1587,12 @@ def _binary_target(
1578
1587
"""
1579
1588
# Return all values from prediction method
1580
1589
elif sum (returns ) == 1 and len (returns ) == 3 :
1590
+ metric_list = '"' + '","' .join (metrics ) + '"'
1581
1591
cls .score_code += (
1582
1592
f"{ '' :4} if input_array.shape[0] == 1:\n "
1583
1593
f"{ '' :8} return prediction[0], prediction[1], prediction[2]"
1584
1594
f"{ '' :4} else:\n "
1585
- f"{ '' :8} output_table = pd.DataFrame(prediction, columns=[{ ',' . join ( metrics ) } ])"
1595
+ f"{ '' :8} output_table = pd.DataFrame(prediction, columns=[{ metric_list } ])"
1586
1596
)
1587
1597
"""
1588
1598
return prediction[0], prediction[1], prediction[2]
@@ -1628,10 +1638,14 @@ def _nonbinary_targets(
1628
1638
# For h2o models with only one metric provided, return the classification
1629
1639
if h2o_model :
1630
1640
cls .score_code += (
1631
- f"{ '' :4} target_values = { target_values } \n { '' :4} "
1632
- f"{ metrics } = target_values[prediction[1][1:]."
1633
- f"index(max(prediction[1][1:]))]\n \n "
1634
- f"{ '' :4} return { metrics } "
1641
+ f"{ '' :4} target_values = { target_values } \n "
1642
+ f"{ '' :4} if input_array.shape[0] == 1:\n "
1643
+ f"{ '' :8} { metrics } = target_values[prediction[1][1:]."
1644
+ f"index(max(prediction[1][1:]))]\n "
1645
+ f"{ '' :8} return { metrics } \n "
1646
+ f"{ '' :4} else:\n "
1647
+ f"{ '' :8} classifications = [target_values[np.argmax(p[1:])[0]] for p in prediction[1:]]\n "
1648
+ f"{ '' :8} return pd.DataFrame({{'{ metrics } ': classifications}})"
1635
1649
)
1636
1650
"""
1637
1651
target_values = [1, 2, 3]
@@ -1641,16 +1655,25 @@ def _nonbinary_targets(
1641
1655
"""
1642
1656
# One return that is the classification
1643
1657
elif len (returns ) == 1 :
1644
- cls .score_code += f"{ '' :4} { metrics } = prediction\n \n return { metrics } "
1658
+ cls .score_code += (
1659
+ f"{ '' :4} if input_array.shape[0] == 1:\n "
1660
+ f"{ '' :8} { metrics } = prediction\n \n return { metrics } \n "
1661
+ f"{ '' :4} else:\n "
1662
+ f"{ '' :8} return pd.DataFrame('{ metrics } ': prediction)"
1663
+ )
1645
1664
"""
1646
1665
classification_variable = prediction
1647
1666
1648
1667
return classification_variable
1649
1668
"""
1650
1669
elif len (returns ) == len (target_values ):
1651
1670
cls .score_code += (
1652
- f"{ '' :4} target_values = { target_values } \n \n "
1653
- f"{ '' :4} return target_values[prediction.index(max(prediction))]"
1671
+ f"{ '' :4} if input_array.shape[0] == 1:\n "
1672
+ f"{ '' :8} target_values = { target_values } \n "
1673
+ f"{ '' :8} return target_values[prediction.index(max(prediction))]\n "
1674
+ f"{ '' :4} else:\n "
1675
+ f"{ '' :8} classifications = [target_values[np.argmax(p)[0]] for p in prediction]\n "
1676
+ f"{ '' :8} return pd.DataFrame('{ metrics } ': classifications)"
1654
1677
)
1655
1678
"""
1656
1679
target_values = [1, 2, 3]
@@ -1660,7 +1683,12 @@ def _nonbinary_targets(
1660
1683
elif len (returns ) == (len (target_values ) + 1 ):
1661
1684
# Determine which return is the classification value
1662
1685
class_index = [i for i , x in enumerate (returns ) if x ][0 ]
1663
- cls .score_code += f"{ '' :4} return prediction[{ class_index } ]"
1686
+ cls .score_code += (
1687
+ f"{ '' :4} if input_array.shape[0] == 1:\n "
1688
+ f"{ '' :8} return prediction[{ class_index } ]"
1689
+ f"{ '' :4} else:\n "
1690
+ f"{ '' :8} return pd.DataFrame('{ metrics } ': [p[{ class_index } ] for p in prediction])"
1691
+ )
1664
1692
"""
1665
1693
return prediction[1]
1666
1694
"""
@@ -1669,10 +1697,15 @@ def _nonbinary_targets(
1669
1697
elif len (metrics ) == 2 :
1670
1698
if h2o_model :
1671
1699
cls .score_code += (
1672
- f"{ '' :4} target_values = { target_values } \n { '' :4} "
1673
- f"{ metrics } = target_values[prediction[1][1:]."
1674
- f"index(max(prediction[1][1:]))]\n \n "
1675
- f"{ '' :4} return { metrics } , max(prediction[1][1:])"
1700
+ f"{ '' :4} target_values = { target_values } \n "
1701
+ f"{ '' :4} if input_array.shape[0] == 1:\n "
1702
+ f"{ '' :8} { metrics } = target_values[prediction[1][1:]."
1703
+ f"index(max(prediction[1][1:]))]\n "
1704
+ f"{ '' :8} return { metrics } , max(prediction[1][1:])\n "
1705
+ f"{ '' :4} else:\n "
1706
+ f"{ '' :8} classifications = [target_values[np.argmax(p[1:])[0]] for p in prediction[1:]]\n "
1707
+ f"{ '' :8} max_proba = [max(p[1:]) for p in prediction[1:]]\n "
1708
+ f"{ '' :8} return pd.DataFrame({{'{ metrics [0 ]} ': classifications, '{ metrics [1 ]} ': max_proba}})"
1676
1709
)
1677
1710
"""
1678
1711
target_values = [1, 2, 3]
@@ -1682,9 +1715,14 @@ def _nonbinary_targets(
1682
1715
"""
1683
1716
elif len (returns ) == len (target_values ):
1684
1717
cls .score_code += (
1685
- f"{ '' :4} target_values = { target_values } \n \n "
1686
- f"{ '' :4} return target_values[prediction.index(max(prediction))], "
1687
- f"max(prediction)"
1718
+ f"{ '' :4} target_values = { target_values } \n "
1719
+ f"{ '' :4} if input_array.shape[0] == 1:\n "
1720
+ f"{ '' :8} return target_values[prediction.index(max(prediction))], "
1721
+ f"max(prediction)\n "
1722
+ f"{ '' :4} else:\n "
1723
+ f"{ '' :8} classifications = [target_values[np.argmax(p)[0]] for p in prediction]\n "
1724
+ f"{ '' :8} max_proba = [max(p) for p in prediction]\n "
1725
+ f"{ '' :8} return pd.DataFrame({{'{ metrics [0 ]} ': classifications, '{ metrics [1 ]} ': max_proba}})"
1688
1726
)
1689
1727
"""
1690
1728
target_values = [1, 2, 3]
@@ -1695,8 +1733,13 @@ def _nonbinary_targets(
1695
1733
# Determine which return is the classification value
1696
1734
class_index = [i for i , x in enumerate (returns ) if x ][0 ]
1697
1735
cls .score_code += (
1698
- f"{ '' :4} return prediction[{ class_index } ], "
1699
- f"max(prediction[:{ class_index } ] + prediction[{ class_index + 1 } :])"
1736
+ f"{ '' :4} if input_array.shape[0] == 1:\n "
1737
+ f"{ '' :8} return prediction[{ class_index } ], "
1738
+ f"max(prediction[:{ class_index } ] + prediction[{ class_index + 1 } :])\n "
1739
+ f"{ '' :4} else:\n "
1740
+ f"{ '' :8} max_proba = [max(p[:{ class_index } ] + p[{ class_index + 1 } :]) for p in prediction]\n "
1741
+ f"{ '' :8} classifications = [p[{ class_index } ] for p in prediction]\n "
1742
+ f"{ '' :8} return pd.DataFrame({{'{ metrics [0 ]} ': classifications, '{ metrics [1 ]} ': nax_proba}})"
1700
1743
)
1701
1744
"""
1702
1745
return prediction[1], max(prediction[:1] + prediction[2:])
@@ -1707,13 +1750,26 @@ def _nonbinary_targets(
1707
1750
if h2o_model :
1708
1751
if len (metrics ) == len (target_values ):
1709
1752
h2o_returns = [f"prediction[1][{ i + 1 } ]" for i in range (len (metrics ))]
1710
- cls .score_code += f"{ '' :4} return { ', ' .join (h2o_returns )} "
1753
+ metric_list = '"' + '","' .join (metrics ) + '"'
1754
+ cls .score_code += (
1755
+ f"{ '' :4} if input_array.shape[0] == 1:\n "
1756
+ f"{ '' :8} return { ', ' .join (h2o_returns )} \n "
1757
+ f"{ '' :4} else:\n "
1758
+ f"{ '' :8} output_table = pd.DataFrame(prediction[1:], columns=[{ metric_list } ])"
1759
+ f"{ '' :8} output_table = output_table.drop('{ metrics [0 ]} ', axis=1)"
1760
+ f"{ '' :8} return output_table"
1761
+ )
1711
1762
"""
1712
1763
return prediction[1][1], prediction[1][2], prediction[1][3]
1713
1764
"""
1714
1765
elif len (metrics ) == (len (target_values ) + 1 ):
1715
1766
h2o_returns = [f"prediction[1][{ i } ]" for i in range (len (metrics ))]
1716
- cls .score_code += f"{ '' :4} return { ', ' .join (h2o_returns )} "
1767
+ cls .score_code += (
1768
+ f"{ '' :4} if input_array.shape[0] == 1:\n "
1769
+ f"{ '' :8} return { ', ' .join (h2o_returns )} \n "
1770
+ f"{ '' :4} else:\n "
1771
+ f"{ '' :8} output_table = pd.DataFrame(prediction[1:], columns=[])"
1772
+ )
1717
1773
"""
1718
1774
return prediction[1][0], prediction[1][1], prediction[1][2]
1719
1775
"""
@@ -1724,7 +1780,13 @@ def _nonbinary_targets(
1724
1780
and sum (returns ) == 1
1725
1781
):
1726
1782
proba_returns = [f"prediction[{ i } ]" for i in range (len (returns ))]
1727
- cls .score_code += f"{ '' :4} return { ', ' .join (proba_returns )} "
1783
+ metric_list = '"' + '","' .join (metrics ) + '"'
1784
+ cls .score_code += (
1785
+ f"{ '' :4} if input_array.shape[0] == 1:\n "
1786
+ f"{ '' :8} return { ', ' .join (proba_returns )} \n "
1787
+ f"{ '' :4} else:\n "
1788
+ f"{ '' :8} output_table = pd.DataFrame(prediction, columns=[{ metric_list } ])"
1789
+ )
1728
1790
"""
1729
1791
return prediction[0], prediction[1], prediction[2]
1730
1792
"""
@@ -1734,8 +1796,13 @@ def _nonbinary_targets(
1734
1796
proba_returns = [f"prediction[{ i } ]" for i in range (len (returns ))]
1735
1797
cls .score_code += (
1736
1798
f"{ '' :4} target_values = { target_values } \n \n "
1737
- f"{ '' :4} return target_values[prediction.index(max(prediction))], "
1738
- f"{ ', ' .join (proba_returns )} "
1799
+ f"{ '' :4} if input_array.shape[0] == 1:\n "
1800
+ f"{ '' :8} return target_values[prediction.index(max(prediction))], "
1801
+ f"{ ', ' .join (proba_returns )} \n "
1802
+ f"{ '' :8} classifications = [target_values[np.argmax(p)[0]] for p in prediction]\n "
1803
+ f"{ '' :8} output_table = pd.DataFrame(prediction, columns={ metrics [1 :]} )"
1804
+ f"{ '' :8} output_table = output_table.insert(loc=0, column={ metrics [0 ]} , data=classifications)"
1805
+ f"{ '' :8} return output_table"
1739
1806
)
1740
1807
"""
1741
1808
target_values = [1, 2, 3]
0 commit comments