Python:调整现有数组的大小并填充零

时间:2022-02-10 21:28:01

I think that my issue should be really simple, yet I can not find any help on the Internet whatsoever. I am very new to Python, so it is possible that I am missing something very obvious.

我认为我的问题应该很简单,但我无法在互联网上找到任何帮助。我是Python的新手,所以我可能会遗漏一些非常明显的东西。

I have an array, S, like this [x x x] (one-dimensional). I now create a diagonal matrix, sigma, with np.diag(S) - so far, so good. Now, I want to resize this new diagonal array so that I can multiply it by another array that I have.

我有一个数组,S,像这样[x x x](一维)。我现在用np.diag(S)创建一个对角矩阵sigma - 到目前为止,非常好。现在,我想调整这个新的对角线数组的大小,以便我可以将它乘以我拥有的另一个数组。

import numpy as np
...
shape = np.shape((6, 6)) #This will be some pre-determined size
sigma = np.diag(S) #diagonalise the matrix - this works
my_sigma = sigma.resize(shape) #Resize the matrix and fill with zeros - returns "None" - why?

However, when I print the contents of my_sigma, I get "None". Can someone please point me in the right direction, because I can not imagine that this should be so complicated.

但是,当我打印my_sigma的内容时,我得到“无”。有人可以指出我正确的方向,因为我无法想象这应该是如此复杂。

Thanks in advance for any help!

在此先感谢您的帮助!

Casper

卡斯帕

Graphical:

图形:

I have this:

我有这个:

[x x x]

I want this:

我要这个:

[x 0 0]
[0 x 0]
[0 0 x]
[0 0 0]
[0 0 0]
[0 0 0] - or some similar size, but the diagonal elements are important.

5 个解决方案

#1


18  

sigma.resize() returns None because it operates in-place. np.resize(sigma, shape), on the other hand, returns the result but instead of padding with zeros, it pads with repeats of the array.

sigma.resize()返回None,因为它就地运行。另一方面,np.resize(sigma,shape)返回结果,但不是用零填充,而是填充数组的重复。

Also, the shape() function returns the shape of the input. If you just want to predefine a shape, just use a tuple.

此外,shape()函数返回输入的形状。如果您只想预定义形状,只需使用元组即可。

import numpy as np
...
shape = (6, 6) #This will be some pre-determined size
sigma = np.diag(S) #diagonalise the matrix - this works
sigma.resize(shape) #Resize the matrix and fill with zeros

However, this will first flatten out your original array, and then reconstruct it into the given shape, destroying the original ordering. If you just want to "pad" with zeros, instead of using resize() you can just directly index into a generated zero-matrix.

但是,这将首先展平原始数组,然后将其重建为给定的形状,从而破坏原始排序。如果你只想用零“填充”,而不是使用resize(),你可以直接索引到生成的零矩阵。

# This assumes that you have a 2-dimensional array
zeros = np.zeros(shape, dtype=np.int32)
zeros[:sigma.shape[0], :sigma.shape[1]] = sigma

#2


51  

There is a new numpy function in version 1.7.0 numpy.pad that can do this in one-line. Like the other answers, you can construct the diagonal matrix with np.diag before the padding. The tuple ((0,N),(0,0)) used in this answer indicates the "side" of the matrix which to pad.

版本1.7.0 numpy.pad中有一个新的numpy函数可以在一行中执行此操作。与其他答案一样,您可以在填充之前使用np.diag构造对角矩阵。在这个答案中使用的元组((0,N),(0,0))表示要填充的矩阵的“边”。

import numpy as np

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

N = A.size
B = np.pad(np.diag(A), ((0,N),(0,0)), mode='constant')

B is now equal to:

B现在等于:

[[1 0 0]
 [0 2 0]
 [0 0 3]
 [0 0 0]
 [0 0 0]
 [0 0 0]]

#3


4  

I see the edit... you do have to create the zeros first and then move some numbers into it. np.diag_indices_from might be useful for you

我看到编辑...你必须先创建零,然后将一些数字移入其中。 np.diag_indices_from可能对您有用

bigger_sigma = np.zeros(shape, dtype=sigma.dtype)
diag_ij = np.diag_indices_from(sigma)
bigger_sigma[diag_ij] = sigma[diag_ij] 

#4


0  

Another pure python solution is

另一个纯python解决方案是

a = [1, 2, 3]
b = []
for i in range(6):
    b.append((([0] * i) + a[i:i+1] + ([0] * (len(a) - 1 - i)))[:len(a)])

b is now

b现在

