LASSO推导及其在恒星光谱上的应用

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

import numpy as np
import scipy.io as sio
import pandas as pd
import matplotlib.pyplot as plt

def Lasso_first_derivatives(X,Y,W):
    '''
    Return the value of the first derivatives of f when the argument is X, Y, W.

    Parameters
    ----------
    X : numpy.array
        The amount of rows is the amount of the features.
        The amount of columns is the amount of the samples.
    Y : numpy.array
        The labels of samples.
        Y.shape = [ 1, samples ]
    W : numpy.array
        The weights in the linear fiting.
        W.shape = [ features, 1 ]

    Returns
    -------
    The value of the first derivatives of f when the argument is X, Y, W. 
    In the shape of [ 1, samples ].
    '''
    Y_minus_WTX = Y - W.T * X
    return 2 * np.sum(Y_minus_WTX * X , axis=1)

def Lasso_second_derivative( X ):
    '''
    Return the value of the second derivatives of f when the argument is X.

    Parameters
    ----------
    X : numpy.array
        The amount of rows is the amount of the samples.
        The amount of columns is the amount of the features.

    Returns
    -------
    The value of the second derivatives of f when the argument is X. 
    The value is a floating number. 
    '''
    v = []
    for i in range( X.shape[ 0 ] ):
        v.append( np.dot( X[ i ], X[ i ] ) )
    return np.sum( np.array( v ) )

def get_L( X ):
    '''
    Return L in the theory.

    Parameters
    ----------
    X : numpy.array
        The amount of rows is the amount of the samples.
        The amount of columns is the amount of the features.

    Returns
    -------
    L = 0.5 * Lasso_second_derivative( X )
    The value is a floating number.
    '''
    return 0.5 * Lasso_second_derivative( X )

def get_z( W, L, X, Y ):
    '''
    Return z in the theory.

    Parameters
    ----------
    X : numpy.array
        The amount of rows is the amount of the samples.
        The amount of columns is the amount of the features.

    Returns
    -------
    z = W - Lasso_first_derivatives( X.T, Y, W ) / L
    The result is a vector in the shape of [ 1, features ].
    '''
    return W - Lasso_first_derivatives( X.T, Y, W ) / L


def load_data():
    sc_train = sio.loadmat('SpectralClassificationTrain.mat')
    sc_test = sio.loadmat('SpectralClassificationTest.mat')
    X = np.array( pd.DataFrame( sc_train['train_x'] ) )
    Y = sc_train['train_y'][:,0]
    X_test = np.array( pd.DataFrame( sc_test['test_x'] ) )
    Y_test = sc_test['test_y'][:,0]
    return X,Y,X_test,Y_test

def get_W(W,L,lambd,z,flag):
    j = 0
    f0 = 0
    while True:
        for i in range( d ):
            if abs( z[ 0, i ] ) > flag:
                W[ 0, i ] = z[ 0, i ] - flag
            else:
                W[ 0, i ] = 0
        f = np.sum( W == 0 )
        if f0 == f:
            j += 1
        # If the amount of 0 in W is unchanged in 20 iterations,then stop the looping.
        if j == 20: 
            break
        else:
            f0 = f
            z = get_z( W, L, X, Y )
    print 'W.dimension:', np.sum( W == 0 )
    return W

X,Y,X_test,Y_test = load_data()

num, d = X.shape
W = np.random.random( [ 1, d ] )
L = get_L( X )
lambd = 2

z = get_z( W, L, X, Y )
flag = lambd / L
W = get_W(W,L,lambd,z,flag)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值