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 with the method resize

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's possible to set a specific value (Nan or -99999) instead of the default value 0.0

3

Best Answer


Making the assumption that you want your array to have shape (MAXLINE,5) and that your array is a 2-dimensional array and not a list of tuples (as the format in your question seems to suggest), this would work:

import numpy as npMAXLINE = 4a=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)

produces:

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'm adding enough rows to make the final shape (MAXLINE,5), where 5 is the number of columns of a (ie: a.shape[1]).

In np.append(), the axis=0 parameter tells numpy to add rows. If you don't have this, it flattens the arrays.

Of course, you can replace np.NAN with any value you prefer.

Because Python arrays don't might resize arrays you can use numpy or write own functions, similar 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 elementif new_size > len(array):new_size = new_size - 1while 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 ita = [ (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 aa = resize(a, 2)print aa = 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)]

If you are not attached to np.resize() you can do it this way:

import numpy as npold_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 + arrprint numpy.array(new_)>> [(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), (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, 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,nan, nan, nan, nan), (nan, nan, nan, nan, nan)]