Performing array manipulations
In finance-related research, quite often we need to change the dimensions of a matrix or an array. For example, converting a set of 100 random numbers into a 20 by 5 matrix or vice versa. For this purpose, we could use two NumPy
functions, flatten()
and reshape()
, as follows:
>>>pv=np.array([[100,10,10.2],[34,22,34]]) # 2 by 3 >>>x=pv.flatten() # matrix becomes a vector >>>vp2=np.reshape(x,[3,2]) # 3 by 2 now