调整具有特定值的数组的大小

时间:2015-05-28 10:23:00

标签: python numpy

我有一个阵列阶段

 [ (3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0)
 (3.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0)
 (3.0598295600822953, 0.376861295611, 0.314535588286, -0.041169303628, 0.0)]

我希望使用resize方法调整大小

phase.resize(MAXLINE)

我得到了这个结果

[ (3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0)
 (3.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0)
 (3.0598295600822953, 0.376861295611, 0.314535588286, -0.041169303628, 0.0)
 (0.0, 0.0, 0.0, 0.0, 0.0) (0.0, 0.0, 0.0, 0.0, 0.0)
 (0.0, 0.0, 0.0, 0.0, 0.0)]

我想知道是否可以设置特定值(Nan或-99999)而不是默认值0.0

3 个答案:

答案 0 :(得分:1)

如果您没有加入file((insert in line 6) ${DOC_DIR}/log.txt "<p>Inserted text.</p>") <html> <head> </head> <body> <!-- Insert text here --> <p>This text is normal.</p> </body> </html> ,可以这样做:

np.resize()

答案 1 :(得分:1)

因为Python数组不能调整数组大小,所以可以使用numpy或自己编写函数,类似如下:

def resize(array, new_size, new_value=0):
    """Resize to biggest or lesser size."""
    element_size = len(array[0]) #Quantity of new elements equals to quantity of first element
    if new_size > len(array):
        new_size = new_size - 1
        while len(array)<=new_size:
            n = tuple(new_value for i in range(element_size))
            array.append(n)
    else:
        array = array[:new_size]
    return array
#Test it
a =  [ (3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0),
(3.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0),
(3.0598295600822953, 0.376861295611, 0.314535588286, -0.041169303628, 0.0)]
a = resize(a, 5)
print a
a = resize(a, 2)
print a
a = resize(a, 3, 28)
print a 

#Output:
#New size 5, default value 0
#[(3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0), (3.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0), (3.0598295600822953, 0.376861295611, 0.314535588286, -0.041169303628, 0.0), (0, 0, 0, 0, 0), (0, 0, 0, 0, 0)]
#new size 2
#[(3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0), (3.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0)]
#New size 4, default value 28
#[(3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0), (3.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0), (28, 28, 28, 28, 28)]

答案 2 :(得分:1)

假设您希望数组具有形状(MAXLINE,5),并且您的数组是二维数组而不是元组列表(正如您的问题中的格式似乎暗示的那样),这将起作用:

import numpy as np
MAXLINE = 4
a=np.array([ [3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0],
 [3.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0],
 [3.0598295600822953, 0.376861295611, 0.314535588286, -0.041169303628, 0.0]])
np.append(a,np.ones((MAXLINE-a.shape[0],a.shape[1]))*np.NAN,axis=0)

产生

array([[ 3.05354009,  0.3713459 ,  0.31295379, -0.01252314,  0.        ],
       [ 3.05668483,  0.37397185,  0.31368239, -0.02645439,  0.        ],
       [ 3.05982956,  0.3768613 ,  0.31453559, -0.0411693 ,  0.        ],
       [        nan,         nan,         nan,         nan,         nan]])

说明:

np.ones()采用形状参数,因此我添加了足够的行来制作最终形状(MAXLINE,5),其中5是a的列数(即:{{1} }})。

a.shape[1]中,np.append()参数告诉numpy添加行。如果你没有这个,它会使阵列变平。

当然,您可以将axis=0替换为您喜欢的任何值。