<jupyter_start><jupyter_text>100 numpy exercises with hintThis 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 exercises for those who teach.If you find an error or think you've a better way to solve some of them, feel free to open an issue at   1. Import the numpy package under the name `np` (★☆☆) (**hint**: import … as …)<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>2. Print the numpy version and the configuration (★☆☆) (**hint**: np.\_\_version\_\_, np.show\_config)<jupyter_code>print(np.__version__)
np.show_config<jupyter_output>1.16.4<jupyter_text>3. Create a null vector of size 10 (★☆☆) (**hint**: np.zeros)<jupyter_code>a = np.zeros(10)
print (a)<jupyter_output>[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]<jupyter_text>4.  How to find the memory size of any array (★☆☆) (**hint**: size, itemsize)<jupyter_code>np.size(a)<jupyter_output><empty_output><jupyter_text>5.  How to get the documentation of the numpy add function from the command line? (★☆☆) (**hint**: np.info)<jupyter_code>np.info(np.add)<jupyter_output>add(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

Add arguments element-wise.

Parameters
----------
x1, x2 : array_like
    The arrays to be added.  If ``x1.shape != x2.shape``, they must be
    broadcastable to a common shape (which may be the shape of one or
    the other).
out : ndarray, None, or tuple of ndarray and None, optional
    A location into which the result is stored. If provided, it must have
    a shape that the inputs broadcast to. If not provided or `None`,
    a freshly-allocated array is returned. A tuple (possible only as a
    keyword argument) must have length equal to the number of outputs.
where : array_like, optional
    Values of True indicate to calculate the ufunc at that position, values
    of False indicate to leave the value in the output alone.
**kwargs
    For other keyword-only arguments, see the
    :ref:`ufunc docs <ufuncs.kwargs>`.

Returns
-------
add : ndarray or scalar
    The[...]<jupyter_text>6.  Create a null vector of size 10 but the fifth value which is 1 (★☆☆) (**hint**: array\[4\])<jupyter_code>b = np.zeros(10)
b[4] = 1
print(b)<jupyter_output>[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]<jupyter_text>7.  Create a vector with values ranging from 10 to 49 (★☆☆) (**hint**: np.arange)<jupyter_code>c = np.arange(10, 50)
print(c)<jupyter_output>[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]<jupyter_text>8.  Reverse a vector (first element becomes last) (★☆☆) (**hint**: array\[::-1\])<jupyter_code>c[::-1]<jupyter_output><empty_output><jupyter_text>9.  Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆) (**hint**: reshape)<jupyter_code>d = np.arange(0, 9)
d = d.reshape(3,3)
print(d)<jupyter_output>[[0 1 2]
 [3 4 5]
 [6 7 8]]<jupyter_text>10. Find indices of non-zero elements from \[1,2,0,0,4,0\] (★☆☆) (**hint**: np.nonzero)<jupyter_code>e = [1,2,0,0,4,0]
np.nonzero(e)<jupyter_output><empty_output><jupyter_text>11. Create a 3x3 identity matrix (★☆☆) (**hint**: np.eye)<jupyter_code>f = np.identity(3)
print(f)<jupyter_output>[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]<jupyter_text>12. Create a 3x3x3 array with random values (★☆☆) (**hint**: np.random.random)<jupyter_code>g = np.random.random((3,3,3))
print(g)<jupyter_output>[[[0.91935742 0.36980996 0.63239118]
  [0.701471   0.34652984 0.45266535]
  [0.88779327 0.85467891 0.6511243 ]]

 [[0.48847737 0.75460451 0.84924678]
  [0.43308303 0.38578117 0.49390477]
  [0.79987427 0.84084545 0.63658596]]

 [[0.45571064 0.46747478 0.20050871]
  [0.50084413 0.74153154 0.61159307]
  [0.98453028 0.13942641 0.02752447]]]<jupyter_text>13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆) (**hint**: min, max)<jupyter_code>h = np.random.random((10,10))
