I am very new to python and ML, I'm trying to customize a RBF kernel to consider Mahalanobis distance instead of Euclidean distance but I am encountering problems when I try to predict on new instances.

>>>Here is my kernel

def my_kernel(X, sigma):
    distance_matrix = cdist(X,X,'mahalanobis')
    sigma = 0.3
    gamma = (1/(2*np.power(sigma,2)))
    K = -(gamma*distance_matrix)
    return np.exp(K)

>>>And a I have fit SVR model to a training set with 423 samples

sc_X = StandardScaler()
sc_y = StandardScaler()
std_X = sc_X.fit_transform(X)
std_y = sc_y.fit_transform(y)

svr_maha = SVR(kernel=my_kernel)

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=None, test_size=0.1)

svr_maha.fit(X_train, y_train)

>>>>And it does predict the training, however, when I pass a new feature matrix(with the same number of features but with 3120 samples), to generate predictions on locations where I don't have the target value  I get the following error message:

**ValueError: X.shape[1] = 3120 should be equal to 423, the number of samples at training time**

>>>Anyone could help me out? Thanks a lot!!!


Your Answer

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

Browse other questions tagged or ask your own question.