题目来源:
GitHub - rougier/numpy-100: 100 numpy exercises (with solutions)
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 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 GitHub - rougier/numpy-100: 100 numpy exercises (with solutions).
File automatically generated. See the documentation to update questions/answers/hints programmatically.
Run the initialize.py
module, then for each question you can query the answer or an hint with hint(n)
or answer(n)
for n
question number.
In [1]:
%run initialise.py
1. Import the numpy package under the name np
(★☆☆)
In [2]:
import numpy as np
2. Print the numpy version and the configuration (★☆☆)
In [3]:
import numpy as np
1.21.5 <function show at 0x000001E1DC0981F0>
3. Create a null vector of size 10 (★☆☆)
In [4]:
null = np.zeros(10)
null
Out[4]:
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
4. How to find the memory size of any array (★☆☆)
In [6]:
print(null.size*null.itemsize)
80
In [7]:
print(null.size) #数组大小
Out[7]:
10
In [8]:
print(null.itemsize) #元素所占内存
Out[8]:
8
5. How to get the documentation of the numpy add function from the command line? (★☆☆)
In [9]:
np.info(np.add)
#np.add? 笔记本支持
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``, locations within it where the condition is False will remain uninitialized. **kwargs For other keyword-only arguments, see the :ref:`ufunc docs <ufuncs.kwargs>`. Returns ------- add : ndarray or scalar The sum of `x1` and `x2`, element-wise. This is a scalar if both `x1` and `x2` are scalars. Notes ----- Equivalent to `x1` + `x2` in terms of array broadcasting. Examples -------- >>> np.add(1.0, 4.0) 5.0 >>> x1 = np.arange(9.0).reshape((3, 3)) >>> x2 = np.arange(3.0) >>> np.add(x1, x2) array([[ 0., 2., 4.], [ 3., 5., 7.], [ 6., 8., 10.]]) The ``+`` operator can be used as a shorthand for ``np.add`` on ndarrays. >>> x1 = np.arange(9.0).reshape((3, 3)) >>> x2 = np.arange(3.0) >>> x1 + x2 array([[ 0., 2., 4.], [ 3., 5., 7.], [ 6., 8., 10.]])
6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)
In [10]:
arr = np.zeros(10) #np.array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])
arr[4] = 1
arr
Out[10]:
array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])
7. Create a vector with values ranging from 10 to 49 (★☆☆)
In [11]:
v = np.array(range(10,50)) #np.arange(10,50)
v
Out[11]:
array([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])
8. Reverse a vector (first element becomes last) (★☆☆)
In [13]:
v = np.arange(50)
v = v[::-1]
v
Out[13]:
array([49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)
In [14]:
v = np.arange(9).reshape(3,3)
v
Out[14]:
array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)
In [15]:
v= np.nonzero([1,2,0,0,4,0])
v
Out[15]:
(array([0, 1, 4], dtype=int64),)
11. Create a 3x3 identity matrix (★☆☆)
In [17]:
v = np.eye(3) #单位矩阵
v
Out[17]:
array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
12. Create a 3x3x3 array with random values (★☆☆)
In [19]:
v = np.random.rand(3,3,3) #np.random.random(3,3,3)
v
Out[19]:
array([[[0.73527078, 0.60789469, 0.32748788], [0.59787423, 0.13201964, 0.29240533], [0.68464728, 0.51757985, 0.86813884]], [[0.07694192, 0.16003625, 0.87576183], [0.24639116, 0.95007728, 0.87826383], [0.91503418, 0.600087 , 0.48932514]], [[0.85289026, 0.92871151, 0.18701809], [0.27678334, 0.71768381, 0.56805309], [0.3233431 , 0.23818919, 0.89645149]]])
In [21]:
v = np.random.random((3,3,3))
v
Out[21]:
array([[[0.70226196, 0.85231412, 0.10218497], [0.28434651, 0.3388226 , 0.41760094], [0.48735007, 0.10301863, 0.70954303]], [[0.0985171 , 0.23593811, 0.20304492], [0.24775831, 0.66249119, 0.83378528], [0.39347008, 0.37838472, 0.57364063]], [[0.2369906 , 0.68533942, 0.28761067], [0.37078408, 0.39534982, 0.11435091], [0.59695905, 0.12687256, 0.29725503]]])
13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)
In [22]:
v = np.random.rand(10,10)
print(np.max(v),np.min(v))
0.9895919394060008 0.005890203026950314