【数据分析与智能计算】第二章: 综合练习题及答案讲解

一、综合练习题(教材第29页)

1.“大润发“、"沃尔玛“、“好德”和“农工商”四个超市都卖苹果、香蕉、橘子和芒果四种水果。使用 NumPy 的 ndarray 实现以下功能。

  1. 创建两个一维数组分别存储超市名称和水果名称。
  2. 创建一个 4x4 的二维数组存储不同超市的水果价格,其中价格由 4~10 范围内的随机数生成。
  3. 选择“大润发”的苹果和“好德”的香蕉,并将价格增加 1 元。
  4. “农工商”水果大减价,所有水果价格减 2 元。
  5. 统计四个超市苹果和芒果的销售均价。
  6. 找出橘子价格最贵的超市名称(不是编号)。

2.基于 2.3 节中随机游走的例子,使用 ndarray 和随机数生成函数模拟一个物体在三维空间随机游走的过程。

  1. 创建 3x10 的二维数组,记录物体每步在三个轴向上的移动距离。在每个轴向的移动距离服从标准正态分布(期望为 o, 方差为 1) 。行序 0 、1 、2 分别对应 x 轴、 y 轴和z 轴。
  2. 计算每步走完后物体在三维空间的位置。
  3. 计算每步走完后物体到原点的距离(只显示两位小数)。
  4. 统计物体在 z 轴上到达的最远距离。
  5. 统计物体在三维空间距离原点的最近值。

【提示】使用 abs()绝对值函数对 z 轴每步运动后的位置求绝对值,然后求最大距离。


二、参考答案

第一题:

import numpy as np
# 1.创建两个一维数组分别存储超市名称和水果名称。
shops = np.array(['DaRunFa','Walmart','HaoDe','NongGongShang'])
fruits = np.array(['apple','banana','orange','mango'])
# 2.创建一个 4x4 的二维数组存储不同超市的水果价格,其中价格由 4~10 范围内的随机数生成。 
prices = np.random.randint(4,10,16).reshape(4,4)
# 3.选择“大润发”的苹果和“好德”的香蕉,并将价格增加 1 元。 
prices[shops == 'DaRunFa',fruits == 'apple'] += 1
print('the price of apple in DaRunFa now: %d' %prices[shops == 'DaRunFa',fruits == 'apple'])
prices[shops == 'HaoDe',fruits == 'banana'] += 1
print('the price of banana in HaoDe now: %d' %prices[shops == 'HaoDe',fruits == 'banana'])
# 4.“农工商”水果大减价,所有水果价格减 2 元。 
prices[shops == 'NongGongShang'] -= 2 
print('the price in NongGongShang now: ',end='')
print(prices[shops == 'NongGongShang'])
# 5.统计四个超市苹果和芒果的销售均价。 
print('ave of apple is: %f'%prices[: , fruits == 'apple'].mean())
print('ave of mango is: %f'%prices[: , fruits == 'mango'].mean())
# 6.找出橘子价格最贵的超市名称(不是编号)。
t = 0
for i in range(0,4) :
    if prices[i, 2] > prices[t, 2] :
        t = i
print('the most expensive orange is in %s'%shops[t])

输出结果:(因代码中涉及随机数,所以结果可能不一样)

the price of apple in DaRunFa now: 7
the price of banana in HaoDe now: 5
the price in NongGongShang now: [[6 7 4 6]]
ave of apple is: 6.750000
ave of mango is: 6.750000
the most expensive orange is in Walmart

第二题:

import numpy as np
steps = 10
rndwlk = np.random.normal(0, 1, size = (3, steps))
print('1)移动距离数组:')
print(rndwlk)
position = rndwlk.cumsum(axis = 1)
x = position[0]
y = position[1]
z = position[2]
print('\n2)每步走完后在三维的空间位置:')
print(position)
dists = np.sqrt(position[0]**2 + position[1]**2 + position[2]**2) #三维直角坐标系的距离公式
np.set_printoptions(precision=2)
print('\n3)每步走完后到原点的距离:')
print(dists)
print('\n4)Z轴到达的最远距离:%f'%abs(position[2]).max())
print('\n5)物体在三维空间距离原点的最近值:%f'%dists.min())

输出如下:(因代码中涉及随机数,所以结果可能不一样)

1)移动距离数组:
[[ 0.09  0.52 -0.96 -0.96 -1.44  1.27 -0.61 -1.18  2.23  0.45]
 [-0.66 -2.22 -0.39 -0.25  0.36 -0.29  0.04  0.12  1.43  0.34]
 [ 0.56  0.56  0.96  0.33  2.15  1.56 -1.09 -2.05 -0.1  -0.48]]

2)每步走完后在三维的空间位置:
[[ 0.09  0.62 -0.34 -1.3  -2.74 -1.47 -2.08 -3.26 -1.03 -0.57]
 [-0.66 -2.87 -3.26 -3.51 -3.16 -3.44 -3.4  -3.29 -1.86 -1.51]
 [ 0.56  1.11  2.07  2.4   4.55  6.12  5.02  2.97  2.87  2.39]]

3)每步走完后到原点的距离:
[0.87 3.14 3.88 4.45 6.18 7.17 6.42 5.5  3.57 2.89]

4)Z轴到达的最远距离:6.116005

5)物体在三维空间距离原点的最近值:0.867622

三、讲解

第二题用到了一个我们不太熟悉的方法my_array.cumsum(),我们用一个直观的例子来理解:

arr = np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]])
print(arr) #二维数组

输出如下:

[[1 1 1 1]
 [2 2 2 2]
 [3 3 3 3]
 [4 4 4 4]]

print(arr.cumsum(axis=0)) #对上述二维数组arr按列累加

输出如下:

[[ 1  1  1  1]
 [ 3  3  3  3]
 [ 6  6  6  6]
 [10 10 10 10]]

print(arr.cumsum(axis=1)) #对上述二维数组arr按行累加

输出如下:

[[ 1  2  3  4]
 [ 2  4  6  8]
 [ 3  6  9 12]
 [ 4  8 12 16]]

通过上面的例子我们能直观地看明白my_array.cumsum()的作用以及按行、按列累加的不同。


如果你正在学习/复习“数据分析与智能计算”这门课,或者是想要入门大数据、人工智能的同学,欢迎订阅本专栏~
觉得有用的话,不要忘了点赞、关注、分享哦~大家多多包涵,有任何问题欢迎指正、讨论。
本文基于CC-BY-NC-SA 4.0协议,请规范转载。
(博客看累了?去我的B站瞧一瞧?)

猜你喜欢

转载自blog.csdn.net/qq_27133869/article/details/106360638