我已經在 Pandas 中實作了某種物件穩定性計算器。但是表演時間是可怕的。有人能幫助我嗎。
def calculate_stability(ind, df, sidx, max_k):
indexes = sidx[:, ind]
indexes = np.delete(indexes, np.where(indexes == ind))
d = 0
last_crtit_obj_count = 0
for j in range(max_k):
if df.at[ind, "Class"] == df.at[indexes[j], "Class"]:
d = d 1
if d / (j 1) > 1/2:
last_crtit_obj_count = (j 1)
print(f'\t Object {ind} = {last_crtit_obj_count / max_k}')
return last_crtit_obj_count / max_k
df.iloc 非常慢。這就是我更改為 df.at 的原因。
代碼在這里
需要矢量化版本的回圈。
uj5u.com熱心網友回復:
這是沒有回圈的版本:
def calculate_stability(ind, df, sidx, max_k):
indexes = sidx[:, ind]
indexes = indexes[indexes != ind][:max_k]
# `d` contains all values from the first condition from the original loop:
d = (df["Class"][ind] == df["Class"][indexes]).cumsum()
# `j` contains all values from the original `range` 1:
j = np.arange(1, len(d) 1)
# select `last_crtit_obj_count` values:
crtit_objs = j[(d / j > 1 / 2)]
# calculate `last_crtit_obj_count / max_k`
result = crtit_objs[-1] / max_k if len(crtit_objs) else 0
print(f"\t Object {ind} = {result}")
return result
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/342908.html
上一篇:使用串列值創建二進制矩陣
下一篇:在字符級別對單詞串列進行編碼
