{"action":"created","issue":{"url":"https://api.github.com/repos/numpy/numpy/issues/8054","repository_url":"https://api.github.com/repos/numpy/numpy","labels_url":"https://api.github.com/repos/numpy/numpy/issues/8054/labels{/name}","comments_url":"https://api.github.com/repos/numpy/numpy/issues/8054/comments","events_url":"https://api.github.com/repos/numpy/numpy/issues/8054/events","html_url":"https://github.com/numpy/numpy/pull/8054","id":177115786,"number":8054,"title":"ENH: add signature argument to vectorize for vectorizing like generalized ufuncs","user":{"login":"shoyer","id":1217238,"avatar_url":"https://avatars.githubusercontent.com/u/1217238?v=3","gravatar_id":"","url":"https://api.github.com/users/shoyer","html_url":"https://github.com/shoyer","followers_url":"https://api.github.com/users/shoyer/followers","following_url":"https://api.github.com/users/shoyer/following{/other_user}","gists_url":"https://api.github.com/users/shoyer/gists{/gist_id}","starred_url":"https://api.github.com/users/shoyer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shoyer/subscriptions","organizations_url":"https://api.github.com/users/shoyer/orgs","repos_url":"https://api.github.com/users/shoyer/repos","events_url":"https://api.github.com/users/shoyer/events{/privacy}","received_events_url":"https://api.github.com/users/shoyer/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/numpy/numpy/labels/09%20-%20Enhancement","name":"09 - Enhancement","color":"fef2c0"},{"url":"https://api.github.com/repos/numpy/numpy/labels/component:%20numpy.lib","name":"component: numpy.lib","color":"009800"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/numpy/numpy/milestones/41","html_url":"https://github.com/numpy/numpy/milestones/1.12.0%20release","labels_url":"https://api.github.com/repos/numpy/numpy/milestones/41/labels","id":1503092,"number":41,"title":"1.12.0 release","description":"Tag for the Numpy 1.12.0 release.","creator":{"login":"charris","id":77272,"avatar_url":"https://avatars.githubusercontent.com/u/77272?v=3","gravatar_id":"","url":"https://api.github.com/users/charris","html_url":"https://github.com/charris","followers_url":"https://api.github.com/users/charris/followers","following_url":"https://api.github.com/users/charris/following{/other_user}","gists_url":"https://api.github.com/users/charris/gists{/gist_id}","starred_url":"https://api.github.com/users/charris/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/charris/subscriptions","organizations_url":"https://api.github.com/users/charris/orgs","repos_url":"https://api.github.com/users/charris/repos","events_url":"https://api.github.com/users/charris/events{/privacy}","received_events_url":"https://api.github.com/users/charris/received_events","type":"User","site_admin":false},"open_issues":29,"closed_issues":67,"state":"open","created_at":"2016-01-12T20:39:09Z","updated_at":"2016-10-17T22:11:05Z","due_on":null,"closed_at":null},"comments":11,"created_at":"2016-09-15T08:15:04Z","updated_at":"2016-10-17T22:56:36Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/numpy/numpy/pulls/8054","html_url":"https://github.com/numpy/numpy/pull/8054","diff_url":"https://github.com/numpy/numpy/pull/8054.diff","patch_url":"https://github.com/numpy/numpy/pull/8054.patch"},"body":"I brought this up on the mailing list today and received some support, so I\r\nthought I would share a preliminary patch.\r\n\r\n~~Still needs tests and more extensive documentation.~~\r\n\r\nExample usage (from the docstring):\r\n\r\n    Vectorized calculation of Pearson correlation coefficient and its p-value:\r\n\r\n    >>> import scipy.stats\r\n    >>> pearsonr = np.vectorize(scipy.stats.pearsonr,\r\n    ...                         signature='(n),(n)->(),()')\r\n    >>> pearsonr([[0, 1, 2, 3]], [[1, 2, 3, 4], [4, 3, 2, 1]])\r\n    (array([ 1., -1.]), array([ 0.,  0.]))\r\n\r\n    Vectorized convolution:\r\n\r\n    >>> convolve = np.vectorize(np.convolve, signature='(n),(m)->(k)')\r\n    >>> convolve(np.eye(4), [1, 2, 1])\r\n    array([[ 1.,  2.,  1.,  0.,  0.,  0.],\r\n           [ 0.,  1.,  2.,  1.,  0.,  0.],\r\n           [ 0.,  0.,  1.,  2.,  1.,  0.],\r\n           [ 0.,  0.,  0.,  1.,  2.,  1.]])\r\n\r\nA note on speed: a `vectorize` with `signature` has about 6x the fixed overhead of `vectorize` (60us vs 10us) and also a much larger overhead per loop (2us vs 100ns). I don't think these numbers will make any difference for the intended use case (only trivial Python functions runs in less than 10us) but I thought I would toss them out there anyways. I have yet to make any attempts at optimization.\r\n\r\n```python\r\n# run on python 3.5.2\r\nimport numpy as np\r\nimport operator\r\n\r\nvectorize_add = np.vectorize(operator.add, otypes='i')\r\nguvectorize_add = np.vectorize(operator.add, signature='(),()->()', otypes='i')\r\n\r\nx = np.ones(1000000, dtype=int)\r\n\r\n%timeit vectorize_add(1, 1)\r\n# 100000 loops, best of 3: 10.6 µs per loop\r\n\r\n%timeit guvectorize_add(1, 1)\r\n# 10000 loops, best of 3: 63.4 µs per loop\r\n\r\n%timeit vectorize_add(x, x)\r\n# 10 loops, best of 3: 112 ms per loop\r\n\r\n%timeit guvectorize_add(x, x)\r\n# 1 loop, best of 3: 2.22 s per loop\r\n```\r\n\r\nAlso fixes #5868"},"comment":{"url":"https://api.github.com/repos/numpy/numpy/issues/comments/254358667","html_url":"https://github.com/numpy/numpy/pull/8054#issuecomment-254358667","issue_url":"https://api.github.com/repos/numpy/numpy/issues/8054","id":254358667,"user":{"login":"shoyer","id":1217238,"avatar_url":"https://avatars.githubusercontent.com/u/1217238?v=3","gravatar_id":"","url":"https://api.github.com/users/shoyer","html_url":"https://github.com/shoyer","followers_url":"https://api.github.com/users/shoyer/followers","following_url":"https://api.github.com/users/shoyer/following{/other_user}","gists_url":"https://api.github.com/users/shoyer/gists{/gist_id}","starred_url":"https://api.github.com/users/shoyer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shoyer/subscriptions","organizations_url":"https://api.github.com/users/shoyer/orgs","repos_url":"https://api.github.com/users/shoyer/repos","events_url":"https://api.github.com/users/shoyer/events{/privacy}","received_events_url":"https://api.github.com/users/shoyer/received_events","type":"User","site_admin":false},"created_at":"2016-10-17T22:56:36Z","updated_at":"2016-10-17T22:56:36Z","body":"@charris how do you feel about GitHub's \"Squash and merge\" option? I think that can result in something that looks very similar to what I would make by hand here."}}
{"actor":{"display_login":"shoyer"}}