题目来源:
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.py1. Import the numpy package under the name np (★☆☆)
In [2]:
import numpy as np2. Print the numpy version and the configuration (★☆☆)
In [3]:
import numpy as np
1.21.53. 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]:
85. 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 `.
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
更新!!! 14. Create a random vector of size 30 and find the mean value (★☆☆)v = np.random.rand(30) print(np.mean(v))
0.494726560107459715. Create a 2d array with 1 on the border and 0 inside (★☆☆)
v = np.ones((6,6)) v[1:-1,1:-1]=0 v
Out[26]:
array([[1., 1., 1., 1., 1., 1.],
[1., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 1.],
[1., 1., 1., 1., 1., 1.]])
16. How to add a border (filled with 0's) around an existing array? (★☆☆)
# v = np.zeros((6,6)) # n = np.ones((6,1)) # v = np.concatenate((v,n),axis = 1) 太麻烦 # v v = np.ones((6,6)) v = np.pad(v,pad_width=1,mode = 'constant',constant_values = 0) v
Out[37]:
array([[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]])
17. What is the result of the following expression? (★☆☆)
0 * np.nan np.nan == np.nan np.inf > np.nan np.nan - np.nan np.nan in set([np.nan]) 0.3 == 3 * 0.1
nan False False nan False False18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)
v = np.diag(np.arange(4),k = -1) v
Out[44]:
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 2, 0, 0],
[0, 0, 0, 3, 0]])
19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)
v = np.zeros((8,8)) v[1::2,::2] = 1 #奇数行 v[::2,1::2] = 1 #偶数行 v
Out[45]:
array([[0., 1., 0., 1., 0., 1., 0., 1.],
[1., 0., 1., 0., 1., 0., 1., 0.],
[0., 1., 0., 1., 0., 1., 0., 1.],
[1., 0., 1., 0., 1., 0., 1., 0.],
[0., 1., 0., 1., 0., 1., 0., 1.],
[1., 0., 1., 0., 1., 0., 1., 0.],
[0., 1., 0., 1., 0., 1., 0., 1.],
[1., 0., 1., 0., 1., 0., 1., 0.]])
20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? (★☆☆)
#还真不会这道题,这个函数就是找到indices:100在形状为(6,7,8)的矩阵中的索引 np.unravel_index(100,(6,7,8))
21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)a = [[0,1],[1,0]] np.tile(a,(4,4)) #推荐直接看源码的例子
Out[52]:
array([[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0]])
22. Normalize a 5x5 random matrix (★☆☆)
v= np.random.random((5,5)) vmax = np.max(v) vmin = np.min(v) v = (v-vmin)/(vmax-vmin) v
Out[54]:
array([[0.09613539, 0.07216039, 0.23781477, 0.78103247, 0.23050097],
[0.73417177, 0. , 0.16407059, 0.62265722, 0.00627843],
[1. , 0.51208786, 0.02543944, 0.41028657, 0.51019465],
[0.4328451 , 0.645893 , 0.16872833, 0.22964477, 0.24975819],
[0.21880174, 0.71034552, 0.37408293, 0.91981415, 0.92219423]])
23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)
color = np.dtype([('R',np.ubyte,1),
('G',np.ubyte,1),
('B',np.ubyte,1),
('A',np.ubyte,1),])
color
Out[55]:
dtype([('R', 'u1'), ('G', 'u1'), ('B', 'u1'), ('A', 'u1')])
24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)
v = np.dot(np.zeros((5,3)),np.zeros((3,2))) #5x3 @ 3x2 = 5x2 没错 v
Out[56]:
array([[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.]])



