零均值和方差爲我給出一個函數的定義,請按以下步驟實現:實現在numpy的
# Problem 1 - Apply zero mean and zero variance scale to the image features
def normalize(data):
pass
然後提供使用numpy
一個單元測試,會堅持我的成功實施
編輯
這不我的單元測試,但通過該課程的教師指定。
np.testing.assert_array_almost_equal(
normalize(np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])),
np.array([-0.4, -0.3, -0.2, -0.099, 0.0, 0.099, 0.199, 0.3, 0.4, 0.5]),
decimal=3)
我的解決辦法是
def normalize(data):
return ((data - data.mean())/data.max() - data.min())
但因爲我得到
AssertionError:
Arrays are not almost equal to 3 decimals
(mismatch 100.0%)
x: array([-1.45, -1.35, -1.25, -1.15, -1.05, -0.95, -0.85, -0.75, -0.65, -0.55])
y: array([-0.4 , -0.3 , -0.2 , -0.099, 0. , 0.099, 0.199, 0.3 ,
0.4 , 0.5 ])
用google搜索,我也發現
(data - data.mean())/data.std()
必須有我的理解差距
經過更多搜索我試圖
(data - data.mean())/data.var()
但是這兩種解決方案都沒有正確聲明。
那麼這裏的正確實施是什麼?
爲什麼選擇投票? –