Python:拉普拉斯正则逻辑回归

'''
auther:Deniu He
date:2020-09-30
'''
from copy import deepcopy
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from scipy.spatial.distance import pdist,squareform
import cvxpy as cvx


class LRLR():
    def __init__(self, X_labeled=None, y_labeled=None ,adjMatrix=None, X_pool=None):
        self.X = None
        self.y = None
        self.d = None
        ###-------------------------------
        self.X_pool = None
        self.adjMatrix = None
        self.N = None
        self.lapMatrix = None
        ###------------------------------
        self.lambd1 = None
        self.lambd2 = None
        self.w = None
        self.P = None
        ###------------------------------
        self.loglikehood = None
        self.L2regu = None
        self.Lapregu = None
        self.obj = None
        self.prob = None

    def fit(self,X_labeled,y_labeled,adjMatrix,X_pool):
        self.X = X_labeled
        self.y = np.vstack(y_labeled)
        self.d = X_labeled.shape[1]
        ###-------------------------------
        self.X_pool = X_pool
        self.adjMatrix = adjMatrix
        self.N = adjMatrix.shape[0]
        self.lapMatrix = np.diag(np.sum(adjMatrix,0)) - adjMatrix
        ###------------------------------
        self.lambd1 = 0.1
        self.lambd2 = 0.1
        self.w = cvx.Variable((self.d, 1))
        self.P = self.X_pool @ self.w
        ###------------------------------
        self.loglikehood = cvx.sum(cvx.multiply(self.y, self.X @ self.w) - cvx.logistic(self.X @ self.w))
        self.L2regu = self.lambd1 * cvx.pnorm(self.w, p=2)**2
        self.Lapregu = self.lambd2 * cvx.quad_form(self.P,self.lapMatrix)
        self.obj = cvx.Maximize(self.loglikehood - self.Lapregu - self.L2regu)
        self.prob = cvx.Problem(self.obj).solve()

    def pred_prob(self,X_test):
        prob = 1 / (1 + cvx.exp(-(X_test @ self.w)).value)
        return prob

    def pred(self, X_test):
        result = []
        for ele in 1 / (1 + cvx.exp(-(X_test @ self.w)).value):
            if ele >= 0.5:
                result.append(1)
            else:
                result.append(0)
        return np.array(result)

if __name__ == '__main__':
    X, y = datasets.make_blobs(n_samples=500, n_features=2, centers=2, cluster_std=[3, 3], random_state=123)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.992,random_state=1)
    print("测试数据个数=",len(y_test))

    plt.scatter(X[:,0],X[:,1],c=y)
    plt.scatter(X_train[:,0],X_train[:,1],c='r',marker="*",s=200,label="Labeled instance")
    plt.legend()
    plt.show()

    distMatrix = squareform(pdist(X,metric='euclidean'))

    def adjacency_matrix(X):
        n = X.shape[0]
        nn_matrix = np.zeros((n,5))
        for i in range(n):
            ord_idx = np.argsort(distMatrix[i])
            neibor = []
            for j in range(5+1):
                if i != ord_idx[j]:
                    neibor.append(ord_idx[j])
            neibor = np.array(neibor)
            nn_matrix[i] = neibor
        adj_matrix = np.zeros((n,n))
        for i in range(n-1):
            for j in range(i,n):
                if i in nn_matrix[j] or j in nn_matrix[i]:
                    adj_matrix[i,j] = 1
                    adj_matrix[j,i] = 1
        return adj_matrix

    adjMatrix = adjacency_matrix(X=X)

    model = LRLR()
    model.fit(X_labeled=X_train,y_labeled=y_train,adjMatrix=adjMatrix,X_pool=X)
    y_pred = model.pred(X_test=X_test)
    acc = accuracy_score(y_true=y_test,y_pred=y_pred)
    print("精度:",acc)
    model2 = LogisticRegression()
    model2.fit(X=X_train,y=y_train)
    y_pred2 = model2.predict(X=X_test)
    acc2 = accuracy_score(y_true=y_test,y_pred=y_pred2)
    print("调包精度:",acc2)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DeniuHe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值