嵌入式选择与L1正则化
来源:互联网 发布:法语网络教学视频 编辑:程序博客网 时间:2024/06/06 04:20
参考代码:
(需将SpectralClassificationTrain.mat文件放入与下面py文件同文件夹下)
# Author: Irene Long# Date: 20170621import numpy as npimport scipy.io as sioimport pandas as pdimport matplotlib.pyplot as pltdef 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# Load datasetssc_train = sio.loadmat('SpectralClassificationTrain.mat')X = np.array( pd.DataFrame( sc_train['train_x'] ) )Y = sc_train['train_y'][:,0]num, d = X.shapeW = np.random.random( [ 1, d ] )L = get_L( X )lambd = 2z = get_z( W, L, X, Y )flag = lambd / Lj = 0f0 = 0count = 0while( 1 ): count += 1 print( 'count=',count ) for i in range( d ): if z[ 0, i ] > flag: W[ 0, i ] = z[ 0, i ] - flag elif z[ 0, i ] < -flag: W[ 0, i ] = z[ 0, i ] + flag else: W[ 0, i ] = 0 f = np.sum( W == 0 ) print( 'number of 0:', f ) 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( 'final:', np.sum( W == 0 ) )
阅读全文
0 0
- 嵌入式选择与L1正则化
- L1、L2正则化
- L1 L2正则化
- L1范数正则化
- L1,L2正则化
- L1、L2正则化
- L1、L2正则化
- 机器学习:L1与L2正则化项
- 机器学习——正则化 (L1与L2范数)
- L1,L2 正则化与过拟合问题
- 机器学习:L1与L2正则化项
- L1和L2正则化
- L1和L2正则化
- L0和L1:正则化
- L1以及L2正则化
- ISLR线性模型选择与正则化
- 【小结】机器学习中的正则化范数 -- L1范数与L2范数
- 机器学习中的正则化技术L0,L1与L2范数
- Python学习笔记-IP地址处理模块Ipy
- R极简教程-7:读取数据
- LOJ 6165. 一道水题 (线性筛)
- mybatis入门基础(六)----高级映射(一对一,一对多,多对多)
- 服务器暴力破解的程序(python开发)
- 嵌入式选择与L1正则化
- opensips压力测试创建用户脚本
- 44 linux内核里的platform设备驱动模型
- 树莓派 SD卡镜像备份
- 面试总结复习2
- codeforces round#420
- 获取MD5、SHA1、SHA256码
- 进程相关工具类
- Android学习基础,NDK入门