the_min = h.min()
the_max = h.max()
print(the_min, the_max)<jupyter_output>0.03438449042130165 0.99685921492039<jupyter_text>14. Create a random vector of size 30 and find the mean value (★☆☆) (**hint**: mean)<jupyter_code>i = np.random.random(30)
print(i)
print(i.mean())<jupyter_output>[0.98229406 0.76319149 0.01521597 0.12270004 0.09709939 0.63163652
 0.84934682 0.41812771 0.37042801 0.1704732  0.92295256 0.8451368
 0.2300568  0.47114757 0.11324947 0.0866101  0.76099842 0.6187652
 0.72896365 0.04658593 0.37410417 0.67037695 0.26682772 0.15384245
 0.66111648 0.65789138 0.21297752 0.53450014 0.68700025 0.78932302]
0.4750979929082625<jupyter_text>15. Create a 2d array with 1 on the border and 0 inside (★☆☆) (**hint**: array\[1:-1, 1:-1\])<jupyter_code>j = np.ones((7,7))
j[1:-1,1:-1] = 0
print(j)<jupyter_output>[[1. 1. 1. 1. 1. 1. 1.]
 [1. 0. 0. 0. 0. 0. 1.]
 [1. 0. 0. 0. 0. 0. 1.]
 [1. 0. 0. 0. 0. 0. 1.]
 [1. 0. 0. 0. 0. 0. 1.]
 [1. 0. 0. 0. 0. 0. 1.]
 [1. 1. 1. 1. 1. 1. 1.]]<jupyter_text>16. How to add a border (filled with 0's) around an existing array? (★☆☆) (**hint**: np.pad)<jupyter_code>np.pad(j)<jupyter_output><empty_output><jupyter_text>17. What is the result of the following expression? (★☆☆) (**hint**: NaN = not a number, inf = infinity) ```python0 * np.nannp.nan == np.nannp.inf > np.nannp.nan - np.nannp.nan in set([np.nan])0.3 == 3 * 0.1```  18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆) (**hint**: np.diag)<jupyter_code>k = np.diag(1+np.arange(4),k=-1)

print(k)<jupyter_output>[[0 0 0 0 0]
 [1 0 0 0 0]
 [0 2 0 0 0]
 [0 0 3 0 0]
 [0 0 0 4 0]]<jupyter_text>19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆) (**hint**: array\[::2\])<jupyter_code>l = np.ones((8,8))
l[1::2, ::2] = 0
l[fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, ::1] = 0
print(l)<jupyter_output>[[1. 1. 1. 1. 1. 1. 1. 1.]
 [0. 1. 0. 1. 0. 1. 0. 1.]
 [0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0.]]<jupyter_text>20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? (**hint**: np.unravel_index)<jupyter_code>print(np.unravel_index(99,(6,7,8)))<jupyter_output>(1, 5, 3)<jupyter_text>21. Create a checkerboard 8x8 matrix using the tile function (★☆☆) (**hint**: np.tile)  22. Normalize a 5x5 random matrix (★☆☆) (**hint**: (x - mean) / std)<jupyter_code>m = np.random.random((5,5))
print(m)
n = np.mean(m)
print(n)
(m - n) / np.std(m)<jupyter_output>[[0.29477212 0.6941612  0.45343631 0.69422264 0.42338902]
 [0.93600978 0.65719029 0.44480287 0.5678587  0.59252591]
 [0.99117736 0.85686279 0.89826968 0.58580198 0.20813812]
 [0.73534337 0.45985061 0.48890976 0.13004943 0.54380639]
 [0.41840839 0.17818111 0.77969998 0.10013925 0.22157688]]
0.5341833581117029<jupyter_text>23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆) (**hint**: np.dtype)  24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆) (**hint**: np.dot | @)<jupyter_code>a = np.random.random((5,3)) 
b = np.random.random((3,4))
a @ b<jupyter_output><empty_output><jupyter_text>25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆) (**hint**: >, <=)<jupyter_code>c = np.arange(23)
print(c)

d = c[(c<3) | (c >= 8)] *= -1
print(d)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup