Skip to main content

Mapping values in NumPy arrays (any shape!) with high speed - Cython -

Project description

Mapping values in NumPy arrays (any shape!) with high speed - Cython -

pip install nparraymapper

Tested against Python 3.11 / Windows 10

Cython (and a C/C++ compiler) must be installed to use the optimized Cython implementation.

This module provides functions for mapping values in NumPy arrays based on a specified mapping dictionary. It works on any shape and with almost all dtypes (OBJECT NOT!!!)

It always returns a copy! The original data doesn't change!

import numpy as np

from nparraymapper import map_numpy_array, map_array_with_strings

np.random.seed(0)
a = np.random.randint(1, 9, (10000, 10000, 4))
ds = map_numpy_array(a, mapdict={4: 400000, 3: 1021, 2: -1}, keepnotmapped=True)
print(a)
print(ds)
print('----------------------------')
a = np.random.randint(2, 9, (100, 100, 3), dtype=np.uint32)
ds = map_numpy_array(a, mapdict={4: 4000.1232, 3: 1021.32}, keepnotmapped=True)
print(a)
print(ds)
print('----------------------------')
a = np.random.randint(2, 9, (1000, 100, 8)).astype(np.float32)

ds = map_numpy_array(a, mapdict={4: 40, 3: 1021}, keepnotmapped=False)

print(a)
print(ds)
print('----------------------------')

subsdi = {110: 'babça', 14: 'bsdfvd', 9: 'çbba'}
src = np.random.randint(1, 12, (1000, 1000, 3))
print(src)
out = map_array_with_strings(src, mapdict=subsdi)
print(out)

# [[[5 8 6 1]
#   [4 4 4 8]
#   [2 4 6 3]
#   ...
#   [6 4 4 4]
#   [2 5 8 2]
#   [3 7 5 5]]
#  [[6 1 4 1]
#   [1 2 5 8]
#   [1 7 6 5]
#   ...
#   [6 7 7 1]
#   [5 5 4 7]
#   [5 8 7 6]]
#  [[1 2 8 5]
#   [2 2 8 6]
#   [1 3 4 4]
#   ...
#   [3 4 3 2]
#   [4 6 8 5]
#   [4 7 6 4]]
#  ...
#  [[2 7 5 5]
#   [2 2 8 8]
#   [8 8 7 8]
#   ...
#   [8 7 5 8]
#   [4 6 5 8]
#   [3 8 5 8]]
#  [[6 4 1 5]
#   [1 7 7 1]
#   [2 4 5 3]
#   ...
#   [1 5 5 4]
#   [1 8 6 7]
#   [4 5 8 4]]
#  [[4 2 3 5]
#   [5 7 8 2]
#   [1 3 4 6]
#   ...
#   [3 8 1 5]
#   [7 8 4 3]
#   [6 8 3 4]]]
# [[[     5      8      6      1]
#   [400000 400000 400000      8]
#   [    -1 400000      6   1021]
#   ...
#   [     6 400000 400000 400000]
#   [    -1      5      8     -1]
#   [  1021      7      5      5]]
#  [[     6      1 400000      1]
#   [     1     -1      5      8]
#   [     1      7      6      5]
#   ...
#   [     6      7      7      1]
#   [     5      5 400000      7]
#   [     5      8      7      6]]
#  [[     1     -1      8      5]
#   [    -1     -1      8      6]
#   [     1   1021 400000 400000]
#   ...
#   [  1021 400000   1021     -1]
#   [400000      6      8      5]
#   [400000      7      6 400000]]
#  ...
#  [[    -1      7      5      5]
#   [    -1     -1      8      8]
#   [     8      8      7      8]
#   ...
#   [     8      7      5      8]
#   [400000      6      5      8]
#   [  1021      8      5      8]]
#  [[     6 400000      1      5]
#   [     1      7      7      1]
#   [    -1 400000      5   1021]
#   ...
#   [     1      5      5 400000]
#   [     1      8      6      7]
#   [400000      5      8 400000]]
#  [[400000     -1   1021      5]
#   [     5      7      8     -1]
#   [     1   1021 400000      6]
#   ...
#   [  1021      8      1      5]
#   [     7      8 400000   1021]
#   [     6      8   1021 400000]]]
# ----------------------------
# [[[2 8 2]
#   [3 4 3]
#   [2 7 7]
#   ...
#   [4 6 4]
#   [3 7 5]
#   [7 7 8]]
#  [[4 4 4]
#   [2 7 4]
#   [2 7 2]
#   ...
#   [2 7 8]
#   [4 6 8]
#   [8 8 7]]
#  [[6 5 3]
#   [5 5 5]
#   [8 6 3]
#   ...
#   [4 3 6]
#   [4 6 7]
#   [2 8 7]]
#  ...
#  [[3 3 4]
#   [8 4 4]
#   [5 5 2]
#   ...
#   [4 2 5]
#   [7 3 7]
#   [6 5 2]]
#  [[3 3 2]
#   [4 6 5]
#   [8 8 4]
#   ...
#   [7 7 5]
#   [6 6 2]
#   [7 5 4]]
#  [[5 8 2]
#   [8 5 4]
#   [6 7 6]
#   ...
#   [4 8 6]
#   [2 8 3]
#   [2 8 4]]]
# [[[2.0000000e+00 8.0000000e+00 2.0000000e+00]
#   [1.0213200e+03 4.0001232e+03 1.0213200e+03]
#   [2.0000000e+00 7.0000000e+00 7.0000000e+00]
#   ...
#   [4.0001232e+03 6.0000000e+00 4.0001232e+03]
#   [1.0213200e+03 7.0000000e+00 5.0000000e+00]
#   [7.0000000e+00 7.0000000e+00 8.0000000e+00]]
#  [[4.0001232e+03 4.0001232e+03 4.0001232e+03]
#   [2.0000000e+00 7.0000000e+00 4.0001232e+03]
#   [2.0000000e+00 7.0000000e+00 2.0000000e+00]
#   ...
#   [2.0000000e+00 7.0000000e+00 8.0000000e+00]
#   [4.0001232e+03 6.0000000e+00 8.0000000e+00]
#   [8.0000000e+00 8.0000000e+00 7.0000000e+00]]
#  [[6.0000000e+00 5.0000000e+00 1.0213200e+03]
#   [5.0000000e+00 5.0000000e+00 5.0000000e+00]
#   [8.0000000e+00 6.0000000e+00 1.0213200e+03]
#   ...
#   [4.0001232e+03 1.0213200e+03 6.0000000e+00]
#   [4.0001232e+03 6.0000000e+00 7.0000000e+00]
#   [2.0000000e+00 8.0000000e+00 7.0000000e+00]]
#  ...
#  [[1.0213200e+03 1.0213200e+03 4.0001232e+03]
#   [8.0000000e+00 4.0001232e+03 4.0001232e+03]
#   [5.0000000e+00 5.0000000e+00 2.0000000e+00]
#   ...
#   [4.0001232e+03 2.0000000e+00 5.0000000e+00]
#   [7.0000000e+00 1.0213200e+03 7.0000000e+00]
#   [6.0000000e+00 5.0000000e+00 2.0000000e+00]]
#  [[1.0213200e+03 1.0213200e+03 2.0000000e+00]
#   [4.0001232e+03 6.0000000e+00 5.0000000e+00]
#   [8.0000000e+00 8.0000000e+00 4.0001232e+03]
#   ...
#   [7.0000000e+00 7.0000000e+00 5.0000000e+00]
#   [6.0000000e+00 6.0000000e+00 2.0000000e+00]
#   [7.0000000e+00 5.0000000e+00 4.0001232e+03]]
#  [[5.0000000e+00 8.0000000e+00 2.0000000e+00]
#   [8.0000000e+00 5.0000000e+00 4.0001232e+03]
#   [6.0000000e+00 7.0000000e+00 6.0000000e+00]
#   ...
#   [4.0001232e+03 8.0000000e+00 6.0000000e+00]
#   [2.0000000e+00 8.0000000e+00 1.0213200e+03]
#   [2.0000000e+00 8.0000000e+00 4.0001232e+03]]]
# ----------------------------
# [[[2. 2. 4. ... 7. 5. 4.]
#   [7. 3. 4. ... 4. 7. 3.]
#   [5. 2. 3. ... 8. 6. 6.]
#   ...
#   [4. 2. 2. ... 5. 5. 4.]
#   [4. 5. 8. ... 4. 2. 2.]
#   [5. 2. 6. ... 6. 2. 8.]]
#  [[7. 5. 7. ... 2. 4. 8.]
#   [6. 4. 6. ... 8. 5. 4.]
#   [4. 8. 7. ... 6. 6. 8.]
#   ...
#   [5. 7. 4. ... 5. 8. 7.]
#   [3. 2. 8. ... 7. 6. 4.]
#   [5. 6. 2. ... 3. 4. 4.]]
#  [[2. 2. 3. ... 3. 2. 3.]
#   [7. 2. 2. ... 6. 7. 8.]
#   [8. 5. 7. ... 3. 6. 3.]
#   ...
#   [3. 8. 5. ... 6. 8. 5.]
#   [6. 8. 2. ... 2. 3. 4.]
#   [2. 7. 4. ... 2. 5. 2.]]
#  ...
#  [[4. 6. 3. ... 7. 6. 2.]
#   [4. 4. 2. ... 5. 8. 4.]
#   [6. 7. 8. ... 4. 2. 6.]
#   ...
#   [7. 3. 6. ... 2. 7. 4.]
#   [2. 6. 7. ... 3. 5. 3.]
#   [5. 4. 8. ... 3. 4. 5.]]
#  [[8. 7. 8. ... 5. 8. 2.]
#   [7. 3. 2. ... 5. 4. 8.]
#   [4. 8. 8. ... 2. 2. 5.]
#   ...
#   [6. 3. 2. ... 4. 6. 7.]
#   [7. 7. 6. ... 2. 7. 3.]
#   [8. 4. 3. ... 3. 6. 8.]]
#  [[6. 3. 4. ... 2. 7. 7.]
#   [2. 3. 6. ... 3. 5. 6.]
#   [7. 6. 2. ... 7. 6. 8.]
#   ...
#   [4. 3. 2. ... 3. 4. 3.]
#   [6. 2. 5. ... 2. 5. 5.]
#   [7. 8. 4. ... 7. 7. 8.]]]
# [[[   0    0   40 ...    0    0   40]
#   [   0 1021   40 ...   40    0 1021]
#   [   0    0 1021 ...    0    0    0]
#   ...
#   [  40    0    0 ...    0    0   40]
#   [  40    0    0 ...   40    0    0]
#   [   0    0    0 ...    0    0    0]]
#  [[   0    0    0 ...    0   40    0]
#   [   0   40    0 ...    0    0   40]
#   [  40    0    0 ...    0    0    0]
#   ...
#   [   0    0   40 ...    0    0    0]
#   [1021    0    0 ...    0    0   40]
#   [   0    0    0 ... 1021   40   40]]
#  [[   0    0 1021 ... 1021    0 1021]
#   [   0    0    0 ...    0    0    0]
#   [   0    0    0 ... 1021    0 1021]
#   ...
#   [1021    0    0 ...    0    0    0]
#   [   0    0    0 ...    0 1021   40]
#   [   0    0   40 ...    0    0    0]]
#  ...
#  [[  40    0 1021 ...    0    0    0]
#   [  40   40    0 ...    0    0   40]
#   [   0    0    0 ...   40    0    0]
#   ...
#   [   0 1021    0 ...    0    0   40]
#   [   0    0    0 ... 1021    0 1021]
#   [   0   40    0 ... 1021   40    0]]
#  [[   0    0    0 ...    0    0    0]
#   [   0 1021    0 ...    0   40    0]
#   [  40    0    0 ...    0    0    0]
#   ...
#   [   0 1021    0 ...   40    0    0]
#   [   0    0    0 ...    0    0 1021]
#   [   0   40 1021 ... 1021    0    0]]
#  [[   0 1021   40 ...    0    0    0]
#   [   0 1021    0 ... 1021    0    0]
#   [   0    0    0 ...    0    0    0]
#   ...
#   [  40 1021    0 ... 1021   40 1021]
#   [   0    0    0 ...    0    0    0]
#   [   0    0   40 ...    0    0    0]]]
# ----------------------------
# [[[ 3  4 11]
#   [ 2  9  6]
#   [ 5  5  5]
#   ...
#   [11  9  3]
#   [ 9  3 10]
#   [ 5  5  4]]
#  [[ 5  6  8]
#   [ 7  8  9]
#   [ 9  8  9]
#   ...
#   [ 5  6 11]
#   [ 7  5  9]
#   [ 1 11  3]]
#  [[ 3  3 10]
#   [ 4  7  2]
#   [ 5  1  9]
#   ...
#   [11  8  5]
#   [ 6 11 11]
#   [ 2 10  1]]
#  ...
#  [[ 1  4  6]
#   [ 8  7  6]
#   [10  3  2]
#   ...
#   [10  8  3]
#   [ 7  7 11]
#   [ 7  7  7]]
#  [[ 3  8  6]
#   [ 5  5  6]
#   [ 7  7  7]
#   ...
#   [ 1  9  9]
#   [ 9  3  9]
#   [10  9  3]]
#  [[ 2  9 11]
#   [ 7  7  9]
#   [10 11  3]
#   ...
#   [ 6  7 11]
#   [11  3  4]
#   [ 5  3  2]]]
# [[['' '' '']
#   ['' 'çbba' '']
#   ['' '' '']
#   ...
#   ['' 'çbba' '']
#   ['çbba' '' '']
#   ['' '' '']]
#  [['' '' '']
#   ['' '' 'çbba']
#   ['çbba' '' 'çbba']
#   ...
#   ['' '' '']
#   ['' '' 'çbba']
#   ['' '' '']]
#  [['' '' '']
#   ['' '' '']
#   ['' '' 'çbba']
#   ...
#   ['' '' '']
#   ['' '' '']
#   ['' '' '']]
#  ...
#  [['' '' '']
#   ['' '' '']
#   ['' '' '']
#   ...
#   ['' '' '']
#   ['' '' '']
#   ['' '' '']]
#  [['' '' '']
#   ['' '' '']
#   ['' '' '']
#   ...
#   ['' 'çbba' 'çbba']
#   ['çbba' '' 'çbba']
#   ['' 'çbba' '']]
#  [['' 'çbba' '']
#   ['' '' 'çbba']
#   ['' '' '']
#   ...
#   ['' '' '']
#   ['' '' '']
#   ['' '' '']]]

