Resize an array with a specific value

I have an array phase

 [ (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)]

      

and I would like to resize using the resize method

phase.resize(MAXLINE)

      

and i get this result

[ (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)]

      

I would like to know if it is possible to set a specific value (Nan or -99999) instead of the default 0.0

+3


source to share


3 answers


If you are not attached to np.resize()

, you can do it like this:



import numpy as np


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

maxline = 20


# If you want fullfil extra dimension with NaN 
arr = [(np.NAN,)*len(old_array[0])]*(maxline - len(old_array))

# If you want fullfil extra dimension with anything else
# arr = np.array([(ANYTHING_YOU_WANT,)*len(old_array[0])]*(maxline - old_array.size))

new_ = old_array + arr

print numpy.array(new_)

>> 

[(3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0), (3
.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0), (3.059
8295600822953, 0.376861295611, 0.314535588286, -0.041169303628, 0.0), (nan, nan,
 nan, nan, nan), (nan, nan, nan, nan, nan), (nan, nan, nan, nan, nan), (nan, nan
, nan, nan, nan), (nan, nan, nan, nan, nan), (nan, nan, nan, nan, nan), (nan, na
n, nan, nan, nan), (nan, nan, nan, nan, nan), (nan, nan, nan, nan, nan), (nan, n
an, nan, nan, nan), (nan, nan, nan, nan, nan), (nan, nan, nan, nan, nan), (nan,
nan, nan, nan, nan), (nan, nan, nan, nan, nan), (nan, nan, nan, nan, nan), (nan,
 nan, nan, nan, nan), (nan, nan, nan, nan, nan)]

      

+1


source


Since Python arrays cannot resize arrays, you can use numpy or write your own functions as shown below:



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

      

+1


source


Assuming you want your array to have a shape (MAXLINE,5)

, and that your array is a two-dimensional array, not a list of tuples (it seems like the format in your question seems to be), this will work:

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)

      

gives:

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

      

Explanation:

np.ones()

takes a shape parameter, so I add enough rows to make the final shape (MAXLINE,5)

, where 5 is the number of columns a

(i.e .:) a.shape[1]

.

The np.append()

parameter axis=0

tells numpy to add lines. If you don't have that, it flattens the arrays.

You can of course replace np.NAN

with whatever value you prefer.

+1


source







All Articles