1.导入numpy库并简写为 np (★☆☆)
import numpy as np
参考答案
import numpy as np
2. 打印numpy的版本和配置说明 (★☆☆)
- (提示: np.version, np.show_config)
np.__version__
输出结果
'1.21.1'
print(np.show_config)
print("________________________")
print(np.show_config())
输出结果
<function show at 0x000001B6BFC9DAF0>
________________________
blas_mkl_info:
NOT AVAILABLE
blis_info:
NOT AVAILABLE
openblas_info:
library_dirs = ['D:\\a\\1\\s\\numpy\\build\\openblas_info']
libraries = ['openblas_info']
language = f77
define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
library_dirs = ['D:\\a\\1\\s\\numpy\\build\\openblas_info']
libraries = ['openblas_info']
language = f77
define_macros = [('HAVE_CBLAS', None)]
lapack_mkl_info:
NOT AVAILABLE
openblas_lapack_info:
library_dirs = ['D:\\a\\1\\s\\numpy\\build\\openblas_lapack_info']
libraries = ['openblas_lapack_info']
language = f77
define_macros = [('HAVE_CBLAS', None)]
lapack_opt_info:
library_dirs = ['D:\\a\\1\\s\\numpy\\build\\openblas_lapack_info']
libraries = ['openblas_lapack_info']
language = f77
define_macros = [('HAVE_CBLAS', None)]
Supported SIMD extensions in this NumPy install:
baseline = SSE,SSE2,SSE3
found = SSSE3,SSE41,POPCNT,SSE42,AVX,F16C,FMA3,AVX2
not found = AVX512F,AVX512CD,AVX512_SKX,AVX512_CLX,AVX512_CNL
None
参考答案
print(np.__version__)
print(np.show_config())
输出结果
1.21.1
blas_mkl_info:
NOT AVAILABLE
blis_info:
NOT AVAILABLE
openblas_info:
library_dirs = ['D:\\a\\1\\s\\numpy\\build\\openblas_info']
libraries = ['openblas_info']
language = f77
define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
library_dirs = ['D:\\a\\1\\s\\numpy\\build\\openblas_info']
libraries = ['openblas_info']
language = f77
define_macros = [('HAVE_CBLAS', None)]
lapack_mkl_info:
NOT AVAILABLE
openblas_lapack_info:
library_dirs = ['D:\\a\\1\\s\\numpy\\build\\openblas_lapack_info']
libraries = ['openblas_lapack_info']
language = f77
define_macros = [('HAVE_CBLAS', None)]
lapack_opt_info:
library_dirs = ['D:\\a\\1\\s\\numpy\\build\\openblas_lapack_info']
libraries = ['openblas_lapack_info']
language = f77
define_macros = [('HAVE_CBLAS', None)]
Supported SIMD extensions in this NumPy install:
baseline = SSE,SSE2,SSE3
found = SSSE3,SSE41,POPCNT,SSE42,AVX,F16C,FMA3,AVX2
not found = AVX512F,AVX512CD,AVX512_SKX,AVX512_CLX,AVX512_CNL
None
3. 创建一个长度为10的空向量 (★☆☆)
a = np.zeros(10)
a
输出结果
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
参考答案
Z = np.zeros(10)
print(Z)
输出结果
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
4. 如何找到任何一个数组的内存大小? (★☆☆)
print(a.size,a.itemsize)
输出结果
10 8
参考答案
Z = np.zeros((10,10))
print("%d bytes" % (Z.size * Z.itemsize))
输出结果
800 bytes
5. 如何从命令行得到numpy中add函数的说明文档? (★☆☆)
np.info(toplevel='add')
参考答案
np.info(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. 创建一个长度为10并且除了第五个值为1的空向量 (★☆☆)
array6 = np.zeros(10)
array6[4] = 1
array6
输出结果
array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])
参考答案
Z = np.zeros(10)
Z[4] = 1
print(Z)
输出结果
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
7. 创建一个值域范围从10到49的向量(★☆☆)
array7 = np.arange(10,50)
array7
输出结果
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])
参考答案
Z = np.arange(10,50)
print(Z)
输出结果
[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. 反转一个向量(第一个元素变为最后一个) (★☆☆)
array8 = np.arange(10)
print(array8,"\n-----\n",type(array8),"\n-----\n",array8[::-1],"\n-----\n")
print("注意以下区别:")
print("只取向量最后一个元素:\n",array8[-1])
print("------------")
print("取向量第一个元素到倒数第二个:\n",array8[:-1])
print("------------")
print("反转向量:\n",array8[::-1])
输出结果
[0 1 2 3 4 5 6 7 8 9]
-----
<class 'numpy.ndarray'>
-----
[9 8 7 6 5 4 3 2 1 0]
-----
注意以下区别:
只取向量最后一个元素:
9
------------
取向量第一个元素到倒数第二个:
[0 1 2 3 4 5 6 7 8]
------------
反转向量:
[9 8 7 6 5 4 3 2 1 0]
array8 = np.arange(12).reshape(3,4)
print(array8,"\n-----\n",type(array8),"\n-----\n",array8[::-1],"\n-----\n")
print("注意以下区别:")
print("只取数组最后一行元素:\n",array8[-1])
print("------------")
print("注意与上一个不同:\n",array8[-1::])
print("------------")
print("取数组第一行元素到倒数第二行:\n",array8[:-1])
print("------------")
print("同上一个:\n",array8[:-1:])
print("------------")
print("数组最后一行到第一行,以此类推:\n",array8[::-1])
输出结果
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
-----
<class 'numpy.ndarray'>
-----
[[ 8 9 10 11]
[ 4 5 6 7]
[ 0 1 2 3]]
-----
注意以下区别:
只取数组最后一行元素:
[ 8 9 10 11]
------------
注意与上一个不同:
[[ 8 9 10 11]]
------------
取数组第一行元素到倒数第二行:
[[0 1 2 3]
[4 5 6 7]]
------------
同上一个:
[[0 1 2 3]
[4 5 6 7]]
------------
数组最后一行到第一行,以此类推:
[[ 8 9 10 11]
[ 4 5 6 7]
[ 0 1 2 3]]
参考答案
Z = np.arange(50)
Z = Z[::-1]
print(Z)
输出结果
[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. 创建一个 3x3 并且值从0到8的矩阵(★☆☆)
array9 = np.arange(0,9).reshape(3,3)
array9
输出结果
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
参考答案
Z = np.arange(9).reshape(3,3)
print(Z)
输出结果
[[0 1 2]
[3 4 5]
[6 7 8]]
10. 找到数组[1,2,0,0,4,0]中非0元素的位置索引 (★☆☆)
array10 = [1,2,0,0,4,0]
np.nonzero(array10)
输出结果
(array([0, 1, 4], dtype=int64),)
参考答案
nz = np.nonzero([1,2,0,0,4,0])
print(nz)
输出结果
(array([0, 1, 4], dtype=int64),)
11. 创建一个 3x3 的单位矩阵 (★☆☆)
array11 = np.eye(3,3)
array11
输出结果
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
参考答案
Z = np.eye(3)
print(Z)
输出结果
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
12. 创建一个 3x3x3的随机数组 (★☆☆)
array12 = np.random.random(27).reshape(3,3,3)
array12
输出结果
array([[[0.82229851, 0.97063002, 0.14368023],
[0.54370589, 0.03199196, 0.85705309],
[0.26821559, 0.15694076, 0.51977488]],
[[0.50242734, 0.29862912, 0.95963966],
[0.4059865 , 0.58694668, 0.82554167],
[0.56819152, 0.34225528, 0.45214493]],
[[0.01517539, 0.89008166, 0.0500397 ],
[0.03236231, 0.19130355, 0.53311733],
[0.09538655, 0.18586758, 0.58278421]]])
参考答案
Z = np.random.random((3,3,3))
print(Z)
输出结果
[[[0.28037142 0.44488439 0.30333044]
[0.1161553 0.82102038 0.25739631]
[0.09821049 0.36713993 0.92724096]]
[[0.2454338 0.87448758 0.50827198]
[0.54874904 0.07023286 0.75978993]
[0.98004792 0.42379336 0.98222663]]
[[0.43035063 0.32212228 0.22881998]
[0.97116268 0.10329178 0.19237496]
[0.89617807 0.79191292 0.51751459]]]
13. 创建一个 10x10 的随机数组并找到它的最大值和最小值 (★☆☆)
array13 = np.random.random(100).reshape(10,10)
print(array13,"\n最大值为\n",array13.max(),"\n最小值为:\n",array13.min())
输出结果
[[0.99778825 0.58095976 0.34966491 0.61488926 0.22311257 0.4618689
0.17235173 0.70900466 0.98204539 0.16713024]
[0.47931749 0.37411923 0.174987 0.4546529 0.18531676 0.75502785
0.13397944 0.45530322 0.43508141 0.95361024]
[0.17900297 0.93651411 0.87413906 0.73874406 0.52680645 0.64828029
0.24795946 0.88003906 0.62365578 0.80426776]
[0.45185357 0.51363758 0.96072332 0.04641699 0.48586224 0.62842817
0.27009438 0.18875272 0.19874009 0.66220609]
[0.44138034 0.2363918 0.74719564 0.22923504 0.27315476 0.53374481
0.18482755 0.50381315 0.25225246 0.75322919]
[0.35705613 0.51395832 0.51189048 0.09275676 0.75417333 0.06750219
0.43249999 0.62178538 0.46245924 0.65893803]
[0.27734846 0.24961469 0.44867329 0.96181551 0.69065328 0.21640466
0.59641193 0.12161551 0.54862819 0.67923836]
[0.21021391 0.32386752 0.37686985 0.85766783 0.73454876 0.69567777
0.79764775 0.28465536 0.4209324 0.35587512]
[0.15687547 0.69758606 0.3013893 0.53512701 0.99620145 0.22607022
0.11752308 0.54600039 0.9315894 0.63885842]
[0.95544494 0.55211529 0.96081971 0.92697433 0.44943367 0.52602786
0.64950368 0.40008766 0.59628704 0.09932262]]
最大值为
0.9977882475176502
最小值为:
0.046416985065966254
参考答案
Z = np.random.random((10,10))
Zmin, Zmax = Z.min(), Z.max()
print(Zmin, Zmax)
输出结果
0.004121552994131639 0.9861579216981144
14. 创建一个长度为30的随机向量并找到它的平均值 (★☆☆)
array14 = np.random.random(30)
print(array14,"\n-------\n",array14.mean())
输出结果
[0.93012289 0.87846182 0.39524015 0.29766567 0.535985 0.96190709
0.11612866 0.49828173 0.41661926 0.3161008 0.45526546 0.45677439
0.42808616 0.2136142 0.85161725 0.72934575 0.88087434 0.17033377
0.71800939 0.74070516 0.25254667 0.15898121 0.25780968 0.65958564
0.10230427 0.94045116 0.80328989 0.6013053 0.66213565 0.50255439]
-------
0.5310700936558221
参考答案
Z = np.random.random(30)
m = Z.mean()
print(m)
输出结果
0.3515583279019299
15. 创建一个二维数组,其中边界值为1,其余值为0 (★☆☆)
array15 = np.ones(9).reshape(3,3)
print(array15,"\n------\n")
array15[1:-1, 1:-1] = 0
print(array15)
输出结果
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
------
[[1. 1. 1.]
[1. 0. 1.]
[1. 1. 1.]]
参考答案
Z = np.ones((10,10))
Z[1:-1,1:-1] = 0
print(Z)
输出结果
[[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.]]
16. 对于一个存在在数组,如何添加一个用0填充的边界? (★☆☆)
- (提示: np.pad)
- np.pad用法详解
- np.pad(array, pad_width, mode=‘constant’, **kwargs)
- array:数组
- pad_width
- mode
- constant
- edge
- linear_ramp
- maximum
- mean
- median
- reflect
- symmetric
- wrap
- **kwargs
a = [1, 2, 3, 4, 5]
np.pad(a, (2, 3), 'constant', constant_values=(4, 6))
输出结果
array([4, 4, 1, 2, 3, 4, 5, 6, 6, 6])
np.pad(a, (2, 3), 'edge')
输出结果
array([1, 1, 1, 2, 3, 4, 5, 5, 5, 5])
np.pad(a, (2, 3), 'linear_ramp', end_values=(5, -4))
输出结果
array([ 5, 3, 1, 2, 3, 4, 5, 2, -1, -4])
np.pad(a, (2,), 'maximum')
输出结果
array([5, 5, 1, 2, 3, 4, 5, 5, 5])
np.pad(a, (2,), 'mean')
输出结果
array([3, 3, 1, 2, 3, 4, 5, 3, 3])
np.pad(a, (2,), 'median')
输出结果
array([3, 3, 1, 2, 3, 4, 5, 3, 3])
a = [[1, 2], [3, 4]]
print(a)
np.pad(a, ((3, 2), (2, 3)), 'minimum')
输出结果
[[1, 2], [3, 4]]
array([[1, 1, 1, 2, 1, 1, 1],
[1, 1, 1, 2, 1, 1, 1],
[1, 1, 1, 2, 1, 1, 1],
[1, 1, 1, 2, 1, 1, 1],
[3, 3, 3, 4, 3, 3, 3],
[1, 1, 1, 2, 1, 1, 1],
[1, 1, 1, 2, 1, 1, 1]])
a = [1, 2, 3, 4, 5]
np.pad(a, (2, 3), 'reflect')
输出结果
array([3, 2, 1, 2, 3, 4, 5, 4, 3, 2])
np.pad(a, (2, 3), 'reflect', reflect_type='odd')
输出结果
array([-1, 0, 1, 2, 3, 4, 5, 6, 7, 8])
np.pad(a, (2, 3), 'symmetric')
输出结果
array([2, 1, 1, 2, 3, 4, 5, 5, 4, 3])
np.pad(a, (2, 3), 'symmetric', reflect_type='odd')
输出结果
array([0, 1, 1, 2, 3, 4, 5, 5, 6, 7])
np.pad(a, (2, 3), 'wrap')
输出结果
array([4, 5, 1, 2, 3, 4, 5, 1, 2, 3])
def pad_with(vector, pad_width, iaxis, kwargs):
pad_value = kwargs.get('padder', 10)
vector[:pad_width[0]] = pad_value
vector[-pad_width[1]:] = pad_value
a = np.arange(6)
a = a.reshape((2, 3))
np.pad(a, 1, pad_with)
输出结果
array([[10, 10, 10, 10, 10],
[10, 0, 1, 2, 10],
[10, 3, 4, 5, 10],
[10, 10, 10, 10, 10]])
np.pad(a, 2, pad_with, padder=100)
输出结果
array([[100, 100, 100, 100, 100, 100, 100],
[100, 100, 100, 100, 100, 100, 100],
[100, 100, 0, 1, 2, 100, 100],
[100, 100, 3, 4, 5, 100, 100],
[100, 100, 100, 100, 100, 100, 100],
[100, 100, 100, 100, 100, 100, 100]])
array16 = np.arange(1,7)
array16 = array16.reshape(2, 3)
print(array16)
np.pad(array16, ((1, 1),(1,1)), 'constant', constant_values=(0, 0))
输出结果
[[1 2 3]
[4 5 6]]
array([[0, 0, 0, 0, 0],
[0, 1, 2, 3, 0],
[0, 4, 5, 6, 0],
[0, 0, 0, 0, 0]])
参考答案
Z = np.ones((5,5))
Z = np.pad(Z, pad_width=1, mode='constant', constant_values=0)
print(Z)
输出结果
[[0. 0. 0. 0. 0. 0. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 0. 0. 0. 0. 0. 0.]]
17. 以下表达式运行的结果分别是什么? (★☆☆)
- (提示: NaN = not a number, inf = infinity)
- 0 * np.nan
- np.nan == np.nan
- np.inf > np.nan
- np.nan - np.nan
- 0.3 == 3 * 0.1
参考答案
print("0 * np.nan输出为:",0 * np.nan)
print("np.nan == np.nan输出为:",np.nan == np.nan)
print("np.inf > np.nan输出为:",np.inf > np.nan)
print("np.nan - np.nan输出为:",np.nan - np.nan)
print("0.3 == 3 * 0.1输出为:",0.3 == 3 * 0.1)
输出结果
0 * np.nan输出为: nan
np.nan == np.nan输出为: False
np.inf > np.nan输出为: False
np.nan - np.nan输出为: nan
0.3 == 3 * 0.1输出为: False
18. 创建一个 5x5的矩阵,并设置值1,2,3,4落在其对角线下方位置 (★☆☆)
参考答案
Z = np.diag(1+np.arange(4),k=-1)
print(Z)
输出结果
[[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]]
19. 创建一个8x8 的矩阵,并且设置成棋盘样式 (★☆☆)
array19 = np.ones((8,8))
array19[1::2,::2]= 0
array19[::2,1::2]= 0
array19
输出结果
array([[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.],
[0., 1., 0., 1., 0., 1., 0., 1.]])
参考答案
Z = np.zeros((8,8),dtype=int)
Z[1::2,::2] = 1
Z[::2,1::2] = 1
print(Z)
输出结果
[[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. 考虑一个 (6,7,8) 形状的数组,其第100个元素的索引(x,y,z)是什么?
参考答案
print(np.unravel_index(100,(6,7,8)))
输出结果
(1, 5, 4)
|