Evaluate a Core ML classifier model and compare against predictions from the original framework (for testing correctness of conversion). Use this evaluation for models that don't deal with probabilities. Parameters ---------- filename: list of str or list of MLModel
(model, data, target="target", verbose=False)
| 453 | |
| 454 | |
| 455 | def evaluate_classifier(model, data, target="target", verbose=False): |
| 456 | """ |
| 457 | Evaluate a Core ML classifier model and compare against predictions |
| 458 | from the original framework (for testing correctness of conversion). |
| 459 | Use this evaluation for models that don't deal with probabilities. |
| 460 | |
| 461 | Parameters |
| 462 | ---------- |
| 463 | filename: list of str or list of MLModel |
| 464 | File to load the model from, or a loaded |
| 465 | version of the MLModel. |
| 466 | |
| 467 | data: list of str or list of Dataframe |
| 468 | Test data on which to evaluate the models (dataframe, |
| 469 | or path to a CSV file). |
| 470 | |
| 471 | target: str |
| 472 | Column to interpret as the target column. |
| 473 | |
| 474 | verbose: bool |
| 475 | Set to true for more verbose output. |
| 476 | |
| 477 | See Also |
| 478 | -------- |
| 479 | evaluate_regressor, evaluate_classifier_with_probabilities |
| 480 | |
| 481 | Examples |
| 482 | -------- |
| 483 | .. sourcecode:: python |
| 484 | |
| 485 | metrics = coremltools.utils.evaluate_classifier( |
| 486 | spec, "data_and_predictions.csv", "target" |
| 487 | ) |
| 488 | print(metrics) |
| 489 | {"samples": 10, num_errors: 0} |
| 490 | """ |
| 491 | model = _get_model(model) |
| 492 | if verbose: |
| 493 | print("") |
| 494 | print("Other Framework\t\tPredicted") |
| 495 | |
| 496 | num_errors = 0 |
| 497 | |
| 498 | for _, row in data.iterrows(): |
| 499 | input_dict = dict(row) |
| 500 | _remove_invalid_keys(input_dict, model) |
| 501 | predicted = model.predict(input_dict)[_to_unicode(target)] |
| 502 | other_framework = row[target] |
| 503 | if predicted != other_framework: |
| 504 | num_errors += 1 |
| 505 | |
| 506 | if verbose: |
| 507 | print("{}\t\t\t\t{}".format(other_framework, predicted)) |
| 508 | |
| 509 | ret = {"num_samples": len(data), "num_errors": num_errors} |
| 510 | |
| 511 | if verbose: |
| 512 | print("results: {}".format(ret)) |