<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>np.__config__<jupyter_output><empty_output><jupyter_text>3. Create a null vector of size 10 (★☆☆) (**hint**: np.zeros)<jupyter_code>np.zeros((5))<jupyter_output><empty_output><jupyter_text>4.  How to find the memory size of any array (★☆☆) (**hint**: size, itemsize)<jupyter_code>Z = np.zeros((5,5))
print(f'size of Z is {Z.size * Z.itemsize}')<jupyter_output>size of Z is 200<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 becomes the shape of the output).
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
    This condition is broadcast over the input. At locations where the
    condition is True, the `out` array will be set to the ufunc result.
    Elsewhere, the `out` array will retain its original value.
    Note that if an uninitialized `out` array is created via the default
    ``out=None`[...]<jupyter_text>6.  Create a null vector of size 10 but the fifth value which is 1 (★☆☆) (**hint**: array\[4\])<jupyter_code>A=np.zeros(10)
A[4]=1
A<jupyter_output><empty_output><jupyter_text>7.  Create a vector with values ranging from 10 to 49 (★☆☆) (**hint**: np.arange)<jupyter_code>A=np.arange(10,49)
print(A)<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]<jupyter_text>8.  Reverse a vector (first element becomes last) (★☆☆) (**hint**: array\[::-1\])<jupyter_code>A[::-1]<jupyter_output><empty_output><jupyter_text>9.  Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆) (**hint**: reshape)<jupyter_code>A=np.arange(0,9)
print(A)
A=A.reshape(3,3)
print(A)<jupyter_output>[0 1 2 3 4 5 6 7 8]
[[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>A.nonzero<jupyter_output><empty_output><jupyter_text>11. Create a 3x3 identity matrix (★☆☆) (**hint**: np.eye)<jupyter_code>A=np.eye(3)
print(A)<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>A=np.random.random((3,3,3))
print(A)<jupyter_output>[[[0.20470616 0.18338524 0.40880609]
  [0.52669853 0.53810966 0.56846926]
  [0.61442939 0.42230461 0.61085203]]

 [[0.12789847 0.1465746  0.64672775]
  [0.23147168 0.26567182 0.51088151]
  [0.04537899 0.42202509 0.70613061]]

 [[0.60133225 0.7367813  0.50640159]
  [0.28772089 0.09175548 0.47411911]
  [0.34179516 0.74607327 0.78788033]]]<jupyter_text>13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆) (**hint**: min, max)<jupyter_code>A = np.random.random((10,10))
print(A.min())<jupyter_output>0.0009351465994474895<jupyter_text>14. Create a random vector of size 30 and find the mean value (★☆☆) (**hint**: mean)<jupyter_code>Z=np.random.random(1000000)
Z.mean()<jupyter_output><empty_output><jupyter_text>15. Create a 2d array with 1 on the border and 0 inside (★☆☆) (**hint**: array\[1:-1, 1:-1\])<jupyter_code>Z=np.zeros((10,10))
Z[1:2, 1:6] = 1
print(Z)<jupyter_output>[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 1. 1. 1. 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. 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>16. How to add a border (filled with 0's) around an existing array? (★☆☆) (**hint**: np.pad)<jupyter_code>Z=np.ones((10,10))
print(np.pad(Z,(1,1)))
np.info(np.pad)<jupyter_output>[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
 pad(*args, **kwargs)

Pad an array.

Parameters
----------
array : array_like of rank N
    The array to pad.
pad_width : {sequence, array_like, int}
    Number of values padded to the edges of each axis.
    ((before_1, after_1), ... (before_N, after_N)) unique pad widths
    for each axis.
    ((before, after),) yields same before and after pad for each axis.
    (pad,) or int is a shortcut for before = after = pad width for all
    axes.
mode : str or function, optional
    One of the following string values or a user sup[...]<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)  19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆) (**hint**: array\[::2\])  20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? (**hint**: np.unravel_index)  21. Create a checkerboard 8x8 matrix using the tile function (★☆☆) (**hint**: np.tile)  22. Normalize a 5x5 random matrix (★☆☆) (**hint**: (x - mean) / std)  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 | @)  25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆) (**hint**: >, <=)<jupyter_code>Z = np.arange(11)
Z[(3 < Z) & (Z <= 8)] *= -1
print(Z)<jupyter_output>[ 0  1  2  3 -4 -5 -6 -7 -8  9 10]<jupyter_text>26. What is the output of the following script? (★☆☆) (**hint**: np.sum) ```python Author: <NAME>print(sum(range(5),-1))from numpy import *print(sum(range(5),-1))```<jupyter_code>print(sum(range(5),-1))
from numpy import *
print(sum(range(5),-1))<jupyter_output>9
10<jupyter_text>27. Consider an integer vector Z, which of these expressions are legal? (★☆☆) ```pythonZ**Z2 > 2Z <- Z1j*ZZ/1/1ZZ```  28. What are the result of the following expressions? ```pythonnp.array(0) / np.array(0)np.array(0) // np.array(0)np.array([np.nan]).astype(int).astype(float)```<jupyter_code>np.array([np.nan]).astype(int).astype(float)<jupyter_output><empty_output><jupyter_text>29. How to round away from zero a float array ? (★☆☆) (**hint**: np.uniform, np.copysign, np.ceil, np.abs)<jupyter_code>Z = np.random.uniform(-10,+10,10)
print(Z)<jupyter_output>[-0.13604097 -2.71796662 -8.99387748  5.62241739 -2.86744216 -6.5337753
 -4.78588007 -2.78621686 -2.62402572 -6.01022518]<jupyter_text>30. How to find common values between two arrays? (★☆☆) (**hint**: np.intersect1d)  31. How to ignore all numpy warnings (not recommended)? (★☆☆) (**hint**: np.seterr, np.errstate)  32. Is the following expressions true? (★☆☆) (**hint**: imaginary number) ```pythonnp.sqrt(-1) == np.emath.sqrt(-1)```  33. How to get the dates of yesterday, today and tomorrow? (★☆☆) (**hint**: np.datetime64, np.timedelta64)  34. How to get all the dates corresponding to the month of July 2016? (★★☆) (**hint**: np.arange(dtype=datetime64\['D'\]))  35. How to compute ((A+B)\*(-A/2)) in place (without copy)? (★★☆) (**hint**: np.add(out=), np.negative(out=), np.multiply(out=), np.divide(out=))  36. Extract the integer part of a random array using 5 different methods (★★☆) (**hint**: %, np.floor, np.ceil, astype, np.trunc)  37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆) (**hint**: np.arange)<jupyter_code>np.arange(9)<jupyter_output><empty_output><jupyter_text>38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆) (**hint**: np.fromiter)  39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆) (**hint**: np.linspace)<jupyter_code>A=np.linspace(0,1,11)
print(A.size)<jupyter_output>11<jupyter_text>40. Create a random vector of size 10 and sort it (★★☆) (**hint**: sort)<jupyter_code>A=np.random.random(10)
A.sort()
print(A)<jupyter_output>[0.03202866 0.03866467 0.19398945 0.27102179 0.37319433 0.58208554
 0.64004509 0.71973038 0.80019672 0.928157  ]
{'fraction_non_alphanumeric': 0.18118503118503118, 'fraction_numerical': 0.11112266112266113, 'mean_word_length': 3.7115572967678747, 'pattern_counts': {'":': 0, '<': 84, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '23', 'dir': 'jupyter-structured-clean-dedup', 'id': '477367', 'license': 'MIT', 'n_tokens_mistral': 4561, 'n_tokens_neox': 3887, 'n_words': 1296, 'path': '100_Numpy_exercises_with_hint.ipynb', 'repo_name': 'zhiyuzh/numpy-101', 'size': '9629'}