npdict


Namenpdict JSON
Version 0.0.4 PyPI version JSON
download
home_pageNone
SummaryA Python dictionary wrapper for numpy arrays
upload_time2025-08-02 02:45:53
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT
keywords dictionary numpy
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
[![GitHub release](https://img.shields.io/github/release/stephenhky/npdict.svg?maxAge=3600)](https://github.com/stephenhky/npdict/releases)
[![pypi](https://img.shields.io/pypi/v/npdict.svg?maxAge=3600)](https://pypi.org/project/npdict/)
[![download](https://img.shields.io/pypi/dm/npdict.svg?maxAge=2592000&label=installs&color=%2327B1FF)](https://pypi.org/project/npdict/)
[![Documentation Status](https://readthedocs.org/projects/npdict/badge/?version=latest)](https://npdict.readthedocs.io/en/latest/?badge=latest)

# `npdict`: Python Package for Dictionary Wrappers for Numpy Arrays

This Python package, `npdict`, aims at facilitating holding numerical
values in a Python dictionary, but at the same time retaining the
ultra-high performance supported by NumPy. It supports an object
which is a Python dictionary on the surface, but numpy behind the
back, facilitating fast assignment and retrieval of values
and fast computation of numpy arrays.

## Installation

To install, in your terminal, simply enter:

```
pip install npdict
```

## Quickstart

### Instantiation

Suppose you are doing a similarity dictionary between two sets of words.
And each of these sets have words:

```
document1 = ['president', 'computer', 'tree']
document2 = ['chairman', 'abacus', 'trees']
```

And you can build a dictionary like this:

```
import numpy as np
from npdict import NumpyNDArrayWrappedDict

similarity_dict = NumpyNDArrayWrappedDict([document1, document2])
```

An `npdict.NumpyNDArrayWrappedDict` instance is instantiated. It is 
a Python dict:

```
isinstance(similarity_dict, dict)  # which gives `True`
```

It has a matrix inside with default value 0.0 (and the initial default value can
be changed to other values when the instance is instantiated.)

```
similarity_dict.to_numpy()
```
giving
```
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])
```

### Value Assignments

Now you can assign values just like what you do to a Python dictionary:

```
similarity_dict['president', 'chairman'] = 0.9
similarity_dict['computer', 'abacus'] = 0.7
similarity_dict['tree', 'trees'] = 0.95
```

And it has changed the inside numpy array to be:

```
array([[0.9  , 0.  , 0.  ],
       [0.  , 0.7 , 0.  ],
       [0.  , 0.  , 0.95]])
```

### Generation of New Object from the Old One

If you want to create another dict using the same words, but 
a manipulation of the original value, 25 percent discount
of the original one for example, you can do something like this:

```
new_similarity_dict = similarity_dict.generate_dict(similarity_dict.to_numpy()*0.75)
```

And you got a new dictionary with numpy array to be:

```
new_similarity_dict.to_numpy()
```
giving
```
array([[0.675 , 0.    , 0.    ],
       [0.    , 0.525 , 0.    ],
       [0.    , 0.    , 0.7125]])
```

This is a simple operation. But the design of this wrapped Python
dictionary is that you can perform any fast or optimized operation
on your numpy array (using numba or Cython, for examples),
while retaining the keywords as your dictionary.

### Retrieval of Values

At the same time, you can set new values just like above, or retrieve
values as if it is a Python dictionary:

```
similarity_dict['president', 'chairman']
```

### Conversion to a Python Dictionary

You can also convert this to an ordinary Python dictionary:

```
raw_similarity_dict = similarity_dict.to_dict()
```

### Instantiation from a Python Dictionary

And you can convert a Python dictionary of this type back to 
`npdict.NumpyNDArrayWrappedDict` by (recommended)

```
new_similarity_dict_2 = NumpyNDArrayWrappedDict.from_dict_given_keywords([document1, document2], raw_similarity_dict)
```

Or you can even do this (not recommended):

```
new_similarity_dict_3 = NumpyNDArrayWrappedDict.from_dict(raw_similarity_dict)
```

It is not recommended because the order of the keys are not retained in this way.
Use it with caution.

## Working with Sparse Arrays

For large, sparse matrices where most elements are zero, using `SparseArrayWrappedDict` can be more memory-efficient than the standard `NumpyNDArrayWrappedDict`.

### Instantiation

Similar to the regular dictionary wrapper, you can instantiate a sparse array wrapper:

```python
from npdict import SparseArrayWrappedDict

document1 = ['president', 'computer', 'tree', 'car', 'house', 'book']
document2 = ['chairman', 'abacus', 'trees', 'vehicle', 'building', 'paper']

# Create a sparse dictionary - efficient for large, sparse matrices
sparse_similarity_dict = SparseArrayWrappedDict([document1, document2])
```

### Value Assignments

Assign values just like with a regular dictionary:

```python
# Only assign values for the few non-zero elements
sparse_similarity_dict['president', 'chairman'] = 0.9
sparse_similarity_dict['computer', 'abacus'] = 0.7
sparse_similarity_dict['tree', 'trees'] = 0.95
```

The sparse implementation only stores the non-zero values, making it memory-efficient for large, sparse matrices.

### Converting Between Formats

You can convert between dense and sparse formats:

```python
# Convert to NumPy array (dense format)
dense_array = sparse_similarity_dict.to_numpy()

# Convert to COO format (another sparse format)
coo_array = sparse_similarity_dict.to_coo()

# Get the underlying DOK (Dictionary of Keys) sparse array
dok_array = sparse_similarity_dict.to_dok()
```

### Generating New Dictionaries

You can generate new dictionaries from existing ones, with options to convert between sparse and dense formats:

```python
# Generate a new sparse dictionary
new_sparse_dict = sparse_similarity_dict.generate_dict(
    sparse_similarity_dict.to_coo() * 0.75
)

# Generate a dense dictionary from a sparse one
dense_dict = sparse_similarity_dict.generate_dict(
    sparse_similarity_dict.to_numpy(),
    dense=True  # This parameter converts to a dense NumpyNDArrayWrappedDict
)
```

### When to Use Sparse Arrays

Use `SparseArrayWrappedDict` when:
- Your data is mostly zeros (sparse)
- You're working with large dimensions where memory usage is a concern
- You need to perform operations that are optimized for sparse matrices

Use `NumpyNDArrayWrappedDict` when:
- Your data has few zeros (dense)
- You need faster element-wise access
- You're working with smaller dimensions where memory usage is less of a concern

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "npdict",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "dictionary, numpy",
    "author": null,
    "author_email": "Kwan Yuet Stephen Ho <stephenhky@yahoo.com.hk>",
    "download_url": "https://files.pythonhosted.org/packages/70/0b/a6e4ecff109c9fdd3353ef1a5c58261ac3ae0d54cb0459c6704405022146/npdict-0.0.4.tar.gz",
    "platform": null,
    "description": "\n[![GitHub release](https://img.shields.io/github/release/stephenhky/npdict.svg?maxAge=3600)](https://github.com/stephenhky/npdict/releases)\n[![pypi](https://img.shields.io/pypi/v/npdict.svg?maxAge=3600)](https://pypi.org/project/npdict/)\n[![download](https://img.shields.io/pypi/dm/npdict.svg?maxAge=2592000&label=installs&color=%2327B1FF)](https://pypi.org/project/npdict/)\n[![Documentation Status](https://readthedocs.org/projects/npdict/badge/?version=latest)](https://npdict.readthedocs.io/en/latest/?badge=latest)\n\n# `npdict`: Python Package for Dictionary Wrappers for Numpy Arrays\n\nThis Python package, `npdict`, aims at facilitating holding numerical\nvalues in a Python dictionary, but at the same time retaining the\nultra-high performance supported by NumPy. It supports an object\nwhich is a Python dictionary on the surface, but numpy behind the\nback, facilitating fast assignment and retrieval of values\nand fast computation of numpy arrays.\n\n## Installation\n\nTo install, in your terminal, simply enter:\n\n```\npip install npdict\n```\n\n## Quickstart\n\n### Instantiation\n\nSuppose you are doing a similarity dictionary between two sets of words.\nAnd each of these sets have words:\n\n```\ndocument1 = ['president', 'computer', 'tree']\ndocument2 = ['chairman', 'abacus', 'trees']\n```\n\nAnd you can build a dictionary like this:\n\n```\nimport numpy as np\nfrom npdict import NumpyNDArrayWrappedDict\n\nsimilarity_dict = NumpyNDArrayWrappedDict([document1, document2])\n```\n\nAn `npdict.NumpyNDArrayWrappedDict` instance is instantiated. It is \na Python dict:\n\n```\nisinstance(similarity_dict, dict)  # which gives `True`\n```\n\nIt has a matrix inside with default value 0.0 (and the initial default value can\nbe changed to other values when the instance is instantiated.)\n\n```\nsimilarity_dict.to_numpy()\n```\ngiving\n```\narray([[0., 0., 0.],\n       [0., 0., 0.],\n       [0., 0., 0.]])\n```\n\n### Value Assignments\n\nNow you can assign values just like what you do to a Python dictionary:\n\n```\nsimilarity_dict['president', 'chairman'] = 0.9\nsimilarity_dict['computer', 'abacus'] = 0.7\nsimilarity_dict['tree', 'trees'] = 0.95\n```\n\nAnd it has changed the inside numpy array to be:\n\n```\narray([[0.9  , 0.  , 0.  ],\n       [0.  , 0.7 , 0.  ],\n       [0.  , 0.  , 0.95]])\n```\n\n### Generation of New Object from the Old One\n\nIf you want to create another dict using the same words, but \na manipulation of the original value, 25 percent discount\nof the original one for example, you can do something like this:\n\n```\nnew_similarity_dict = similarity_dict.generate_dict(similarity_dict.to_numpy()*0.75)\n```\n\nAnd you got a new dictionary with numpy array to be:\n\n```\nnew_similarity_dict.to_numpy()\n```\ngiving\n```\narray([[0.675 , 0.    , 0.    ],\n       [0.    , 0.525 , 0.    ],\n       [0.    , 0.    , 0.7125]])\n```\n\nThis is a simple operation. But the design of this wrapped Python\ndictionary is that you can perform any fast or optimized operation\non your numpy array (using numba or Cython, for examples),\nwhile retaining the keywords as your dictionary.\n\n### Retrieval of Values\n\nAt the same time, you can set new values just like above, or retrieve\nvalues as if it is a Python dictionary:\n\n```\nsimilarity_dict['president', 'chairman']\n```\n\n### Conversion to a Python Dictionary\n\nYou can also convert this to an ordinary Python dictionary:\n\n```\nraw_similarity_dict = similarity_dict.to_dict()\n```\n\n### Instantiation from a Python Dictionary\n\nAnd you can convert a Python dictionary of this type back to \n`npdict.NumpyNDArrayWrappedDict` by (recommended)\n\n```\nnew_similarity_dict_2 = NumpyNDArrayWrappedDict.from_dict_given_keywords([document1, document2], raw_similarity_dict)\n```\n\nOr you can even do this (not recommended):\n\n```\nnew_similarity_dict_3 = NumpyNDArrayWrappedDict.from_dict(raw_similarity_dict)\n```\n\nIt is not recommended because the order of the keys are not retained in this way.\nUse it with caution.\n\n## Working with Sparse Arrays\n\nFor large, sparse matrices where most elements are zero, using `SparseArrayWrappedDict` can be more memory-efficient than the standard `NumpyNDArrayWrappedDict`.\n\n### Instantiation\n\nSimilar to the regular dictionary wrapper, you can instantiate a sparse array wrapper:\n\n```python\nfrom npdict import SparseArrayWrappedDict\n\ndocument1 = ['president', 'computer', 'tree', 'car', 'house', 'book']\ndocument2 = ['chairman', 'abacus', 'trees', 'vehicle', 'building', 'paper']\n\n# Create a sparse dictionary - efficient for large, sparse matrices\nsparse_similarity_dict = SparseArrayWrappedDict([document1, document2])\n```\n\n### Value Assignments\n\nAssign values just like with a regular dictionary:\n\n```python\n# Only assign values for the few non-zero elements\nsparse_similarity_dict['president', 'chairman'] = 0.9\nsparse_similarity_dict['computer', 'abacus'] = 0.7\nsparse_similarity_dict['tree', 'trees'] = 0.95\n```\n\nThe sparse implementation only stores the non-zero values, making it memory-efficient for large, sparse matrices.\n\n### Converting Between Formats\n\nYou can convert between dense and sparse formats:\n\n```python\n# Convert to NumPy array (dense format)\ndense_array = sparse_similarity_dict.to_numpy()\n\n# Convert to COO format (another sparse format)\ncoo_array = sparse_similarity_dict.to_coo()\n\n# Get the underlying DOK (Dictionary of Keys) sparse array\ndok_array = sparse_similarity_dict.to_dok()\n```\n\n### Generating New Dictionaries\n\nYou can generate new dictionaries from existing ones, with options to convert between sparse and dense formats:\n\n```python\n# Generate a new sparse dictionary\nnew_sparse_dict = sparse_similarity_dict.generate_dict(\n    sparse_similarity_dict.to_coo() * 0.75\n)\n\n# Generate a dense dictionary from a sparse one\ndense_dict = sparse_similarity_dict.generate_dict(\n    sparse_similarity_dict.to_numpy(),\n    dense=True  # This parameter converts to a dense NumpyNDArrayWrappedDict\n)\n```\n\n### When to Use Sparse Arrays\n\nUse `SparseArrayWrappedDict` when:\n- Your data is mostly zeros (sparse)\n- You're working with large dimensions where memory usage is a concern\n- You need to perform operations that are optimized for sparse matrices\n\nUse `NumpyNDArrayWrappedDict` when:\n- Your data has few zeros (dense)\n- You need faster element-wise access\n- You're working with smaller dimensions where memory usage is less of a concern\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A Python dictionary wrapper for numpy arrays",
    "version": "0.0.4",
    "project_urls": {
        "Documentation": "https://npdict.readthedocs.io/",
        "Issues": "https://github.com/stephenhky/npdict/issues",
        "Repository": "https://github.com/stephenhky/npdict"
    },
    "split_keywords": [
        "dictionary",
        " numpy"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "a795a39f3e9eaeccdf56224bbd5b6cfec4f2b794161ecb18b1aea05a4f550571",
                "md5": "398ade35ad5bb4931647f3aa0abf234c",
                "sha256": "8436c93141e67da9fb2060ecd8c2fb9927648d6c2c638bf8410862726fe456e5"
            },
            "downloads": -1,
            "filename": "npdict-0.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "398ade35ad5bb4931647f3aa0abf234c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 9834,
            "upload_time": "2025-08-02T02:45:52",
            "upload_time_iso_8601": "2025-08-02T02:45:52.965461Z",
            "url": "https://files.pythonhosted.org/packages/a7/95/a39f3e9eaeccdf56224bbd5b6cfec4f2b794161ecb18b1aea05a4f550571/npdict-0.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "700ba6e4ecff109c9fdd3353ef1a5c58261ac3ae0d54cb0459c6704405022146",
                "md5": "4d2e3f8242b9853adf1b94a21dc11910",
                "sha256": "6976eb1d9321e581d66aff1953cd7121f7f5ab29548a7fc9a6b9a164852f2a9d"
            },
            "downloads": -1,
            "filename": "npdict-0.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "4d2e3f8242b9853adf1b94a21dc11910",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 18261,
            "upload_time": "2025-08-02T02:45:53",
            "upload_time_iso_8601": "2025-08-02T02:45:53.752565Z",
            "url": "https://files.pythonhosted.org/packages/70/0b/a6e4ecff109c9fdd3353ef1a5c58261ac3ae0d54cb0459c6704405022146/npdict-0.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-02 02:45:53",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "stephenhky",
    "github_project": "npdict",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "npdict"
}
        
Elapsed time: 0.65310s