一、綜合練習題(教材第29頁)
1.“大潤發“、"沃爾瑪“、“好德”和“農工商”四個超市都賣蘋果、香蕉、橘子和芒果四種水果。使用 NumPy 的 ndarray 實現以下功能。
- 創建兩個一維數組分別存儲超市名稱和水果名稱。
- 創建一個 4x4 的二維數組存儲不同超市的水果價格,其中價格由 4~10 範圍內的隨機數生成。
- 選擇“大潤發”的蘋果和“好德”的香蕉,並將價格增加 1 元。
- “農工商”水果大減價,所有水果價格減 2 元。
- 統計四個超市蘋果和芒果的銷售均價。
- 找出橘子價格最貴的超市名稱(不是編號)。
2.基於 2.3 節中隨機遊走的例子,使用 ndarray 和隨機數生成函數模擬一個物體在三維空間隨機遊走的過程。
- 創建 3x10 的二維數組,記錄物體每步在三個軸向上的移動距離。在每個軸向的移動距離服從標準正態分佈(期望爲 o, 方差爲 1) 。行序 0 、1 、2 分別對應 x 軸、 y 軸和z 軸。
- 計算每步走完後物體在三維空間的位置。
- 計算每步走完後物體到原點的距離(只顯示兩位小數)。
- 統計物體在 z 軸上到達的最遠距離。
- 統計物體在三維空間距離原點的最近值。
【提示】使用 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站瞧一瞧?)