亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

給定行索引和起始列索引,如何將值分配給 numpy 數組?

給定行索引和起始列索引,如何將值分配給 numpy 數組?

翻過高山走不出你 2023-12-29 16:44:13
a = np.array([2,3,1,4])b = np.array([2,3,7,1])c = np.zeros((4, 10))1我想為 中的某些元素賦值c。a并b定義這些元素的位置。是每行中a值的起始列索引。1并表示該行有b多少個連續的。1我期望的輸出是:array([[ 0.,  0.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],       [ 0.,  0.,  0.,  1.,  1.,  1.,  0.,  0.,  0.,  0.],       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.,  0.],       [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.]])我可以使用一個簡單的 for 循環,如下所示:for i in range(c.shape[0]):    for k in range(a[i], a[i]+b[i]):        c[i,k]=1但對于大型數組來說會很慢,有沒有更快的 numpy 索引來做到這一點?謝謝。
查看完整描述

3 回答

?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

您可以將其轉化為一維問題


def convert_inds(a,b,array_shape):

    

    nrows,ncols = array_shape

    to_take = np.zeros(sum(b))

    count = 0

    for ind,item in enumerate(b):

        start_ind = ind*ncols+a[ind]

        to_take[count:count+item] = np.arange(start_ind,start_ind+item)

        count += item

        

    return to_take.astype(np.int)


to_take = convert_inds(a,b,c.shape)


c.ravel()[to_take] = 1

在上面的代碼中,convert_inds將a和轉換b為


array([ 2,  3, 13, 14, 15, 21, 22, 23, 24, 25, 26, 27, 34])

它們是1展平后 s的索引c。通過這樣做,您只需要b在函數中進行迭代即可convert_inds。


查看完整回答
反對 回復 2023-12-29
?
LEATH

TA貢獻1936條經驗 獲得超7個贊

我實現了下一個解決方案,沒有任何 Python 循環,只有純 NumPy 代碼。也許它不像 python-loop 解決方案那么簡單,但肯定會快得多,特別是對于大數據。

在線嘗試一下!

import numpy as np


def set_val_2d(a, val, starts, lens):

    begs = starts + np.arange(a.shape[0]) * a.shape[1]

    ends = begs + lens

    clens = lens.cumsum()

    ix = np.ones((clens[-1],), dtype = np.int64)

    ix[0] = begs[0]

    ix[clens[:-1]] = begs[1:] - ends[:-1] + 1

    ix = ix.cumsum()

    a.ravel()[ix] = val


a = np.array([2,3,1,4])

b = np.array([2,3,7,1])

c = np.zeros((4, 10))


set_val_2d(c, 1, a, b)

print(c)

輸出:


[[0. 0. 1. 1. 0. 0. 0. 0. 0. 0.]

 [0. 0. 0. 1. 1. 1. 0. 0. 0. 0.]

 [0. 1. 1. 1. 1. 1. 1. 1. 0. 0.]

 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]]


查看完整回答
反對 回復 2023-12-29
?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

如果您選擇基于索引的奇特方法,最困難的部分是查找軸 1 的索引。這非常類似于:


>>> np.repeat(a, b)

array([2, 2, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 4])

除了每組索引應該遞增之外??梢允褂靡韵潞瘮低瓿纱诵迯停?/p>


def accumulative_count(counts, initial):

    counter = np.ones(np.sum(counts), dtype=int)

    marker_idx = np.r_[0, np.cumsum(counts)[:-1]]

    subtract_vals = np.r_[1, counts[:-1]]

    initial_vals = np.r_[initial[0], np.diff(initial)]

    counter[marker_idx] = counter[marker_idx] - subtract_vals + initial_vals

    return np.cumsum(counter)


>>> accumulative_count(counts, initial)

array([2, 3, 3, 4, 5, 1, 2, 3, 4, 5, 6, 7, 4], dtype=int32)

畢竟,你有能力完成它:


c[np.repeat(np.arange(len(c)), b), accumulative_count(b, a)] = 1

c:


array([[0., 0., 1., 1., 0., 0., 0., 0., 0., 0.],

       [0., 0., 0., 1., 1., 1., 0., 0., 0., 0.],

       [0., 1., 1., 1., 1., 1., 1., 1., 0., 0.],

       [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])


查看完整回答
反對 回復 2023-12-29
  • 3 回答
  • 0 關注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號