[[1, 0, 0], [0, 2, 0], [0, 0, 3], [0, 0, 0], [0, 0, 0], [0, 0, 0]]

it's a hideous solution, I'll admit that. However, it illustrates some functions of the list type that can be used.

这是一个可怕的解决方案,我承认这一点。但是,它说明了可以使用的列表类型的一些功能。

#5


0  

This solution works with resize function

此解决方案适用于调整大小功能

Take a sample array

拿一个样本数组

S= np.ones((3))
print (S)
# [ 1.  1.  1.]
d= np.diag(S) 
print(d)
"""
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

"""

This dosent work, it just add a repeating values

这种剂量工作,它只是添加了重复值

np.resize(d,(6,3))
"""
adds a repeating value
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.],
       [ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
"""

This does work

这确实有效

d.resize((6,3),refcheck=False)
print(d)
"""
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]
"""

#1


18  

sigma.resize() returns None because it operates in-place. np.resize(sigma, shape), on the other hand, returns the result but instead of padding with zeros, it pads with repeats of the array.

sigma.resize()返回None,因为它就地运行。另一方面,np.resize(sigma,shape)返回结果,但不是用零填充,而是填充数组的重复。

Also, the shape() function returns the shape of the input. If you just want to predefine a shape, just use a tuple.

此外,shape()函数返回输入的形状。如果您只想预定义形状,只需使用元组即可。

import numpy as np
...
shape = (6, 6) #This will be some pre-determined size
sigma = np.diag(S) #diagonalise the matrix - this works
sigma.resize(shape) #Resize the matrix and fill with zeros

However, this will first flatten out your original array, and then reconstruct it into the given shape, destroying the original ordering. If you just want to "pad" with zeros, instead of using resize() you can just directly index into a generated zero-matrix.

但是,这将首先展平原始数组,然后将其重建为给定的形状,从而破坏原始排序。如果你只想用零“填充”,而不是使用resize(),你可以直接索引到生成的零矩阵。

# This assumes that you have a 2-dimensional array
zeros = np.zeros(shape, dtype=np.int32)
zeros[:sigma.shape[0], :sigma.shape[1]] = sigma

#2


51  

There is a new numpy function in version 1.7.0 numpy.pad that can do this in one-line. Like the other answers, you can construct the diagonal matrix with np.diag before the padding. The tuple ((0,N),(0,0)) used in this answer indicates the "side" of the matrix which to pad.

版本1.7.0 numpy.pad中有一个新的numpy函数可以在一行中执行此操作。与其他答案一样,您可以在填充之前使用np.diag构造对角矩阵。在这个答案中使用的元组((0,N),(0,0))表示要填充的矩阵的“边”。

import numpy as np

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

N = A.size
B = np.pad(np.diag(A), ((0,N),(0,0)), mode='constant')

B is now equal to:

B现在等于:

[[1 0 0]
 [0 2 0]
 [0 0 3]
 [0 0 0]
 [0 0 0]
 [0 0 0]]

#3


4  

I see the edit... you do have to create the zeros first and then move some numbers into it. np.diag_indices_from might be useful for you

我看到编辑...你必须先创建零,然后将一些数字移入其中。 np.diag_indices_from可能对您有用

bigger_sigma = np.zeros(shape, dtype=sigma.dtype)
diag_ij = np.diag_indices_from(sigma)
bigger_sigma[diag_ij] = sigma[diag_ij] 

#4


0  

Another pure python solution is

另一个纯python解决方案是

a = [1, 2, 3]
b = []
for i in range(6):
    b.append((([0] * i) + a[i:i+1] + ([0] * (len(a) - 1 - i)))[:len(a)])

b is now

b现在

[[1, 0, 0], [0, 2, 0], [0, 0, 3], [0, 0, 0], [0, 0, 0], [0, 0, 0]]

it's a hideous solution, I'll admit that. However, it illustrates some functions of the list type that can be used.

这是一个可怕的解决方案,我承认这一点。但是,它说明了可以使用的列表类型的一些功能。

#5


0  

This solution works with resize function

此解决方案适用于调整大小功能

Take a sample array

拿一个样本数组

S= np.ones((3))
print (S)
# [ 1.  1.  1.]
d= np.diag(S) 
print(d)
"""
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

"""

This dosent work, it just add a repeating values

这种剂量工作,它只是添加了重复值

np.resize(d,(6,3))
"""
adds a repeating value
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.],
       [ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
"""

This does work

这确实有效

d.resize((6,3),refcheck=False)
print(d)
"""
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]
"""