关于#numpy#的问题,如何解决?

请用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))

  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7738791
  • 除此之外, 这篇博客: 5、numpy数组统计分析之坐标矩阵、统计函数、排序中的 二、数据处理函数 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
    • 条件逻辑:np.where(条件,赋值,否则赋值)
    • 均值:a.mean()或np.mean(a)
    • 求和:a.sum()或np.sum(a)
    • ndarray.函数(轴):按轴计算
    • ndarray[条件].函数():用于数组中满足条件的元素的计算
    • ndarray.all():数组中全部是True(非0)的布尔值
    • ndarray.any():数组中包含True(非0)的布尔值
    • np.all/any(条件,axis=轴,keepdims=布尔值):ndarray中,在指定的轴上,各元素全是/包含指定条件,则返回布尔值
      ndarray没有指定条件是,默认非0返回True,其他返回False;
      ndarray指定条件时,满足条件,则返回布尔值;
      axis没指定时,默认定位数组全部元素。

    统计函数图例
    在这里插入图片描述
    在这里插入图片描述

  • 以下回答由chatgpt基于相关博客总结生成:
    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]]