|
238 | 238 | "1. [numpy.argmin](#argmin)\n", |
239 | 239 | "1. [numpy.argsort](#argsort)\n", |
240 | 240 | "1. [numpy.asarray*](#asarray)\n", |
| 241 | + "1. [numpy.bincount](#bincount)\n", |
241 | 242 | "1. [numpy.bitwise_and](#bitwise_and)\n", |
242 | 243 | "1. [numpy.bitwise_or](#bitwise_and)\n", |
243 | 244 | "1. [numpy.bitwise_xor](#bitwise_and)\n", |
|
612 | 613 | "print('a == c: {}'.format(a is c))" |
613 | 614 | ] |
614 | 615 | }, |
| 616 | + { |
| 617 | + "cell_type": "markdown", |
| 618 | + "metadata": {}, |
| 619 | + "source": [ |
| 620 | + "## bincount\n", |
| 621 | + "\n", |
| 622 | + "`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.bincount.htm\n", |
| 623 | + "\n", |
| 624 | + "This method counts number of occurrences of each value in array of non-negative integers. A method accepts a single positional, and two keyword arguments, `weights`, and `minlength`. The positional arguments is assumed to be a one-dimensional array of unsigned integers that are either 8 or 16 bits wide. For other types, a `TypeError` is raised.\n", |
| 625 | + "\n", |
| 626 | + "The `weights` keyword argument can be supplied to apply weigths to the counts. In this case, the return value is a `float` array, otherwise, it is a `uint16`. The `weights` are assumed to be a one-dimensional `ndarray`, otherwise, a `TypeError` is raised. \n", |
| 627 | + "\n", |
| 628 | + "`minlength` can be supplied to give a lower bound to the length of the `ndarray` returned. In this case, the unused slots are filled with zeros." |
| 629 | + ] |
| 630 | + }, |
| 631 | + { |
| 632 | + "cell_type": "code", |
| 633 | + "execution_count": 15, |
| 634 | + "metadata": {}, |
| 635 | + "outputs": [ |
| 636 | + { |
| 637 | + "name": "stdout", |
| 638 | + "output_type": "stream", |
| 639 | + "text": [ |
| 640 | + "a: array([0, 1, 2, 3, 4, 5, 6, 7], dtype=uint8)\n", |
| 641 | + "bincount(a): array([1, 1, 1, 1, 1, 1, 1, 1], dtype=uint16)\n", |
| 642 | + "\n", |
| 643 | + "a: array([0, 0, 1, 1, 1, 2, 2, 2, 2], dtype=uint8)\n", |
| 644 | + "bincount(a): array([2, 3, 4], dtype=uint16)\n", |
| 645 | + "\n", |
| 646 | + "a: array([0, 1, 2, 3, 4, 5, 6, 7], dtype=uint8)\n", |
| 647 | + "w: array([0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.8], dtype=float64)\n", |
| 648 | + "bincount(a, weights=w): array([0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.8], dtype=float64)\n", |
| 649 | + "\n", |
| 650 | + "a: array([0, 1, 2, 3], dtype=uint8)\n", |
| 651 | + "bincount(a, minlength=8): array([1, 1, 1, 1, 0, 0, 0, 0], dtype=uint16)\n", |
| 652 | + "\n", |
| 653 | + "\n" |
| 654 | + ] |
| 655 | + } |
| 656 | + ], |
| 657 | + "source": [ |
| 658 | + "%%micropython -unix 1\n", |
| 659 | + "\n", |
| 660 | + "from ulab import numpy as np\n", |
| 661 | + "\n", |
| 662 | + "a = np.array(range(8), dtype=np.uint8)\n", |
| 663 | + "\n", |
| 664 | + "print(f'a: {a}\\nbincount(a): {np.bincount(a)}')\n", |
| 665 | + "\n", |
| 666 | + "a = np.array([0, 0, 1, 1, 1, 2, 2, 2, 2], dtype=np.uint8)\n", |
| 667 | + "print(f'\\na: {a}\\nbincount(a): {np.bincount(a)}')\n", |
| 668 | + "\n", |
| 669 | + "a = np.array(range(8), dtype=np.uint8)\n", |
| 670 | + "w = np.array([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.8])\n", |
| 671 | + "\n", |
| 672 | + "print(f'\\na: {a}\\nw: {w}\\nbincount(a, weights=w): {np.bincount(a, weights=w)}')\n", |
| 673 | + "\n", |
| 674 | + "a = np.array(range(4), dtype=np.uint8)\n", |
| 675 | + "print(f'\\na: {a}\\nbincount(a, minlength=8): {np.bincount(a, minlength=8)}')\n" |
| 676 | + ] |
| 677 | + }, |
615 | 678 | { |
616 | 679 | "cell_type": "markdown", |
617 | 680 | "metadata": {}, |
|
2944 | 3007 | ], |
2945 | 3008 | "metadata": { |
2946 | 3009 | "kernelspec": { |
2947 | | - "display_name": "Python 3.8.5 ('base')", |
| 3010 | + "display_name": "base", |
2948 | 3011 | "language": "python", |
2949 | 3012 | "name": "python3" |
2950 | 3013 | }, |
|
3006 | 3069 | "_Feature" |
3007 | 3070 | ], |
3008 | 3071 | "window_display": false |
3009 | | - }, |
3010 | | - "vscode": { |
3011 | | - "interpreter": { |
3012 | | - "hash": "9e4ec6f642f986afcc9e252c165e44859a62defc5c697cae6f82c2943465ec10" |
3013 | | - } |
3014 | 3072 | } |
3015 | 3073 | }, |
3016 | 3074 | "nbformat": 4, |
|
0 commit comments