Logistic回归模型中的内核LogisticRegression scikit-learn sklearn

时间:2018-11-07 22:54:19

标签: machine-learning scikit-learn kernel svm logistic-regression

如何使用sklearn库在逻辑回归模型中使用内核?

logreg = LogisticRegression()

logreg.fit(X_train, y_train)

y_pred = logreg.predict(X_test)
print(y_pred)

print(confusion_matrix(y_test,y_pred))
print(classification_report(y_test,y_pred))
predicted= logreg.predict(predict)
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))

1 个答案:

答案 0 :(得分:2)

非常好的问题,但是uniq当前不支持内核逻辑回归和ANOVA内核。

您可以实现它。

ANOVA内核的示例1:

scikit-learn

Nyström的示例2:

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 split the data and here, train are the indices for the training set
clf.fit(R[train], y_train)
preds = clf.predict(R[test])¨