Python 根据索引追加缺少的值
如果我有张量Python 根据索引追加缺少的值,python,numpy,pytorch,Python,Numpy,Pytorch,如果我有张量 values = torch.tensor([5, 3, 2, 8]) 和相应的索引到值 index = torch.tensor([0, 2, 4, 5]) 假设我想在缺少的索引(1和3)中插入一个固定值(100),这样 values = torch.tensor([5, 100, 3, 100, 2, 8]) 在PyTorch(或numpy)中有没有矢量化的方法呢?您可以先用100填充,然后用原始值填充 在皮托克 import torch result = torch.
values = torch.tensor([5, 3, 2, 8])
和相应的索引
到值
index = torch.tensor([0, 2, 4, 5])
假设我想在缺少的索引(1和3)
中插入一个固定值(100)
,这样
values = torch.tensor([5, 100, 3, 100, 2, 8])
在PyTorch(或numpy)中有没有矢量化的方法呢?您可以先用100填充,然后用原始值填充 在皮托克
import torch
result = torch.empty(6, dtype = torch.int32).fill_(100)
values = torch.tensor([5, 3, 2, 8], dtype = torch.int32)
index = torch.tensor([0, 2, 4, 5])
result[index] = values
print(result)
在努比
import numpy as np
result = np.full((6,), 100)
index = np.array([0, 2, 4, 5])
values = np.array([5, 3, 2, 8])
result[index] = values
print(result)