Computing the inverse matrix with pandas

I have a large pandas DataFrame - a square matrix with a header and index, and I'm trying to use the pandas features to calculate the inverse of this matrix without going directly through numpy.

I want to stay within the pandas framework to preserve the headers of my framework. I could use the pd.as_matrix function, but that turns it into an ndarray, and I lose all the information provided by the headers.

Any suggestions?

+4
source share
1 answer

consider the information frame df

np.random.seed([3,1415])
df = pd.DataFrame(np.random.rand(3, 3), list('abc'), list('xyz'))
df

enter image description here

calculate the inverse (with numpy, albeit not crazy)

df_inv = pd.DataFrame(np.linalg.pinv(df.values), df.columns, df.index)
df_inv

enter image description here

Notice I use pinvfor pseudo reverse

then check

df_inv.dot(df)

enter image description here

+6

Source: https://habr.com/ru/post/1662221/


All Articles