@@ -260,13 +260,13 @@ def score(var1, var2, var3, var4):
260
260
f"\n { '' :4} # Check for numpy values and convert to a CAS readable "
261
261
f"representation\n "
262
262
f"{ '' :4} if isinstance(prediction, np.ndarray):\n "
263
- f"{ '' :8} prediction = prediction.tolist()[0] \n \n "
263
+ f"{ '' :8} prediction = prediction.tolist()\n \n "
264
264
)
265
265
"""
266
266
267
267
# Check for numpy values and conver to a CAS readable representation
268
268
if isinstance(prediction, np.ndarray):
269
- prediction = prediction.tolist()[0]
269
+ prediction = prediction.tolist()
270
270
271
271
272
272
"""
@@ -1164,43 +1164,74 @@ def _no_targets_no_thresholds(
1164
1164
# Classification (with only classification output) or prediction model
1165
1165
if h2o_model :
1166
1166
cls .score_code += (
1167
- f"{ '' :4} { metrics } = prediction[1][0]\n \n { '' :4} return { metrics } "
1167
+ f"{ '' :4} if input_array.shape[0] == 1\n "
1168
+ f"{ '' :8} { metrics [0 ]} = prediction[1][0]\n \n { '' :8} return { metrics [0 ]} "
1169
+ f"{ '' :4} else:\n "
1170
+ f"{ '' :8} prediction = pd.Dataframe(prediction[1:], columns=prediction[0])\n "
1171
+ f"{ '' :8} { metrics [0 ]} = prediction['predict']\n \n "
1172
+ f"{ '' :8} return { metrics [0 ]} "
1168
1173
)
1169
1174
"""
1170
1175
output_variable = predictions[1][0]
1171
1176
return output_variable
1172
1177
"""
1173
1178
else :
1174
1179
cls .score_code += (
1175
- f"{ '' :4} { metrics } = prediction\n \n { '' :4} return { metrics } "
1180
+ f"{ '' :4} if input_array.shape[0] == 1\n "
1181
+ f"{ '' :8} { metrics } = prediction[0]\n \n { '' :8} return { metrics } "
1182
+ f"{ '' :4} else:\n "
1183
+ f"{ '' :8} output_table = pd.DataFrame('{ metrics } ': prediction)\n \n { '' :8} return output_table"
1176
1184
)
1177
1185
"""
1178
- output_variable = prediction
1179
- return output_variable
1186
+ if input_array.shape[0] == 1:
1187
+ output_variable = prediction[0]
1188
+ return output_variable
1189
+ else:
1190
+ output_table = pd.DataFrame('output_variable': prediction)
1191
+ return output_table
1180
1192
"""
1181
1193
else :
1182
1194
# Classification model including predictions and classification
1183
1195
if h2o_model :
1184
- cls .score_code += f"{ '' :4} { metrics [0 ]} = prediction[1][0]\n "
1196
+ cls .score_code += (
1197
+ f"{ '' :4} if input_array.shape[0] == 1\n "
1198
+ f"{ '' :8} { metrics [0 ]} = prediction[1][0]\n " )
1185
1199
for i in range (len (metrics ) - 1 ):
1186
1200
cls .score_code += (
1187
- f"{ '' :4 } { metrics [i + 1 ]} = float(prediction[1][{ i + 1 } ])\n "
1201
+ f"{ '' :8 } { metrics [i + 1 ]} = float(prediction[1][{ i + 1 } ])\n "
1188
1202
)
1203
+ cls .score_code += (
1204
+ f"{ '' :4} else:\n "
1205
+ f"{ '' :8} output_table = pd.DataFrame(prediction[1:], columns=[{ ',' .join (metrics )} ])\n "
1206
+ f"{ '' :8} return output_table\n "
1207
+ )
1189
1208
"""
1190
- classification_variable = prediction[1][0]
1191
- prediction_variable_1 = float(prediction[1][1])
1192
- prediction_variable_2 = float(prediction[1][2])
1209
+ if input_array.shape[0] == 1:
1210
+ classification_variable = prediction[1][0]
1211
+ prediction_variable_1 = float(prediction[1][1])
1212
+ prediction_variable_2 = float(prediction[1][2])
1213
+ else:
1214
+ output_table = pd.DataFrame(prediction[1:], columns=[classification_variable,variable_1,variable_2])
1215
+ return output_table
1193
1216
"""
1194
1217
else :
1218
+ cls .score_code += f"{ '' :4} if input_array.shape[0] == 1"
1195
1219
for i in range (len (metrics )):
1196
- cls .score_code += f"{ '' :4} { metrics [i ]} = prediction[{ i } ]\n "
1220
+ 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
+ cls .score_code += (
1223
+ f"{ '' :4} else:\n "
1224
+ f"{ '' :8} output_table = pd.DataFrame(prediction, columns=[{ ',' .join (metrics )} ])\n "
1225
+ f"{ '' :8} return output_table\n "
1226
+ )
1197
1227
cls .score_code += f"\n { '' :4} return { ', ' .join (metrics )} "
1198
1228
"""
1199
1229
classification_variable = prediction[0]
1200
1230
prediction_variable_1 = prediction[1]
1201
1231
prediction_variable_2 = prediction[2])
1202
1232
"""
1203
1233
1234
+
1204
1235
@classmethod
1205
1236
def _binary_target (
1206
1237
cls ,
@@ -1250,11 +1281,16 @@ def _binary_target(
1250
1281
# For h2o models with only one metric provided, return the classification
1251
1282
if h2o_model :
1252
1283
cls .score_code += (
1253
- f"{ '' :4} if prediction[1][2] > { threshold } :\n "
1254
- f"{ '' :8} { metrics } = \" { target_values [0 ]} \" \n "
1284
+ f"{ '' :4} if input_array.shape[0] == 1\n "
1285
+ f"{ '' :8} if prediction[1][2] > { threshold } :\n "
1286
+ f"{ '' :12} { metrics } = \" { target_values [0 ]} \" \n "
1287
+ f"{ '' :8} else:\n "
1288
+ f"{ '' :12} { metrics } = \" { target_values [1 ]} \" \n "
1289
+ f"{ '' :8} return { metrics } \n "
1255
1290
f"{ '' :4} else:\n "
1256
- f"{ '' :8} { metrics } = \" { target_values [1 ]} \" \n \n "
1257
- f"{ '' :4} return { metrics } "
1291
+ f"{ '' :8} output_table = pd.DataFrame(prediction[1:], columns=prediction[0])\n "
1292
+ f"{ '' :8} output_table = pd.DataFrame({{'test': output_table['p1'].map(lambda x: float(x) > { threshold } ).astype(int)}})\n "
1293
+ f"{ '' :8} return output_table"
1258
1294
)
1259
1295
"""
1260
1296
if prediction[1][2] > 0.5:
@@ -1266,18 +1302,25 @@ def _binary_target(
1266
1302
"""
1267
1303
# One return that is the classification
1268
1304
elif len (returns ) == 1 and returns [0 ]:
1269
- cls .score_code += f"{ '' :4} return prediction"
1305
+ cls .score_code += (
1306
+ f"{ '' :4} if input_array.shape[0] == 1\n "
1307
+ f"{ '' :8} return prediction\n "
1308
+ f"{ '' :4} else:\n "
1309
+ f"{ '' :8} return pd.DataFrame({{'{ metrics } ': prediction}})" )
1270
1310
"""
1271
1311
return prediction
1272
1312
"""
1273
1313
# One return that is a probability
1274
1314
elif len (returns ) == 1 and not returns [0 ]:
1275
1315
cls .score_code += (
1276
- f"{ '' :4} if prediction > { threshold } :\n "
1277
- f"{ '' :8} { metrics } = \" { target_values [0 ]} \" \n "
1316
+ f"{ '' :4} if input_array.shape[0] == 1\n "
1317
+ f"{ '' :8} if prediction > { threshold } :\n "
1318
+ f"{ '' :12} { metrics } = \" { target_values [0 ]} \" \n "
1319
+ f"{ '' :8} else:\n "
1320
+ f"{ '' :12} { metrics } = \" { target_values [1 ]} \" \n "
1321
+ f"{ '' :8} return { metrics } \n "
1278
1322
f"{ '' :4} else:\n "
1279
- f"{ '' :8} { metrics } = \" { target_values [1 ]} \" \n \n "
1280
- f"{ '' :4} return { metrics } "
1323
+ f"{ '' :8} return pd.DataFrame({{'{ metrics } ': ['{ target_values [0 ]} ' if p > { threshold } else '{ target_values [1 ]} ' for p in prediction]}})\n "
1281
1324
)
1282
1325
"""
1283
1326
if prediction > 0.5:
@@ -1291,11 +1334,15 @@ def _binary_target(
1291
1334
elif len (returns ) == 2 and sum (returns ) == 0 :
1292
1335
# Only probabilities returned; return classification for larger value
1293
1336
cls .score_code += (
1294
- f"{ '' :4} if prediction[0] > prediction[1]:\n "
1295
- f"{ '' :8} { metrics } = \" { target_values [0 ]} \" \n "
1337
+ f"{ '' :4} if input_array.shape[0] == 1\n "
1338
+ f"{ '' :8} if prediction[0] > prediction[1]:\n "
1339
+ f"{ '' :12} { metrics } = \" { target_values [0 ]} \" \n "
1340
+ f"{ '' :8} else:\n "
1341
+ f"{ '' :12} { metrics } = \" { target_values [1 ]} \" \n \n "
1342
+ f"{ '' :8} return { metrics } \n "
1296
1343
f"{ '' :4} else:\n "
1297
- f"{ '' :8} { metrics } = \" { target_values [ 1 ] } \" \n \n "
1298
- f"{ '' :4 } return { metrics } "
1344
+ f"{ '' :8} classifications = np.argmax(prediction, axis=1) \n "
1345
+ f"{ '' :8 } reutrn pd.DataFrame({{' { metrics } ': [' { target_values [ 1 ] } ' if c == 0 else ' { target_values [ 0 ] } ' for c in classfications]}}) "
1299
1346
)
1300
1347
"""
1301
1348
if prediction[0] > prediction[1]:
@@ -1310,7 +1357,11 @@ def _binary_target(
1310
1357
# Determine which return is the classification value
1311
1358
class_index = [i for i , x in enumerate (returns ) if x ][0 ]
1312
1359
cls .score_code += (
1313
- f"{ '' :4} { metrics } = prediction[{ class_index } ]\n \n return { metrics } "
1360
+ f"{ '' :4} if input_array.shape[0] == 1\n "
1361
+ f"{ '' :8} { metrics } = prediction[{ class_index } ]\n \n return { metrics } \n "
1362
+ f"{ '' :4} else:\n "
1363
+ f"{ '' :8} output_table = pd.DataFrame({{'{ metrics } ': [p[{ class_index } ] for p in prediction]}})\n "
1364
+ f"{ '' :8} return output_table"
1314
1365
)
1315
1366
"""
1316
1367
classification_variable = prediction[2]
@@ -1328,7 +1379,11 @@ def _binary_target(
1328
1379
"the target event to occur."
1329
1380
)
1330
1381
cls .score_code += (
1331
- f"{ '' :4} return prediction[1][0], float(prediction[1][2])"
1382
+ f"{ '' :4} if input_array.shape[0] == 1\n "
1383
+ f"{ '' :8} return prediction[1][0], float(prediction[1][2])"
1384
+ f"{ '' :4} else:\n "
1385
+ f"{ '' :8} output_table = pd.DataFrame(prediction[1:], columns=[{ ',drop,' .join (metrics )} ])\n "
1386
+ f"{ '' :8} return output_table.drop('drop', axis=1)"
1332
1387
)
1333
1388
"""
1334
1389
return prediction[1][0], float(prediction[1][2])
@@ -1341,11 +1396,15 @@ def _binary_target(
1341
1396
"metric is returned first."
1342
1397
)
1343
1398
cls .score_code += (
1344
- f"{ '' :4} if prediction > { threshold } :\n "
1345
- f"{ '' :8} { metrics [0 ]} = \" { target_values [0 ]} \" \n "
1399
+ f"{ '' :4} if input_array.shape[0] == 1\n "
1400
+ f"{ '' :8} if prediction > { threshold } :\n "
1401
+ f"{ '' :12} { metrics [0 ]} = \" { target_values [0 ]} \" \n "
1402
+ f"{ '' :8} else:\n "
1403
+ f"{ '' :12} { metrics [0 ]} = \" { target_values [1 ]} \" \n \n "
1404
+ f"{ '' :8} return { metrics [0 ]} , prediction\n "
1346
1405
f"{ '' :4} else:\n "
1347
- f"{ '' :8} { metrics [0 ]} = \" { target_values [1 ]} \" \n \n "
1348
- f"{ '' :4 } return { metrics [0 ]} , prediction"
1406
+ f"{ '' :8} classifications = [' { target_values [0 ]} ' if p > { threshold } else ' { target_values [1 ]} ' for p in prediction] \n "
1407
+ f"{ '' :8 } return pd.DataFrame({{' { metrics [0 ]} ': classifications, ' { metrics [ 1 ] } ': prediction}}) "
1349
1408
)
1350
1409
"""
1351
1410
if prediction > 0.5:
@@ -1363,14 +1422,18 @@ def _binary_target(
1363
1422
"target event probability should be returned."
1364
1423
)
1365
1424
cls .score_code += (
1366
- f"{ '' :4} if prediction[0] > prediction[1]:\n "
1367
- f"{ '' :8} { metrics [0 ]} = \" { target_values [0 ]} \" \n "
1425
+ f"{ '' :4} if input_array.shape[0] == 1\n "
1426
+ f"{ '' :8} if prediction[0] > prediction[1]:\n "
1427
+ f"{ '' :12} { metrics [0 ]} = \" { target_values [0 ]} \" \n "
1428
+ f"{ '' :8} else:\n "
1429
+ f"{ '' :12} { metrics [0 ]} = \" { target_values [1 ]} \" \n \n "
1430
+ f"{ '' :8} return { metrics [0 ]} , prediction[0]\n "
1368
1431
f"{ '' :4} else:\n "
1369
- f"{ '' :8} { metrics [0 ]} = \" { target_values [1 ]} \" \n \n "
1370
- f"{ '' :4 } return { metrics [0 ]} , prediction[0] "
1432
+ f"{ '' :8} classifications = [' { target_values [0 ]} ' if p[0]>p[1] else ' { target_values [1 ]} for p in prediction] \n "
1433
+ f"{ '' :8 } return pd.DataFrame({{' { metrics [0 ]} ': classifications, ' { metrics [ 1 ] } ': [p[0] for p in prediction]}}) \n "
1371
1434
)
1372
1435
"""
1373
- if prediction > 0.5 :
1436
+ if prediction[0] > prediction[1] :
1374
1437
classification_variable = "first_event"
1375
1438
else:
1376
1439
classification_variable = "second_event"
@@ -1379,7 +1442,11 @@ def _binary_target(
1379
1442
"""
1380
1443
# Return classification and probability value
1381
1444
elif sum (returns ) == 1 and len (returns ) == 2 :
1382
- cls .score_code += f"{ '' :4} return prediction[0], prediction[1]"
1445
+ cls .score_code += (
1446
+ f"{ '' :4} if input_array.shape[0] == 1\n "
1447
+ f"{ '' :8} return prediction[0], prediction[1]\n "
1448
+ f"{ '' :4} else:\n "
1449
+ f"{ '' :8} return pd.DataFrame(prediction, columns=[{ ',' .join (metrics )} ])" )
1383
1450
"""
1384
1451
return prediction[0], prediction[1]
1385
1452
"""
@@ -1392,14 +1459,23 @@ def _binary_target(
1392
1459
# Determine which return is the classification value
1393
1460
class_index = [i for i , x in enumerate (returns ) if x ][0 ]
1394
1461
if class_index == 0 :
1395
- cls .score_code += f"{ '' :4} return prediction[0], prediction[1]"
1462
+ cls .score_code += (
1463
+ f"{ '' :4} if input_array.shape[0] == 1\n "
1464
+ f"{ '' :8} return prediction[0], prediction[1]\n "
1465
+ f"{ '' :4} else:\n "
1466
+ f"{ '' :8} output_table = pd.DataFrame(prediction, columns=[{ ',' .join (metrics )} ,drop])\n "
1467
+ f"{ '' :8} return output_table.drop('drop', axis=1)" )
1468
+
1396
1469
"""
1397
1470
return prediction[0], prediction[1]
1398
1471
"""
1399
1472
else :
1400
1473
cls .score_code += (
1401
- f"{ '' :4} return prediction[{ class_index } ], prediction[0]"
1402
- )
1474
+ f"{ '' :4} if input_array.shape[0] == 1\n "
1475
+ f"{ '' :8} return prediction[{ class_index } ], prediction[0]\n "
1476
+ f"{ '' :4} else:\n "
1477
+ f"{ '' :8} output_table = pd.DataFrame(prediction, columns=[{ ',drop,' .join (metrics [::- 1 ])} ])\n "
1478
+ f"{ '' :8} return output_table.drop('drop', axis=1)" )
1403
1479
"""
1404
1480
return prediction[2], prediction[0]
1405
1481
"""
@@ -1408,8 +1484,11 @@ def _binary_target(
1408
1484
elif len (metrics ) == 3 :
1409
1485
if h2o_model :
1410
1486
cls .score_code += (
1411
- f"{ '' :4} return prediction[1][0], float(prediction[1][1]), "
1412
- f"float(prediction[1][2])"
1487
+ f"{ '' :4} if input_array.shape[0] == 1\n "
1488
+ f"{ '' :8} return prediction[1][0], float(prediction[1][1]), "
1489
+ f"float(prediction[1][2])\n "
1490
+ f"{ '' :4} else:\n "
1491
+ f"{ '' :8} return pd.DataFrame(prediction[1:], columns=[{ ',' .join (metrics )} ])"
1413
1492
)
1414
1493
"""
1415
1494
return prediction[1][0], float(prediction[1][1]), float(prediction[1][2])
@@ -1426,6 +1505,7 @@ def _binary_target(
1426
1505
f"{ '' :4} else:\n "
1427
1506
f"{ '' :8} { metrics [0 ]} = \" { target_values [1 ]} \" \n \n "
1428
1507
f"{ '' :4} return { metrics [0 ]} , prediction, 1 - prediction"
1508
+
1429
1509
)
1430
1510
"""
1431
1511
if prediction > 0.5:
0 commit comments