核逻辑回归--kernel logistics regression

基于sklearn实现anova核逻辑回归

import numpy as np
from sklearn.metrics.pairwise import check_pairwise_arrays
from scipy.linalg import cholesky
from sklearn.linear_model import LogisticRegression

def anova_kernel(X, Y=None, gamma=None, p=1):
    X, Y = check_pairwise_arrays(X, Y)
    if gamma is None:
        gamma = 1. / X.shape[1]

    diff = X[:, None, :] - Y[None, :, :]
    diff **= 2
    diff *= -gamma
    np.exp(diff, out=diff)
    K = diff.sum(axis=2)
    K **= p
    return K

# Kernel matrix based on X matrix of all data points
K = anova_kernel(X)
R = cholesky(K, lower=False)

# Define the model
clf = LogisticRegression()

# Here, I assume that you have splitted the data and here, traina re the indices for the training set
clf.fit(R[train], y_train)
preds = clf.predict(R[test])¨

你可能感兴趣的:(machine,learning)