<jupyter_start><jupyter_text>以下为算法： SVR<jupyter_code>def kernel(x,y,gamma):
    return np.exp(-(np.sum((x-y)**2)*gamma))

def eps_svr(X_train, Y_train, X_test, epsilon, c, kernel_param):
    
    t_start = time.clock()
    
    """implements the CVXOPT version of epsilon SVR"""
    m, n = X_train.shape      #m is num samples, n is num features
    #Finding the kernels i.e. k(x,x')
    k = np.zeros((m,m))
    for i in range(m):
        for j in range(m):
            k[i][j] = kernel(X_train[i,:], X_train[j,:], kernel_param)
    
    P= np.hstack((k,-1*k))
    P= np.vstack((P,-1*P))
    q= epsilon*np.ones((2*m,1))
    qadd=np.vstack((-1*Y_train,Y_train))
    q=q+qadd
    A=np.hstack((np.ones((1,m)),-1*(np.ones((1,m)))))
    
    #define matrices for optimization problem       
    P = cvxopt.matrix(P)
    q = cvxopt.matrix(q)
    A = cvxopt.matrix(A)
    b = cvxopt.matrix(np.zeros((1,1)))
    
    c= float(c)
    temp=np.vstack((np.eye(2*m),-1*np.eye(2*m)))
    G=cvxopt.matrix(temp)
    
    temp=np.vstack((c*np.ones((2*m,1)),np.zeros((2*m,1))))
    h = cvxopt.matrix(temp)
    #solve the optimization problem
    sol = cvxopt.solvers.qp(P,q,G,h,A,b,solver='glpk')
    #lagrange multipliers
    l = np.ravel(sol['x'])
    #extracting support vectors i.e. non-zero lagrange multiplier
    alpha=l[0:m]
    alpha_star=l[m:]
    
    bias= sol['y']
    print("bias="+str(bias))
    #find weight vector and predict y
    Y_pred = np.zeros(len(X_test))
    for i in range(len(X_test)):
        res=0
        for u_,v_,z in zip(alpha,alpha_star,X_train):
            res+=(u_ - v_)*kernel(X_test[i],z,kernel_param)
        Y_pred[i]= res
    Y_pred = Y_pred+bias[0,0]
    
    t_end = time.clock()
    
    return Y_pred, (t_end - t_start)<jupyter_output><empty_output><jupyter_text>LSSVR<jupyter_code>def lssvr(X_train, Y_train, X_test, kernel_param):
    
    t_start = time.clock()
    
    m, n = X_train.shape      #m is num samples, n is num features
    #Finding the kernels i.e. k(x,x')
    k = np.zeros((m,m))
    for i in range(m):
        for j in range(m):
            k[i][j] = kernel(X_train[i,:], X_train[j,:], kernel_param)
    
    temp_v = np.row_stack((np.ones(m), k))
    temp_h = np.row_stack(([0],np.ones((m,1))))
    A = np.column_stack((temp_h,temp_v ) )
    temp_b = np.row_stack(([0],Y_train))
    params =  np.dot( np.linalg.pinv(A),temp_b )
    alpha = params[1:]
    bias = params[0]
    Y_pred = np.zeros(len(X_test))
    for i in range(len(X_test)):
        res=0
        for u_,z in zip(alpha, X_train):
            res+=u_*kernel(X_test[i],z,kernel_param)
        Y_pred[i]= res
    Y_pred = Y_pred+bias
    
    t_end = time.clock()
    
    return Y_pred,alpha,bias, t_end - t_start<jupyter_output><empty_output><jupyter_text>LSTSVR<jupyter_code>def lstsvr(X_train, Y_train, X_test, eps1, eps2, kernel_param):
    
    m, n = X_train.shape      #m is num samples, n is num features
    
    Y_pred,alpha,bias,duration_lssvr = lssvr(X_train, Y_train, X_test, 0.25)
    
    Y_pred_l,alpha_l,bias_l,duration_lssvr_l = lssvr(X_train, Y_train - eps1, X_test, 0.25)
    
    Y_pred_u,alpha_u,bias_u,duration_lssvr_u = lssvr(X_train, Y_train + eps2, X_test, 0.25)
    
    return Y_pred,Y_pred_l,Y_pred_u, duration_lssvr<jupyter_output><empty_output><jupyter_text>以下为算法的测试代码：<jupyter_code>X = np.array([1, 1])
X = np.row_stack((X, np.array([1, 2])))
X = np.row_stack((X, np.array([2, 1])))
X = np.row_stack((X, np.array([2, 2])))

Y = np.array([[2, 3, 3, 4]]).T<jupyter_output><empty_output><jupyter_text>SVR<jupyter_code>Yt_svr,duration_svr = eps_svr(X, Y, X, 0.1, 2, 0.25)
Yt_svr
duration_svr<jupyter_output><empty_output><jupyter_text>LSSVR<jupyter_code>Yt_lssvr,alpha,bias,duration_lssvr = lssvr(X, Y, X, 0.25)
Yt_lssvr
alpha
bias<jupyter_output><empty_output><jupyter_text>LSTSVR<jupyter_code>Yt_lstsvr,Yt_lstsvr_l, Yt_lstsvr_u,duration_lstsvr = lstsvr(X, Y, X, 0.05, 0.05, 0.25)
Yt_lstsvr
Yt_lstsvr_l
Yt_lstsvr_u<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1656210790464241, 'fraction_numerical': 0.0178168130489335, 'mean_word_length': 3.4887387387387387, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '481434', 'license': 'Apache-2.0', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1802, 'n_words': 287, 'path': 'train.ipynb', 'repo_name': 'Shi-YangWang/kpi-prediction-ar-ls-tsvr', 'size': '3985'}