Skip to main content

Some useful extensions for NumPy

Project description

npx

PyPi Version PyPI pyversions GitHub stars PyPi downloads

gh-actions codecov LGTM Code style: black

NumPy is a large library used everywhere in scientific computing. That's why breaking backwards-compatibility comes at a significant cost and is almost always avoided, even if the API of some methods is arguably lacking. This package provides drop-in wrappers "fixing" those.

scipyx does the same for SciPy.

If you have a fix for a NumPy method that can't go upstream for some reason, feel free to PR here.

dot

import npx
import numpy as np

a = np.random.rand(3, 4, 5)
b = np.random.rand(5, 2, 2)

out = npx.dot(a, b)
# out.shape == (3, 4, 2, 2)

Forms the dot product between the last axis of a and the first axis of b.

(Not the second-last axis of b as numpy.dot(a, b).)

np.solve

import npx
import numpy as np

A = np.random.rand(3, 3)
b = np.random.rand(3, 10, 4)

out = npx.solve(A, b)
# out.shape == (3, 10, 4)

Solves a linear equation system with a matrix of shape (n, n) and an array of shape (n, ...). The output has the same shape as the second argument.

sum_at/add_at

npx.sum_at(a, idx, minlength=0)
npx.add_at(out, idx, a)

Returns an array with entries of a summed up at indices idx with a minumum length of minlength. idx can have any shape as long as it's matching a. The output shape is (minlength,...).

The numpy equivalent numpy.add.at is much slower:

memory usage

Relevant issue reports:

unique_rows

import npx
import numpy as np

a = np.random.randint(0, 5, size=(100, 2))

npx.unique_rows(a, return_inverse=False, return_counts=False)

Returns the unique rows of the integer array a. The numpy alternative np.unique(a, axis=0) is slow.

Relevant issue reports:

isin_rows

import npx
import numpy as np

a = [[0, 1], [0, 2]]
b = np.random.randint(0, 5, size=(100, 2))

npx.isin_rows(a, b)

Returns a boolean array of length len(a) specifying if the rows a[k] appear in b. Similar to NumPy's own np.isin which only works for scalars.

mean

import npx

a = [1.0, 2.0, 5.0]
npx.mean(a, p=3)

Returns the generalized mean of a given list. Handles the cases +-np.inf (max/min) and0 (geometric mean) correctly. Also does well for large p.

Relevant NumPy issues:

License

This software is published under the BSD-3-Clause license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

npx-0.0.20.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

npx-0.0.20-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file npx-0.0.20.tar.gz.

File metadata

  • Download URL: npx-0.0.20.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0rc2

File hashes

Hashes for npx-0.0.20.tar.gz
Algorithm Hash digest
SHA256 c49ec4f3c1c2befd019399eeb4bc36430d4d5a23d3eaa5a521b94844dada5b84
MD5 4b2e5fc859d94ad0c92ab17afb718fe8
BLAKE2b-256 dad82469d92f90a36d90bed94157952b1a3d282ba75bee314b52f75341897d1b

See more details on using hashes here.

File details

Details for the file npx-0.0.20-py3-none-any.whl.

File metadata

  • Download URL: npx-0.0.20-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0rc2

File hashes

Hashes for npx-0.0.20-py3-none-any.whl
Algorithm Hash digest
SHA256 f570259128614efe836daa86b013f2313652e020ec2f2b73ef82cf5725fc57bf
MD5 18fe7e29c30060b088a1664a2b3efcd3
BLAKE2b-256 1af76190ddfd95db3762b67bee36cdf7e865da025388b1ea6aa4f3c0aaac4318

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page