原项目地址
Github地址:https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises.md
在线练习地址:https://hub.gke.mybinder.org/user/rougier-numpy-100-kthvetgj/notebooks/100_Numpy_exercises.ipynb
原题目也是英文,我并没有提供翻译,希望增加自己的英文阅读水平。如果对题目以及方法有不懂的地方,可以到文档了解更多。
Numpy中文文档地址:https://www.numpy.org.cn/
Numpy英文文档地址:https://numpy.org/doc/stable/
[1]: import numpy as np
[2]: np.__version__
'1.17.4'
[3]: np.show_config()
blas_mkl_info:
NOT AVAILABLE
blis_info:
NOT AVAILABLE
openblas_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/local/lib']
language = c
define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/local/lib']
language = c
define_macros = [('HAVE_CBLAS', None)]
lapack_mkl_info:
NOT AVAILABLE
openblas_lapack_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/local/lib']
language = c
define_macros = [('HAVE_CBLAS', None)]
lapack_opt_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/local/lib']
language = c
define_macros = [('HAVE_CBLAS', None)]
[4]: np.zeros(10)
[5]: np.empty(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
[6]: x = np.empty(10,dtype="int64")
y = np.zeros(10,dtype="float32")
[7]: x.itemsize * x.size
80
[8]: y.itemsize * y.size
40
[9]: np.info(np.random)
[10]:temp = np.zeros(10)
temp[4] = 1
temp
array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])
[11]:np.arange(10,50)
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])
[12]:np.flipud(np.arange(10))
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
[13]:np.arange(10)[::-1]
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
[14]:np.arange(9).reshape((3,3))
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
[15]:np.nonzero([1,2,0,0,4,0])
(array([0, 1, 4]),)
[16]:np.identity(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
[17]:np.random.random((3,3,3))
array([[[0.21159848, 0.84697373, 0.61578889],
[0.63471677, 0.5486937 , 0.43472706],
[0.11671133, 0.38004242, 0.10978385]],
[[0.06436487, 0.87575193, 0.37946734],
[0.01652253, 0.51239309, 0.41160512],
[0.27659719, 0.08318487, 0.58435711]],
[[0.29876001, 0.03213723, 0.96619526],
[0.75473948, 0.24142761, 0.36869255],
[0.02434352, 0.01669259, 0.95600846]]])
[18]:temp = np.random.random((10,10))
print(temp)
print(np.max(temp))
print(np.min(temp))
[19]:temp = np.random.random(30)
temp.mean()
0.5470484712402423
[20]:temp = np.ones((10,10))
temp
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., 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., 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.]])
[21]:temp[1:-1, 1:-1] = 0
temp
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
[22]:np.pad(temp,1,"constant",constant_values=0)
array([[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., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 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.]])
[23]:0 * np.nan
nan
[24]:np.nan == np.nan
False
[25]:np.inf > np.nan
False
[26]:np.nan - np.nan
nan
[27]:np.nan in set([np.nan])
True
[28]:0.3 == 3 * 0.1
False
[29]:import math
math.isclose(0.3, 3 * 0.1)
True
[30]:np.diag([1, 2, 3, 4], -1)
[[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]]
[31]:temp = np.zeros((8, 8))
temp[::2, ::2] = 1
temp
array([[1., 0., 1., 0., 1., 0., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 1., 0., 1., 0., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 1., 0., 1., 0., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 1., 0., 1., 0., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]])
[32]:np.unravel_index(99,(6,7,8))
(1, 5, 3)