1.导入numpy库并简写为np
import numpy as np

1.18.5

2.打印numpy的版本和配置说明
print(np.__version__)

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)]

3.创建一个长度为10的空向量
data = np.zeros(10)
print(data)

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

4.如何找到任何一个数组的内存大小
data = np.zeros((10,10))
size = data.size*data.itemsize
print(size)

800

5.如何从命令行得到numpy中add函数的说明文档?
print(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.]])

6.创建一个长度为10并且除了第五个值为1的空向量
data = np.zeros(10)
data[4] = 1
print(data)

[0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]

7.创建一个值域范围从10到49的向量
data = np.arange(10, 50)
print(data)

[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.反转一个向量(第一个元素变为最后一个)
data = np.arange(50)
data1 = data[::-1]
print(data1)

[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的矩阵
a = np.arange(9).reshape(3,3)
print(a)

[[0 1 2]

[3 4 5]

[6 7 8]]

10.找到数组[1,2,0,0,4,0]中非0元素的位置索引
data = np.array([1,2,0,0,4,0])
nz = np.nonzero(data)
print(nz)

(array([0, 1, 4]),)

11.创建一个3x3的单位矩阵
data = np.eye(3)
print(data)

[[1. 0. 0.]

[0. 1. 0.]

[0. 0. 1.]]

12.创建一个3x3x3的随机数组
data = np.random.random((3,3,3))
print(data)

[[[0.49636271 0.83838843 0.06417393]

 [0.82195645 0.51237583 0.0114491 ]

 [0.7405036  0.39659706 0.03227348]]

[[0.63851754 0.80021438 0.7920785 ]

 [0.2406759  0.67543697 0.42027336]

 [0.63085702 0.35717684 0.97860962]]

[[0.01086027 0.74921743 0.26180766]

 [0.86373045 0.89711615 0.69148172]

 [0.40816789 0.57048578 0.27933363]]]

13.创建一个10x10的随机数组并找到它的最大值和最小值
data = np.random.random((10,10))
data_min, data_max = data.min(), data.max()
print(data)
print(data_min, data_max)

[[0.75663674 0.17844001 0.85002126 0.13172174 0.29408908 0.41969569

 0.23361479 0.2108635  0.20592198 0.27844824]

[0.81355517 0.41224512 0.11997801 0.96950686 0.92275456 0.3488865

 0.75720993 0.74558365 0.975169   0.60246761]

[0.22106176 0.09018057 0.7895037  0.36480635 0.09659013 0.67309123

 0.46910627 0.1764413  0.84397167 0.74586522]

[0.57137615 0.31583    0.88894133 0.70353074 0.39971682 0.91511001

 0.76765151 0.5745941  0.77788015 0.79931621]

[0.03478835 0.41559061 0.71280684 0.16909797 0.46635441 0.8827292

 0.65676973 0.66978226 0.00355154 0.53219789]

[0.88288346 0.17108551 0.57297318 0.87288666 0.06630736 0.06485355

 0.24679334 0.27741585 0.08302555 0.75426484]

[0.36280452 0.64841807 0.97838234 0.88603573 0.1671354  0.59336731

 0.38955173 0.34285529 0.7836714  0.90464085]

[0.90640051 0.74722665 0.38180324 0.43325245 0.0741859  0.5579621

 0.81968112 0.14065454 0.05105436 0.69727726]

[0.54015284 0.73357179 0.57264243 0.96944606 0.48120064 0.52303426

 0.72974627 0.15313958 0.98512348 0.41098146]

[0.58610653 0.68952747 0.05254411 0.87168296 0.14973974 0.73087013

 0.95045341 0.00521367 0.26853988 0.36178675]]

0.0035515374660728405 0.9851234802087657

14.创建一个长度为30的随机向量并找到它的平均值
data = np.random.random(30)
print(data)
m = data.mean()
print(m)

[0.25621257 0.21803699 0.15008757 0.52329855 0.63721352 0.82697183

0.88605523 0.56303141 0.76514747 0.10767787 0.46170384 0.3991193

0.60063211 0.31583176 0.44926071 0.70317634 0.18852366 0.44927385

0.96359451 0.54583082 0.57153248 0.21319883 0.17595709 0.14272109

0.24723186 0.42819483 0.99494263 0.83336304 0.74526906 0.38350683]

0.491553254982445

15.创建一个二维数组,其中边界值为1,其余值为0
data = np.ones((10,10))
data[1:-1,1:-1] = 0
print(data)

[[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填充的边界
data = np.ones((5,5))
data1 = np.pad(data, pad_width=1, mode='constant', constant_values=0)
print(data1)

[[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.]]

# 填充两位
data1 = np.pad(data, pad_width=2, mode='constant', constant_values=0)
print(data1)

[[0. 0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 1. 1. 1. 1. 1. 0. 0.]

[0. 0. 1. 1. 1. 1. 1. 0. 0.]

[0. 0. 1. 1. 1. 1. 1. 0. 0.]

[0. 0. 1. 1. 1. 1. 1. 0. 0.]

[0. 0. 1. 1. 1. 1. 1. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 0.]]

# 两个轴前面填充两位,后面填充三位
data1 = np.pad(data, pad_width=(2,3), mode='constant', constant_values=0)
print(data1)

[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 1. 1. 1. 1. 1. 0. 0. 0.]

[0. 0. 1. 1. 1. 1. 1. 0. 0. 0.]

[0. 0. 1. 1. 1. 1. 1. 0. 0. 0.]

[0. 0. 1. 1. 1. 1. 1. 0. 0. 0.]

[0. 0. 1. 1. 1. 1. 1. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

# 竖轴前面填充一位,后面填充两位,横轴前面填充三位,后面填充四位
data1 = np.pad(data, pad_width=((1,2),(3,4)), mode='constant', constant_values=0)
print(data1)

[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 1. 1. 1. 1. 1. 0. 0. 0. 0.]

[0. 0. 0. 1. 1. 1. 1. 1. 0. 0. 0. 0.]

[0. 0. 0. 1. 1. 1. 1. 1. 0. 0. 0. 0.]

[0. 0. 0. 1. 1. 1. 1. 1. 0. 0. 0. 0.]

[0. 0. 0. 1. 1. 1. 1. 1. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

# model 取值
# 'constant'——表示连续填充相同的值,每个轴可以分别指定填充值,constant_values=(x, y)时前面用x填充,后面用y填充,缺省值填充0
# 'edge'——表示用边缘值填充
# 'linear_ramp'——表示用边缘递减的方式填充
# 'maximum'——表示最大值填充
# 'mean'——表示均值填充
# 'median'——表示中位数填充
# 'minimum'——表示最小值填充
# 'reflect'——表示对称填充
# 'symmetric'——表示对称填充
# 'wrap'——表示用原数组后面的值填充前面,前面的值填充后面
17.以下表达式运行结果是什么
print(0 * np.nan)

nan

print(np.nan == np.nan)

False

print(np.nan-np.nan)

nan

print(0.3 == 3*0.1)

False

a = 0.3
b = 3*0.1
print(a,b)

0.3 0.30000000000000004

18.创建一个5x5的矩阵,并设置值1,2,3,4落在其对角线下方位置
d = np.arange(1,5)
print('d:', d)
# 以d数组内容做为对角线
# k=-1 向下移动一格,k=1向上移动一格
data = np.diag(d, k=-1)
print(data)

d: [1 2 3 4]

[[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的矩阵,并且设置成棋盘样式
data = np.zeros((8,8), dtype=int)
# a::b 从索引为a开始 间隔b,a省略时默认为从0开始
data[1::2,::2] = 1 # 横轴从1开始,间隔2,竖轴从0开始,间隔2
data[::2,1::2] = 1 # 横轴从0开始,间隔2,竖轴从1开始,间隔2
print(data)

[[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)是什么
np.unravel_index(100,(6,7,8))

(1, 5, 4)

# 完整函数定义 unravel_index(indices, shape, order=‘C’)
# indices -- 就是索引值,可以是一个数,也可以是一个列表
# shape -- 代表的是数组的形状
# order -- 有两种取值 ‘C‘,’F’; 'C': 表示的是以横坐标为基准; 'F': 表示的是以纵坐标为基准
# 实例:已知数组 [2,3,4,5,6,7,8,1,12,23,34,55] 中index=7的值是1,把此数组转换成3x4的二维数组后1的索引是什么
a = [2,3,4,5,6,7,8,1,12,23,34,55]
b = np.array(a).reshape((3,4))
index = np.unravel_index(7,b.shape)
print('转换后的数据为:\n',b)
print('转换后的索引为:',index)
print('此索引的值为:',b[index])

转换后的数据为:

[[ 2  3  4  5]

[ 6  7  8  1]

[12 23 34 55]]

转换后的索引为: (1, 3)

此索引的值为: 1

21.用tile函数去创建一个8X8的棋盘样式矩阵
data = np.tile(np.array([[0,1],[1,0]]),(4,4))# 第二个参数(4,4)的意思是用原矩阵分别沿横轴扩大(平铺)4倍,沿竖轴扩大4倍
print(data)

[[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.对一个 5x5 的随机矩阵做归一化
data = np.random.random((5,5))
data_max, data_min = data.max(), data.min()
data = (data-data_min)/(data_max-data_min)
print(data)

[[0.60869233 0.9835157  0.21854819 1.         0.83353816]

[0.         0.68240932 0.45124496 0.88266605 0.31929441]

[0.16032999 0.81138498 0.91282807 0.4355519  0.41880903]

[0.56181584 0.60601155 0.9521618  0.79943604 0.96619814]

[0.01959091 0.80151255 0.916702   0.56048952 0.46647695]]

23.创建一个将颜色描述为(RGBA)四个无符号字节的自定义dtype
color = np.dtype([('r',np.ubyte, (1,)), ('g',np.ubyte, (1,)), ('b',np.ubyte, (1,)), ('a',np.ubyte, (1,))])
print(color)

[('r', 'u1', (1,)), ('g', 'u1', (1,)), ('b', 'u1', (1,)), ('a', 'u1', (1,))]

24.一个5x3与一个3x2的矩阵相乘,矩阵乘积是什么
data = np.dot(np.ones((5,3)), np.ones((3,2)))
print(data)

[[3. 3.]

[3. 3.]

[3. 3.]

[3. 3.]

[3. 3.]]

25.给定一个一维数组,对其在3到8之间的所有元素取反
data = np.arange(10)
data[(3<data)&(data<=8)]*=-1
print(data)

[ 0  1  2  3 -4 -5 -6 -7 -8  9]

data = np.arange(10)
data[4:9]*=-1
print(data)

[ 0  1  2  3 -4 -5 -6 -7 -8  9]