请用numpy库完成以下任务:
(1)生成由10个随机整数组成的一维数组scores,整数的取值范围为[0,100]。
(2)在(1)的基础上,求该数组的和。
(3)在(1)的基础上,求该数组的平均值。
(4)在(1)的基础上,求该数组的最小值。
(5)在(1)的基础上,将每个值的加上5。
(6)在(1)的基础上,将该一维数组转换成2×5的二维数组。
https://ask.csdn.net/questions/7933127?spm=1001.2014.3001.5505
几乎一样的问题。。。
import numpy as np
scores = np.random.randint(0, 101, size=10)
sum_scores = np.sum(scores)
mean_scores = np.mean(scores)
min_scores = np.min(scores)
scores_plus_5 = scores + 5
two_dim_scores = scores.reshape((2, 5))
统计函数图例
import numpy as np
# 1. 用numpy库生成一个一维数组scores,数组长度为10,数组中每个元素都是[0,100]之间的随机整数。
scores = np.random.randint(0, 101, 10)
print(f"生成的一维数组为:{scores}\n")
# 2. 计算scores数组的总和。
scores_sum = np.sum(scores)
print(f"scores数组的总和为:{scores_sum}\n")
# 3. 计算scores数组的平均值。
scores_avg = np.mean(scores)
print(f"scores数组的平均值为:{scores_avg}\n")
# 4. 找出scores数组中的最小值。
scores_min = np.min(scores)
print(f"scores数组中的最小值为:{scores_min}\n")
# 5. 将scores数组中每个元素加上5。
scores_add5 = scores + 5
print(f"将scores数组中每个元素加上5后得到:{scores_add5}\n")
# 6. 将scores数组转换成一个2×5的二维数组。
scores_2d = np.reshape(scores, (2, 5))
print(f"转换成2*5的二维数组后得到:\n{scores_2d}")
输出结果:
生成的一维数组为:[73 55 8 69 86 49 68 75 97 61]
scores数组的总和为:641
scores数组的平均值为:64.1
scores数组中的最小值为:8
将scores数组中每个元素加上5后得到:[78 60 13 74 91 54 73 80 102 66]
转换成2*5的二维数组后得到:
[[73 55 8 69 86]
[49 68 75 97 61]]