I am trying to get the upper diagonal elements of correlation matrix mS. So for that I am using np.triu (I don't want to have the ones on the diagonal, so I use k=1). However, I want to have those elements in a vector. I have already read lots of thins about np.triu_indices, but the code doesn't work, because I get the error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
mS= [[1, .8, .6, .8, .7, .8, .6, .9, .5, .6, .8],
[.8, 1, .8, .5, .6, .7, .7, .8, .5, .8, .7],
[.6, .8, 1, .7, .8, .6, .7, .6, .7, .7, .9],
[.8, .5, .7, 1, .8, .6, .8, .7, .6, .9, .8],
[.7, .6, .8, .8, 1, .5, .8, .9, .9, .8, .6],
[.8, .7, .6, .6, .5, 1, .9, .7, .5, .9, .8],
[.6, .7, .7, .8, .8, .9, 1, .6, .8, .7, .7],
[.9, .8, .6, .7, .9, .7, .6, 1, .8, .6, .9],
[.5, .5, .7, .6, .9, .5, .8, .8, 1, .9, .8],
[.6, .8, .7, .9, .8, .9, .7, .6, .9, 1, .8],
[.8, .7, .9, .8, .6, .8, .7, .9, .8, .8, 1]]
mS= np.array(mS)
mSi= np.triu(mS, k=1).
# Show mSi
mSi = array([[0. , 0.8, 0.6, 0.8, 0.7, 0.8, 0.6, 0.9, 0.5, 0.6, 0.8],
[0. , 0. , 0.8, 0.5, 0.6, 0.7, 0.7, 0.8, 0.5, 0.8, 0.7],
[0. , 0. , 0. , 0.7, 0.8, 0.6, 0.7, 0.6, 0.7, 0.7, 0.9],
[0. , 0. , 0. , 0. , 0.8, 0.6, 0.8, 0.7, 0.6, 0.9, 0.8],
[0. , 0. , 0. , 0. , 0. , 0.5, 0.8, 0.9, 0.9, 0.8, 0.6],
[0. , 0. , 0. , 0. , 0. , 0. , 0.9, 0.7, 0.5, 0.9, 0.8],
[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.6, 0.8, 0.7, 0.7],
[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.8, 0.6, 0.9],
[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.9, 0.8],
[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.8],
[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ]])
vPR= np.triu_indices(mS, -55)
This gives me the error
I would like to have one array (named vPR) where all the triu elements from above are placed into. Hope someone can help!
you could generate the index arrays for the diagonal you want
r, c = mSi.shape
mSi[np.arange(0, r-1), np.arange(1, c)]
Out[28]: array([0.8, 0.8, 0.7, 0.8, 0.5, 0.9, 0.6, 0.8, 0.9, 0.8])