1

Is there a way to use numpy.linalg.eigh() or scipy.linalg.eigh() for solving the generalized eigenvalue problem A⋅x=λB⋅x when A and B do not have matching dimensions? E.g., for when A is a 4x4 matrix and B is a 5x5 matrix. Or is there another Python function that can handle that? If I try to use eigh(A, B) I get the following error for the scipy version:

    valueError: wrong b dimensions (6, 6), should be (4, 4)

and this error in the numpy version:

    'numpy.ndarray' object has no attribute 'upper'

I found this answer Solve Generalized Eigenvalue Problem in Numpy, which is fine for when both matrices are the same dimensions but I'm working on a multiple linear regression problem, trying to predict target values from input features, where A and B are symmetric matrices but of different dimensions to each other. I also tried this solution: https://guihongwan.github.io/2018/11/The-Generalized-Eigenvector-and-Eigenvalue-Problem/, but get an error for the same non-matching dimensions reason.

Background: I'm trying to use a set of input features, X ∈ Rn×d, where n is the number of observations and d is the number of input features, and a corresponding set of output values, Y ∈ Rn×p where p is the number of output dimensions, to map X to Y so as to try to predict new Y_hat output. As a mini example with dummy numbers, say X is a 5 x 4 input matrix of 5 observations and 4 features and Y is a 5 x 2 target matrix of 5 observations and 2 output values:

    X = [[1,2,3,4],
         [5,6,7,8],
         [4,3,2,1],
         [8,7,6,5],
         [9,6,4,2]]

    Y = [[8,7],
         [6,5],
         [4,3],
         [2,1],
         [1,9]]

I calculate the covariance matrix for the input X:

    A = np.cov(X, rowvar=False)    # A = 4x4 matrix

Then I calculate the covariance matrix for the input and output:

    Cxy = np.hstack((X, Y))
    B = np.cov(Cxy, rowvar=False)    # B = 6x6 matrix

I then try to use A and B to calculate the eigenvalues and eigenvectors using eigh() but that's where I get stuck. Can anyone offer a solution please or spot where I've gone wrong?

humbleHacker
  • 437
  • 6
  • 17

0 Answers0