In numpy
the dimensions of the resulting array vary at run time.
There is often confusion between a 1d array and a 2d array with 1 column.
In one case I can iterate over the columns, in the other case I cannot.
How do you solve elegantly that problem?
To avoid littering my code with if
statements checking for the dimensionality, I use this function:
def reshape_to_vect(ar):
if len(ar.shape) == 1:
return ar.reshape(ar.shape[0],1)
return ar
However, this feels inelegant and costly. Is there a better solution?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…