Project details


Release history Release notifications | RSS feed

This version

0.10

Download files

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

Source Distribution

nparraymapper-0.10.tar.gz (25.4 kB view details)

Uploaded Source

Built Distribution

nparraymapper-0.10-py3-none-any.whl (25.0 kB view details)

Uploaded Python 3

File details

Details for the file nparraymapper-0.10.tar.gz.

File metadata

  • Download URL: nparraymapper-0.10.tar.gz
  • Upload date:
  • Size: 25.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for nparraymapper-0.10.tar.gz
Algorithm Hash digest
SHA256 d5fe77e6d0ea25c58862ca3e21af84f001a6faaef529a6cf00ff26771f44456d
MD5 59e3eceeab2aef29de13222c00b6a5fd
BLAKE2b-256 1bb803bea0c55c22dc211520b2cabf8462639500fb15ada9722a65c0109d6c12

See more details on using hashes here.

File details

Details for the file nparraymapper-0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for nparraymapper-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 ea0cf0d00195c5bc0d6220dcc6545194ad4c81e2da32edc7bb93017cbc049ebe
MD5 0f2e0ad0f66c929246bfa5c5ac5c4c04
BLAKE2b-256 c69ec033fdeafd78afe6e3061416dd21c615f8fc6482fc80a4e65052a7c1fc68

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