I wrote two functions for determining the linear discriminant classifier of a EEG data-set. The data set consists preprocessed EEG data π‘‹βˆˆπ‘…5Γ—62Γ—5322 and stimulus labels π‘Œβˆˆπ‘…2Γ—5322 during a copy-spelling paradigm with a P300 speller. The data matrix X contains 5 selected time windows of EEG activity at 62 electrodes after a visual stimulus was presented on the screen in front of the subject. If the first row of π‘Œ is 1, the stimulus was a target stimulus, if the second row of π‘Œ is 1, the stimulus was a non-target stimulus. The first function returns the weight vector and the bias term. The second function is a graph class to show the result

def lda_fit(X,Y):

# class means

for i,name in enumerate(unique_classes):
    mu[i,:] = X[Y==name,:].mean(axis=0)


# D-by-D inter class covariance matrix (signal)
Sinter = np.dot((muneg-mupos),(muneg-mupos).T)

# D-by-D intra class covariance matrices (noise)
Sintra =np.dot((Xneu-mupos),(Xneu-mupos).T)+np.dot((Xneu-muneg),(Xneu-muneg).T)

# solve eigenproblem
eigvals, eigvecs = sp.linalg.eig(Sinter,Sintra)
w = eigvecs[:,eigvals.argmax()]
# bias term
b = (w.dot(mupos) + w.dot(muneg))/2.
# return the weight vector
return w,b

I get the following error: "ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 155 is different from 310)"

I know it has something to do with the shape of the matrices, but I really stuck.


As you say; it has something to do with the shape of the matrices. In this case, you have matrices in the same of (155,2) - whereas it's expecting them to be of (310, 1).

Try looking into the shape of each matrix as you transform them (tip: insert a print statement wherever a transformation occurs); and make sure everything stays consistent.

Also, can you provide us with more information, such as exactly where this error occurs? It looks to be an issue with the dot product.

| improve this answer | |
  • $\begingroup$ Indeed it is an issue with the dot product.First I tried to calculate the inter class covariance matrix and the intra class covariance matrix with the shape 310,1 and it didn't work. SO I reshaped the matrices to the size of 155,2 and then the dot product worked. Unfortunatly the result was pretty useless for the graph class. So there has to be a another possibility how I can calculate the sinter and sintra. The code for my graph class is this:X_train, X_test, Y_train, Y_test = train_test_split(X,Y) w_lda,b_lda = lda_fit(X_train,Y_train) w_lda=np.repeat(w_lda,2) $\endgroup$ – me4gqp Jun 1 '19 at 8:18
  • $\begingroup$ The output of print(X_test[Y_test<0, :].shape) is (1118, 310). The exact error is: ValueError Traceback (most recent call last) <ipython-input-163-8e53abe25a8e> in <module> 26 pl.legend(('$b_{ncc}$','non-target','target')) 27 pl.ylim([0, 300]) ---> 28 acc = int((sp.sign(X_test @ w_lda - b_lda)==Y_test).mean()*100) 29 pl.title(f"LDA Acc {acc}%"); ValueError: operands could not be broadcast together with shapes (1331,) (2,) $\endgroup$ – me4gqp Jun 1 '19 at 8:21

Your Answer

By clicking β€œPost Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.