{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {
    "Notes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "vectorize",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "module",
                "path": "numpy:vectorize"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " function is provided primarily for convenience, not for performance. The implementation is essentially a for loop."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "If neither "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "otypes",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": null,
                "version": null,
                "kind": "local",
                "path": "otypes"
              },
              "kind": "local"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " nor "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "signature",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": null,
                "version": null,
                "kind": "local",
                "path": "signature"
              },
              "kind": "local"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " are specified, then a call to the function with the first argument will be used to determine the number of outputs.  The results of this call will be cached if "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "cache",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": null,
                "version": null,
                "kind": "local",
                "path": "cache"
              },
              "kind": "local"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "True",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to prevent calling the function twice.  However, to implement the cache, the original function must be wrapped which will slow down subsequent calls, so only do this if your function is expensive."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The new keyword argument interface and "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "excluded",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": null,
                "version": null,
                "kind": "local",
                "path": "excluded"
              },
              "kind": "local"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " argument support further degrades performance."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Raises": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Yields": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Methods": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Returns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "out",
              "annotation": "callable",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "A vectorized function if "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "pyfunc"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " was provided, a decorator otherwise."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Returns an object that acts like pyfunc, but takes arrays as input."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Receives": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warnings": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Attributes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "pyfunc",
              "annotation": "callable, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "A python function or method. Can be omitted to produce a decorator with keyword arguments."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "otypes",
              "annotation": "str or list of dtypes, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The output data type. It must be specified as either a string of typecode characters or a list of data type specifiers. There should be one data type specifier for each output."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "doc",
              "annotation": "str, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The docstring for the function. If None, the docstring will be the "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "pyfunc.__doc__"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "excluded",
              "annotation": "set, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Set of strings or integers representing the positional or keyword arguments for which the function will not be vectorized. These will be passed directly to "
                    },
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "pyfunc",
                      "reference": {
                        "__type": "RefInfo",
                        "__tag": 4000,
                        "module": null,
                        "version": null,
                        "kind": "local",
                        "path": "pyfunc"
                      },
                      "kind": "local"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " unmodified."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "cache",
              "annotation": "bool, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If neither "
                    },
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "otypes",
                      "reference": {
                        "__type": "RefInfo",
                        "__tag": 4000,
                        "module": null,
                        "version": null,
                        "kind": "local",
                        "path": "otypes"
                      },
                      "kind": "local"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " nor "
                    },
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "signature",
                      "reference": {
                        "__type": "RefInfo",
                        "__tag": 4000,
                        "module": null,
                        "version": null,
                        "kind": "local",
                        "path": "signature"
                      },
                      "kind": "local"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " are provided, and "
                    },
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "cache",
                      "reference": {
                        "__type": "RefInfo",
                        "__tag": 4000,
                        "module": null,
                        "version": null,
                        "kind": "local",
                        "path": "cache"
                      },
                      "kind": "local"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "True"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", then cache the number of outputs."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "signature",
              "annotation": "string, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Generalized universal function signature, e.g., "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "(m,n),(n)->(m)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " for vectorized matrix-vector multiplication. If provided, "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "pyfunc"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " will be called with (and expected to return) arrays with shapes given by the size of corresponding core dimensions. By default, "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "pyfunc"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is assumed to take scalars as input and output."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Extended Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Define a vectorized function which takes a nested sequence of objects or numpy arrays as inputs and returns a single numpy array or a tuple of numpy arrays. The vectorized function evaluates "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "pyfunc",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": null,
                "version": null,
                "kind": "local",
                "path": "pyfunc"
              },
              "kind": "local"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " over successive tuples of the input arrays like the python map function, except it uses the broadcasting rules of numpy."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The data type of the output of "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "vectorized",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is determined by calling the function with the first element of the input.  This can be avoided by specifying the "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "otypes",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": null,
                "version": null,
                "kind": "local",
                "path": "otypes"
              },
              "kind": "local"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " argument."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Other Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    }
  },
  "_ordered_sections": [
    "Summary",
    "Extended Summary",
    "Parameters",
    "Attributes",
    "Methods",
    "Returns",
    "Yields",
    "Receives",
    "Other Parameters",
    "Raises",
    "Warns",
    "Warnings",
    "Notes"
  ],
  "item_file": "/numpy/__init__.py",
  "item_line": null,
  "item_type": "class",
  "aliases": [
    "numpy.vectorize"
  ],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "import numpy as np\ndef myfunc(a, b):\n    \"Return a-b if a>b, otherwise return a+b\"\n    if a > b:\n        return a - b\n    else:\n        return a + b\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "vfunc = np.vectorize(myfunc)\nvfunc([1, 2, 3, 4], 2)\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe docstring is taken from the input function to `vectorize` unless it\nis specified:\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "vfunc.__doc__\nvfunc = np.vectorize(myfunc, doc='Vectorized `myfunc`')\nvfunc.__doc__\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe output type is determined by evaluating the first element of the input,\nunless it is specified:\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "out = vfunc([1, 2, 3, 4], 2)\ntype(out[0])\nvfunc = np.vectorize(myfunc, otypes=[float])\nout = vfunc([1, 2, 3, 4], 2)\ntype(out[0])\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe `excluded` argument can be used to prevent vectorizing over certain\narguments.  This can be useful for array-like arguments of a fixed length\nsuch as the coefficients for a polynomial as in `polyval`:\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "def mypolyval(p, x):\n    _p = list(p)\n    res = _p.pop(0)\n    while _p:\n        res = res*x + _p.pop(0)\n    return res\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nHere, we exclude the zeroth argument from vectorization whether it is\npassed by position or keyword.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "vpolyval = np.vectorize(mypolyval, excluded={0, 'p'})\nvpolyval([1, 2, 3], x=[0, 1])\nvpolyval(p=[1, 2, 3], x=[0, 1])\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe `signature` argument allows for vectorizing functions that act on\nnon-scalar arrays of fixed length. For example, you can use it for a\nvectorized calculation of Pearson correlation coefficient and its p-value:\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "import scipy.stats\npearsonr = np.vectorize(scipy.stats.pearsonr,\n                signature='(n),(n)->(),()')\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "pearsonr([[0, 1, 2, 3]], [[1, 2, 3, 4], [4, 3, 2, 1]])\n",
        "execution_status": "failure"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nOr for a vectorized convolution:\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "convolve = np.vectorize(np.convolve, signature='(n),(m)->(k)')\nconvolve(np.eye(4), [1, 2, 1])\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nDecorator syntax is supported.  The decorator can be called as\na function to provide keyword arguments:\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "@np.vectorize\ndef identity(x):\n    return x\nidentity([0, 1, 2])\n@np.vectorize(otypes=[float])\ndef as_float(x):\n    return x\nas_float([0, 1, 2])\n",
        "execution_status": "success"
      }
    ],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [
    {
      "__type": "SeeAlsoItem",
      "__tag": 4028,
      "name": {
        "__type": "CrossRef",
        "__tag": 4002,
        "value": "frompyfunc",
        "reference": {
          "__type": "RefInfo",
          "__tag": 4000,
          "module": "current-module",
          "version": "current-version",
          "kind": "to-resolve",
          "path": "frompyfunc"
        },
        "kind": "module"
      },
      "descriptions": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Takes an arbitrary Python function and returns a ufunc"
            }
          ]
        }
      ],
      "type": null
    }
  ],
  "signature": null,
  "references": [
    ".. [1] :doc:`/reference/c-api/generalized-ufuncs`"
  ],
  "qa": "numpy:vectorize",
  "arbitrary": [],
  "local_refs": [
    "cache",
    "doc",
    "excluded",
    "otypes",
    "out",
    "pyfunc",
    "signature"
  ]
}