MCPcopy Create free account
hub / github.com/apple/coremltools / predict

Method predict

coremltools/models/model.py:791–874  ·  view source on GitHub ↗

Return predictions for the model. Parameters ---------- data: dict[str, value] or list[dict[str, value]] Dictionary of data to use for predictions, where the keys are the names of the input features. For batch predictons, use a list of such d

(self, data, state: _Optional[MLState] = None)

Source from the content-addressed store, hash-verified

789
790
791 def predict(self, data, state: _Optional[MLState] = None):
792 """
793 Return predictions for the model.
794
795 Parameters
796 ----------
797 data: dict[str, value] or list[dict[str, value]]
798 Dictionary of data to use for predictions, where the keys are the names of the input features.
799 For batch predictons, use a list of such dictionaries.
800
801 The following dictionary values types are acceptable: list, array, numpy.ndarray, tensorflow.Tensor
802 and torch.Tensor.
803
804 state : MLState
805 Optional state object as returned by ``make_state()``.
806
807 Returns
808 -------
809 dict[str, value]
810 Predictions as a dictionary where each key is the output feature name.
811
812 list[dict[str, value]]
813 For batch prediction, returns a list of the above dictionaries.
814
815 Examples
816 --------
817 .. sourcecode:: python
818
819 data = {"bedroom": 1.0, "bath": 1.0, "size": 1240}
820 predictions = model.predict(data)
821
822 data = [
823 {"bedroom": 1.0, "bath": 1.0, "size": 1240},
824 {"bedroom": 4.0, "bath": 2.5, "size": 2400},
825 ]
826 batch_predictions = model.predict(data)
827
828 """
829 def verify_and_convert_input_dict(d):
830 self._verify_input_dict(d)
831 self._convert_tensor_to_numpy(d)
832 # TODO: remove the following call when this is fixed: rdar://92239209
833 self._update_float16_multiarray_input_to_float32(d)
834
835 if self.is_package and _is_macos() and _macos_version() < (12, 0):
836 raise Exception(
837 "predict() for .mlpackage is not supported in macOS version older than 12.0."
838 )
839 MLModel._check_predict_data(data)
840
841 if self.__proxy__:
842 return self._get_predictions(self.__proxy__,
843 verify_and_convert_input_dict,
844 data,
845 state)
846 else: # Error case
847 if _macos_version() < (10, 13):
848 raise Exception(

Calls 6

_get_predictionsMethod · 0.95
_is_macosFunction · 0.85
_has_custom_layerFunction · 0.85
_check_predict_dataMethod · 0.80
_macos_versionFunction · 0.70
strClass · 0.50