一、练习题1
- 导入 numpy模块
import numpy as np
- 创建一个长度为10的一维全为0的ndarray对象,然后让第5个元素等于1
n = np.zeros(10,dtype=np.int32)
n[4] = 1
n
# 执行结果
array([0, 0, 0, 0, 1, 0, 0, 0, 0, 0])
- 创建一个元素为从10到49的ndarray对象
n = np.arange(10,50)
n
# 执行结果
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])
- 将第2题的所有元素位置反转
n[::-1]
# 执行结果
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])
- 使用 np.random.random 创建一个 10 * 10 的ndarray对象,并打印出最大最小元素
n = np.random.random(size=(10,10))
display(n,np.max(n),np.min(n))
# 执行结果
array([[0.34156444, 0.55412338, 0.97559366, 0.96282451, 0.82301957,
0.91275603, 0.00564381, 0.83650483, 0.44904509, 0.1162804 ],
[0.12687926, 0.49060794, 0.58335149, 0.97119897, 0.88324556,
0.14541761, 0.87426693, 0.36096634, 0.70017914, 0.94123502],
[0.14150848, 0.05665892, 0.85008689, 0.2503025 , 0.81570171,
0.42551545, 0.37296926, 0.83983705, 0.09469496, 0.31333237],
[0.89021428, 0.2281922 , 0.16046708, 0.98281193, 0.06559717,
0.15631928, 0.25707421, 0.16688749, 0.74073207, 0.95631189],
[0.05977675, 0.26797098, 0.9118984 , 0.38625465, 0.33810171,
0.6976046 , 0.59535338, 0.30631281, 0.91639903, 0.66059256],
[0.37988545, 0.67403555, 0.37156141, 0.84371145, 0.64571415,
0.91492496, 0.75448415, 0.68062946, 0.63368574, 0.29835 ],
[0.42899809, 0.67818689, 0.64472523, 0.21611191, 0.46770244,
0.86989707, 0.81548425, 0.36219811, 0.82315194, 0.94993409],
[0.25202363, 0.25665528, 0.74148035, 0.92144265, 0.15776939,
0.00306655, 0.94057909, 0.46908839, 0.44494692, 0.08529181],
[0.56447017, 0.58032801, 0.15675499, 0.78259373, 0.10015014,
0.58493712, 0.41774184, 0.7363931 , 0.00877337, 0.42751915],
[0.26258318, 0.18940144, 0.23747884, 0.92743148, 0.08156698,
0.79222988, 0.04752766, 0.64974775, 0.03317872, 0.15055297]])
0.9828119291613719
0.003066552297036451
- 创建一个 10 * 10 的 ndarray 对象,且矩阵边界全为1,里面全为0
# 方式一
n = np.zeros((10,10),dtype=np.int16)
n[[0,-1]] = 1
n[:,[0,-1]] = 1
n
# 执行结果
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]], dtype=int16)
# 方式二
n = np.ones((10,10),dtype=np.int16)
n[1:-1,1:-1] = 0
n
# 执行结果
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]], dtype=int16)
- 创建一个每一行都是从 0 到 4 的 5 * 5矩阵
l = [0,1,2,3,4]
n = np.array(l*5).reshape((5,5))
n
# 执行结果
array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])
- 创建一个范围在(0,1)之间的长度为12的等差数列
n = np.linspace(0,1,12)
n
# 执行结果
array([0. , 0.09090909, 0.18181818, 0.27272727, 0.36363636,
0.45454545, 0.54545455, 0.63636364, 0.72727273, 0.81818182,
0.90909091, 1. ])
- 创建一个长度为10的随机数组并排序
n = np.random.randint(0,10,size=10)
# 默认为升序
n.sort()
# 降序排序
n1 = n[::-1]
display(n,n1)
# 执行结果
array([2, 2, 3, 5, 5, 7, 7, 8, 8, 8])
array([8, 8, 8, 7, 7, 5, 5, 3, 2, 2])
- 创建一个长度为10的随机数组并将最大值替换为0
n = np.random.randint(0,10,size=10)
display(n)
# 执行结果
array([7, 8, 1, 7, 8, 6, 9, 9, 1, 4])
# 获取最大值
max1 = np.max(n)
max1
# 所有最大值对应的索引
max_indexs = np.argwhere(n == max1).reshape(-1)
n[max_indexs] = 0
n
# 执行结果
array([7, 8, 1, 7, 8, 6, 0, 0, 1, 4])
二、练习题2
- 导入 numpy模块
import numpy as np
- 给定一个4维矩阵,如何得到最后两维的和?
n = np.random.randint(0,10,size=(2,3,4,5))
n
# 第一个维度是axis=0
# 第二个维度是axis=1
# 第三个维度是axis=2
# 第四个维度是axis=3
n.sum(axis=(2,3))
# 执行结果
array([[80, 88, 87],
[90, 64, 84]])
- 给定数组[1,2,3,4,5],如何得到在这个数组的每个元素之间插入3个0后的新数组?
n = np.arange(1,6)
n1 = np.zeros(17,dtype=np.int16)
n1[::4] = n
n1
# 执行结果
array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5], dtype=int16)
- 给定一个二维矩阵,如何交互其中两行的元素?
n = np.random.randint(0,10,size=(4,4))
display(n)
# 可以利用索引交换行
n1 = n[[1,0,2,3]]
display(n1)
# 执行结果
array([[0, 0, 7, 8],
[1, 4, 0, 4],
[1, 0, 3, 2],
[9, 4, 2, 9]])
array([[1, 4, 0, 4],
[0, 0, 7, 8],
[1, 0, 3, 2],
[9, 4, 2, 9]])
- 创建一个100000长度的随机数组,使用两种方法对其求三次方,并比较所用时间
n = np.random.randint(0,10,size=100000)
display(n)
# 方式一
%timeit np.power(n,3)
# 执行结果
array([2, 1, 2, ..., 8, 5, 5])
649 µs ± 12.1 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
# 方式二
%timeit n**3
# 执行结果
666 µs ± 27.2 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
- 创建一个 5 * 3 随机矩阵和一个 3 * 2 随机矩阵,求矩阵积
n1 = np.random.randint(0,100,size=(5,3))
n2 = np.random.randint(0,100,size=(3,2))
np.dot(n1,n2)
# 执行结果
array([[9508, 2341],
[5602, 1053],
[4515, 1122],
[9462, 2013],
[5541, 998]])
- 矩阵的每一行的元素都减去该行的平均值
n = np.random.randint(0,10,size=(3,4))
display(n)
n2 = n.mean(axis=1).reshape(3,1)
display(n2)
n - n2
#执行结果
array([[2, 4, 4, 0],
[1, 6, 3, 3],
[9, 2, 5, 5]])
array([[2.5 ],
[3.25],
[5.25]])
Out[21]:
array([[-0.5 , 1.5 , 1.5 , -2.5 ],
[-2.25, 2.75, -0.25, -0.25],
[ 3.75, -3.25, -0.25, -0.25]])
- 打印出以下矩阵(要求使用np.zeros创建 8 * 8 的矩阵)
[[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]]
n = np.zeros((8,8),dtype=int)
n[::2,1::2] = 1
n[1::2,::2] = 1
n
# 执行结果
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]])
- 正则化一个 5 * 5 随机矩阵
- 正则的概念:加入a是矩阵中的一个元素,max/min分别是矩阵元素的最大最小值,则正则化后 a = (a – min)/(max – min)
n = np.random.randint(0,100,size=(5,5))
display(n)
# 执行结果
array([[46, 16, 24, 78, 34],
[ 6, 4, 15, 79, 44],
[18, 67, 72, 55, 70],
[21, 19, 94, 88, 1],
[77, 55, 39, 86, 69]])
min1 = n.min()
max1 = n.max()
min1,max1
# 执行结果
(1, 94)
# 正则化后 a = (a - min)/(max - min)
# 归化:0-1 之间
(n - min1)/(max1 - min1)
# 执行结果
array([[0.48387097, 0.16129032, 0.24731183, 0.82795699, 0.35483871],
[0.05376344, 0.03225806, 0.15053763, 0.83870968, 0.46236559],
[0.1827957 , 0.70967742, 0.76344086, 0.58064516, 0.74193548],
[0.21505376, 0.19354839, 1. , 0.93548387, 0. ],
[0.8172043 , 0.58064516, 0.40860215, 0.91397849, 0.7311828 ]])
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...





