简体   繁体   English

如何在 1 的任一侧用 1 填充长度为 n 的一个热数组

[英]How to pad a one hot array of length n with a 1 on either side of the 1

For example I have the following array: [0, 0, 0, 1, 0, 0, 0] what I want is [0, 0, 1, 1, 1, 0, 0]例如我有以下数组: [0, 0, 0, 1, 0, 0, 0]我想要的是[0, 0, 1, 1, 1, 0, 0]

If the 1 is at the at the end, for example [1, 0, 0, 0] it should add only on one side [1, 1, 0, 0]如果 1 在末尾,例如[1, 0, 0, 0]它应该只在一侧添加[1, 1, 0, 0]

How do I add a 1 on either side while keeping the array the same length?如何在保持数组长度相同的同时在任一侧添加 1? I have looked at the numpy pad function, but that didn't seem like the right approach.我看过 numpy 焊盘 function,但这似乎不是正确的方法。

You can usenp.pad to create two shifted copies of the array: one shifted 1 time toward the left (eg 0 1 0 -> 1 0 0 ) and one shifted 1 time toward the right (eg 0 1 0 -> 0 0 1 ).您可以使用np.pad创建数组的两个移位副本:一个向左移动 1 次(例如0 1 0 -> 1 0 0 ),一个向右移动 1 次(例如0 1 0 -> 0 0 1 )。

Then you can add all three arrays together:然后您可以将所有三个 arrays 加在一起:

  0 1 0
  1 0 0
+ 0 0 1
-------
  1 1 1

Code:代码:

output = a + np.pad(a, (1,0))[:-1] + np.pad(a, (0,1))[1:]
# (1, 0) says to pad 1 time at the start of the array and 0 times at the end
# (0, 1) says to pad 0 times at the start of the array and 1 time at the end

Output: Output:

# Original array
>>> a = np.array([1, 0, 0, 0, 1, 0, 0, 0])
>>> a
array([1, 0, 0, 0, 1, 0, 0, 0])

# New array
>>> output = a + np.pad(a, (1,0))[:-1] + np.pad(a, (0,1))[1:]
>>> output
array([1, 1, 0, 1, 1, 1, 0, 0])

One way using numpy.convolve with mode == "same" :使用numpy.convolve with mode == "same"的一种方法:

np.convolve([0, 0, 0, 1, 0, 0, 0], [1,1,1], "same")

Output: Output:

array([0, 0, 1, 1, 1, 0, 0])

With other examples:与其他示例:

np.convolve([1,0,0,0], [1,1,1], "same")
# array([1, 1, 0, 0])
np.convolve([0,0,0,1], [1,1,1], "same")
# array([0, 0, 1, 1])
np.convolve([1,0,0,0,1,0,0,0], [1,1,1], "same")
# array([1, 1, 0, 1, 1, 1, 0, 0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 numpy 阵列转换为一种热编码? - How to convert a numpy array to one hot encoding? 如果长度小于,则用“0”填充数组 - Pad out array with “0” if length less than 带有噪声的高斯过程模型:ValueError:块必须是标量或长度为n_samples的数组 - Gaussian Process Model with Noise: ValueError: nugget must be either a scalar or array of length n_samples 如何将python字典转换为2D数组并在长度不匹配的地方填充? - How to convert a python dictionary to an 2D array and pad wherever there is mismatch in length? 如何填充和标准化可变长度数组 - How to pad and normalize variable length arrays 如何在文本的左侧和右侧用空格填充字符数组 - How to pad char array with empty spaces on left and right hand side of the text 如何在C#中填充数组? - How to pad an array in C#? 确保字符串数组中的每个元素只有一个空格C# - Making sure each element in string array has only one space either side C# 使用值填充javascript数组,直到length可以被3整除 - pad javascript array with value until length is evenly divisible by three 递归函数将任意函数应用于任意长度的 N 个数组,形成一个锯齿状的 N 维多维数组 - Recursive function to apply any function to N arrays of any length to form one jagged multidimensional array of N dimensions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM