|
1 | 1 |
|
2 | 2 | # 100 numpy exercises |
3 | 3 |
|
4 | | -This is a collection of exercises that have been collected in the numpy mailing |
5 | | -list, on stack overflow and in the numpy documentation. I've also created some |
6 | | -to reach the 100 limit. The goal of this collection is to offer a quick |
7 | | -reference for both old and new users but also to provide a set of exercices for |
8 | | -those who teach. |
| 4 | +This is a collection of exercises that have been collected in the numpy mailing list, on stack overflow and in the numpy documentation. The goal of this collection is to offer a quick reference for both old and new users but also to provide a set of exercices for those who teach. |
9 | 5 |
|
10 | | -If you find an error or think you've a better way to solve some of them, feel |
11 | | -free to open an issue at <https://github.com/rougier/numpy-100> |
| 6 | + |
| 7 | +If you find an error or think you've a better way to solve some of them, feel free to open an issue at <https://github.com/rougier/numpy-100> |
12 | 8 |
|
13 | 9 | #### 1. Import the numpy package under the name `np` (★☆☆) |
14 | 10 |
|
@@ -43,7 +39,10 @@ print("%d bytes" % (Z.size * Z.itemsize)) |
43 | 39 |
|
44 | 40 | #### 5. How to get the documentation of the numpy add function from the command line? (★☆☆) |
45 | 41 |
|
46 | | -$ `python -c "import numpy; numpy.info(numpy.add)"` |
| 42 | + |
| 43 | +```python |
| 44 | +%run `python -c "import numpy; numpy.info(numpy.add)"` |
| 45 | +``` |
47 | 46 |
|
48 | 47 | #### 6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆) |
49 | 48 |
|
@@ -651,6 +650,11 @@ Z = np.ones(10) |
651 | 650 | I = np.random.randint(0,len(Z),20) |
652 | 651 | Z += np.bincount(I, minlength=len(Z)) |
653 | 652 | print(Z) |
| 653 | + |
| 654 | +# Another solution |
| 655 | +# Author: Bartosz Telenczuk |
| 656 | +np.add.at(Z, I, 1) |
| 657 | +print(Z) |
654 | 658 | ``` |
655 | 659 |
|
656 | 660 | #### 65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★) |
|
0 commit comments