reshape
Reshape an array in NumPy
a = np.arange(18).reshape(9,2) b = a.reshape(3,3,2).swapaxes(0,2) # a: array([[ 0, 1], [ 2, 3], [ 4, 5], [ 6, 7], [ 8, 9], [10, 11], [12, 13], [14, 15], [16, 17]]) # b: array([[[ 0, 6, 12], [ 2, 8, 14], [ 4, 10, 16]], [[ 1, 7, 13], [ 3, 9, 15], [ 5, … Read more
What does -1 mean in pytorch view?
Yes, it does behave like -1 in numpy.reshape(), i.e. the actual value for this dimension will be inferred so that the number of elements in the view matches the original number of elements. For instance: import torch x = torch.arange(6) print(x.view(3, -1)) # inferred size will be 2 as 6 / 3 = 2 # … Read more
Melt the Upper Triangular Matrix of a Pandas Dataframe
First I convert lower values of df to NaN by where and numpy.triu and then stack, reset_index and set column names: import numpy as np print df a b c a 1.0 0.5 0.3 b 0.5 1.0 0.4 c 0.3 0.4 1.0 print np.triu(np.ones(df.shape)).astype(np.bool) [[ True True True] [False True True] [False False True]] df … Read more