1 回答

TA貢獻1796條經驗 獲得超4個贊
當您使用 NumPy 數組時,我將嘗試在部分中解決這個問題,就好像它們是列表一樣,因此首先失去了庫的很多目的。盡管語法更加緊湊,但它帶來了顯著的速度提升。
創造人口
這個很簡單。我們可以直接替換生成pop
使用numpy.random.randint
。我們需要為 和 指定值population_size
并chromosome length
使用這些值來指定輸出大小。
population_size = 6 chromosome_length = 10 pop = np.random.randint(0, 2, (population_size, chromosome_length))
注意:這不會給出與您在實際問題中包含的完全相同的值,因為我們沒有為隨機數生成器設置種子。但是,代碼直接等效于您的for
循環,但性能更高。
生成expected
我無法準確替換本節,因為替換你的循環太多了,一些變量也未定義。所以,我只是假設我將獲得與您顯示的相同的二維數組:
expected = np.array([[1.99214608], [1.45140389], [0.07068525], [0.69507167], [1.08384057], [0.70685254]])
分箱數據
這有點復雜。我們可以利用numpy.digitize
在您的區間(0、0.9 和 1.5)之間對數據進行分類。但是,此方法不適用于 2D 數組,因此我將首先使用numpy.ravel()
展平數組。
這將返回每個值expected
所屬的 bin 標識列表。但是,bin 標識從 1 開始,我們希望將這些值用作數組的索引,因此我還將同時從結果中減去 1。
bins = np.array([0, 0.9, 1.5]) dig = np.digitize(expected.ravel(), bins) - 1
最后的步驟
我將創建一個與 bin 類別相對應的值數組。然后我們可以使用相應的替換值numpy.take
來替換 的值。dig
replacements = np.array([0, 1, 2]) actual = np.take(replacements, dig)
最后:),我們可以使用numpy.repeat
usingactual
以正確的比例獲取行pop
來構建輸出。
最終代碼
import numpy as np
population_size = 6
chromosome_length = 10
pop = np.random.randint(0, 2, (population_size, chromosome_length))
# But I'm going to deliberately overwrite the above to solve your particular case
pop = np.array([[0., 1., 0., 1., 1., 1., 0., 0., 1., 1.],
[0., 0., 1., 0., 1., 0., 1., 1., 0., 0.],
[0., 0., 1., 0., 0., 1., 1., 0., 0., 1.],
[0., 0., 0., 0., 1., 0., 1., 1., 1., 0.],
[0., 0., 0., 0., 1., 0., 1., 1., 0., 1.],
[0., 0., 0., 0., 0., 0., 0., 1., 0., 0.]])
# Hard-coded :/
expected = np.array([[1.99214608],
[1.45140389],
[0.07068525],
[0.69507167],
[1.08384057],
[0.70685254]])
bins = np.array([0, 0.9, 1.5])
dig = np.digitize(expected.ravel(), bins) - 1
replacements = np.array([0, 1, 2])
actual = np.take(replacements, dig)
out = np.repeat(pop, actual, axis=0)
print(out)
給出:
[[0. 1. 0. 1. 1. 1. 0. 0. 1. 1.]
[0. 1. 0. 1. 1. 1. 0. 0. 1. 1.]
[0. 0. 1. 0. 1. 0. 1. 1. 0. 0.]
[0. 0. 0. 0. 1. 0. 1. 1. 0. 1.]]
添加回答
舉報