Python:OIICkNNI

本文将深入探讨Python中K近邻(K-Nearest Neighbors, KNN)算法的实现与应用,包括数据预处理、模型训练和预测,以及在实际案例中的效果分析。" 50770256,5616460,寻找第一个坏版本:LeetCode 004 First Bad Version,"['算法', '编程题', 'LeetCode', '二分搜索']
摘要由CSDN通过智能技术生成
"""
注意:注意:一定要使用训练集索引
"""
import os
import xlwt
import xlrd
import numpy as np
import pandas as pd
from pathlib import Path
from copy import deepcopy
from sklearn.preprocessing import StandardScaler
from time import time
from sklearn.impute import KNNImputer
from sklearn.metrics.pairwise import pairwise_distances
from numpy.linalg import inv
from sklearn.metrics import accuracy_score, mean_absolute_error, f1_score, mutual_info_score
from sklearn.neighbors import NearestNeighbors
from mord import LogisticAT
np.seterr(divide='ignore',invalid='ignore')


def _svd_threshold(svd_obj, lambdadL):
    svd_obj = np.asarray(svd_obj)
    eig_obj = svd_obj.T.dot(svd_obj)
    eig_obj2 = eig_obj
    eig_tag = 0
    eig_obj_timer = 1e20
    while (eig_tag == 0) and (eig_obj_timer >= 1e8):
        try:
            D, V = np.linalg.eig(eig_obj2)
            sorted_args = np.argsort(D)
            V = V[:, sorted_args]
            D = D[sorted_args]
            eig_tag = 1
        except:
            eig_tag = 0
            eig_obj2 = round(eig_obj * eig_obj_timer) / eig_obj_timer
            eig_obj_timer = eig_obj_timer / 10

    # D = np.diag(D)
    D[D < 0] = 0
    D = np.sqrt(D)
    D2 = copy.copy(D)
    D2[D2 != 0] = 1.0 / D2[D2 != 0]
    D = np.diag(np.fmax(np.zeros(D.shape), D - lambdadL))
    Zk = svd_obj.dot((V.dot(np.diag(D2))).dot(D.dot(V.T)))
    traceNorm = np.sum(np.diag(D))

    return Zk, traceNorm


# --------------------------------------------------------------------------------------

def sigmoid(t):
    # sigmoid function, 1 / (1 + exp(-t))
    # stable computation
    idx = t > 0
    out = np.zeros_like(t)
    out[idx] = 1. / (1 + np.exp(-t[idx]))
    exp_t = np.exp(t[~idx])
    out[~idx] = exp_t / (1. + exp_t)
    return out

def log_loss(Z):
    # stable computation of the logistic loss
    idx = Z > 0
    out = np.zeros_like(Z)
    out[idx] = np.log(1 + np.exp(-Z[idx]))
    out[~idx] = (-Z[~idx] + np.log(1 + np.exp(Z[~idx])))
    return out


def GLoss(X,Theta,w,S):
    Xw = X.dot(w)
    Alpha = Theta[:, None] - Xw
    Sigma = S * sigmoid(-S * Alpha)
    Sigma = Sigma.sum(0)
    gloss = Sigma.reshape(-1,1) @ w.reshape(1,-1)
    return gloss

def Loss(X, Theta, w, S):
    return np.sum(log_loss(S * (Theta[:, None] - X.dot(w))))

def MAY_mask_mc(X, y, mask, **kwargs):

    X = np.asarray(X)
    y = np.asarray(y).astype(int)
    n_Class = len(np.unique(y))
    # M = np.array([[(i - j)**2 for i in range(n_Class)] for j in range(n_Class)])
    # T_lab = M[y,:]
    n_samples, n_features = X.shape
    max_in_iter = kwargs.pop('max_in_iter', 100)
    max_out_iter = kwargs.pop('max_out_iter', 10)
    lambda1 = kwargs.pop('lambda1', 1)
    lambda2 = kwargs.pop('lambda2', 1/(n_Class-1))
    # lambda2 = kwargs.pop('lambda2', 1)
    check_para = kwargs.pop('check_para', True)
    obrT = mask
    if check_para:
        obrT = np.asarray(mask)
        assert obrT.shape[0] == X.shape[0] and obrT.shape[1] == X.shape[1]
        ue = np.unique(obrT)
        assert len(ue) == 2
        assert 0 in ue
        assert 1 in ue
    X_obr = np.zeros((n_samples, n_features))
    X_obr += obrT * X
    n_samples, n_features = X.shape
    lambda2 /= n_samples
    theta0 = 1
    theta1 = 1
    Z0 = np.zeros((n_samples, n_features))
    Z1 = np.zeros((n_samples, n_features))
    ineqLtemp0 = np.zeros((n_samples, n_features))
    ineqLtemp1 = ineqLtemp0
    L = 2
    converge_out = np.zeros((max_in_iter, 1))



    X_mc = X_obr
    for i in range(max_out_iter):

        model = LogisticAT()
        model.fit(X=X_mc, y=y)
        w = model.coef_
        Theta = model.theta_
        # print("Theta::",Theta)
        S = np.sign(np.arange(n_Class - 1)[:, None] - y + 0.5)

        convergence = np.zeros(max_in_iter)


        for k in range(max_in_iter):
            Y = Z1 + theta1 * (1 / theta0 - 1) * (Z1 - Z0)
            svd_obj_temp_temp = (theta1 * (1 / theta0 - 1) + 1) * ineqLtemp1 - theta1 * (
                    1 / theta0 - 1) * ineqLtemp0 - X_obr

            # TODO W 1

            svd_obj_temp = svd_obj_temp_temp + 2 * lambda2 * GLoss(X=Y,Theta=Theta,w=w,S=S)
            svd_obj = Y - 1 / L * svd_obj_temp
            Z0 = copy.copy(Z1)
            Z1, traceNorm = _svd_threshold(svd_obj, lambda1 / L)

            ineqLtemp0 = ineqLtemp1
            # do not know whether it is element wise or not
            ineqLtemp1 = Z1 * obrT

            # TODO W 2
            ineqL = np.linalg.norm(ineqLtemp1 - X_obr, ord='fro') ** 2 / 2 + Loss(X=Z1,Theta=Theta, w=w, S=S) * lambda2

            # TODO W 3
            ineqRtemp = sum(sum(svd_obj_temp_temp ** 2)) / 2 + Loss(X=Z1,Theta=Theta, w=w, S=S)  * lambda2 - svd_obj_temp.flatten().dot(Y.flatten())
            ineqR = ineqRtemp + svd_obj_temp.flatten().dot(Z1.flatten()) + L / 2 * sum(sum((Z1 - Y) ** 2))

            while ineqL > ineqR:
                L = L * 2
                svd_obj = Y - 1 / L * svd_obj_temp
                Z1, traceNorm = _svd_threshold(svd_obj, lambda1 / L)

                ineqLtemp1 = Z1 * obrT
                # TODO W 4

                ineqL = np.linalg.norm(ineqLtemp1 - X_obr, ord='fro') ** 2 / 2 + Loss(X=Z1,Theta=Theta, w=w, S=S) * lambda2
                ineqR = ineqRtemp + svd_obj_temp.flatten().dot(Z1.flatten()) + L / 2 * sum(sum((Z1 - Y) ** 2))

            theta0 = theta1
            theta1 = (np.sqrt(theta1 ** 4 + 4 * theta1 ** 2) - theta1 ** 2) / 2

            convergence[k] = ineqL + lambda1 * traceNorm

            # judge convergence
            if k == 0:
                minObj = convergence[k]
                X_mc = Z1
            else:
                if convergence[k] < minObj:
                    minObj = convergence[k]
                    X_mc = Z1
            if k > 0:
                if np.abs(convergence[k] - convergence[k - 1]) < ((1e-6) * convergence[k - 1]):
                    break

        converge_out[i] = minObj
        if i == 0:
            minObj_out = converge_out[k]
            Xmc = X_mc
            # print("Xmc_1 ====&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

DeniuHe

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

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

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

打赏作者

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

抵扣说明:

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

余额充值