Numpy面试练习题

一、说明

  • 原项目地址
      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 the numpy package under the name np (★☆☆)
[1]: import numpy as np
  1. Print the numpy version and the configuration (★☆☆)
[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)]
  1. Create a null vector of size 10 (★☆☆)
[4]: np.zeros(10)
[5]: np.empty(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
  1. How to find the memory size of any array (★☆☆)
[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
  1. How to get the documentation of the numpy add function from the command line? (★☆☆)
[9]: np.info(np.random)
  1. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)
[10]:temp = np.zeros(10)
	 temp[4] = 1
	 temp
	 
array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])
  1. Create a vector with values ranging from 10 to 49 (★☆☆)
[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])
  1. Reverse a vector (first element becomes last) (★☆☆)
[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])
  1. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)
[14]:np.arange(9).reshape((3,3))

array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
  1. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)
[15]:np.nonzero([1,2,0,0,4,0])

(array([0, 1, 4]),)
  1. Create a 3x3 identity matrix (★☆☆)
[16]:np.identity(3)

array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
  1. Create a 3x3x3 array with random values (★☆☆)
[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]]])
  1. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)
[18]:temp = np.random.random((10,10))
	 print(temp)
	 print(np.max(temp))
	 print(np.min(temp))
  1. Create a random vector of size 30 and find the mean value (★☆☆)
[19]:temp = np.random.random(30)
	 temp.mean()
	 
0.5470484712402423
  1. Create a 2d array with 1 on the border and 0 inside (★☆☆)
[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.]])
  1. How to add a border (filled with 0’s) around an existing array? (★☆☆)
[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.]])
  1. 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
[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
  1. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)
[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]]
  1. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)
[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.]])
  1. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?
[32]:np.unravel_index(99,(6,7,8))

(1, 5, 3)

你可能感兴趣的:(Python基础)