第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

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多少個(gè)連續(xù)的。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.]])我可以使用一個(gè)簡(jiǎn)單的 for 循環(huán),如下所示:for i in range(c.shape[0]):    for k in range(a[i], a[i]+b[i]):        c[i,k]=1但對(duì)于大型數(shù)組來(lái)說(shuō)會(huì)很慢,有沒(méi)有更快的 numpy 索引來(lái)做到這一點(diǎn)?謝謝。
查看完整描述

3 回答

?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以將其轉(zhuǎn)化為一維問(wèn)題


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和轉(zhuǎn)換b為


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

它們是1展平后 s的索引c。通過(guò)這樣做,您只需要b在函數(shù)中進(jìn)行迭代即可convert_inds。


查看完整回答
反對(duì) 回復(fù) 2023-12-29
?
LEATH

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊

我實(shí)現(xiàn)了下一個(gè)解決方案,沒(méi)有任何 Python 循環(huán),只有純 NumPy 代碼。也許它不像 python-loop 解決方案那么簡(jiǎn)單,但肯定會(huì)快得多,特別是對(duì)于大數(shù)據(jù)。

在線嘗試一下!

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.]]


查看完整回答
反對(duì) 回復(fù) 2023-12-29
?
慕俠2389804

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊

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


>>> np.repeat(a, b)

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

除了每組索引應(yīng)該遞增之外。可以使用以下函數(shù)完成此修復(fù):


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.]])


查看完整回答
反對(duì) 回復(fù) 2023-12-29
  • 3 回答
  • 0 關(guān)注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)