LIBTwinSvm代码解读中遇到的问题(二)

解读estimators.py模块

In this module, Standard TwinSVM and Least Squares TwinSVM estimators are defined.
在此模块中,定义了标准TwinSVM和最小二乘TwinSVM估计器。
模块代码采用clipDCD 算法求解原始问题的对偶问题。

1.optimize()函数:

It solves a dual optimization problem using clipDCD algorithm。

Parameters:
mat_dual : array-like, shape (n_samples, n_samples)
   The Q matrix of dual QPP problems.
c : float
    The penalty parameter
Returns:list  ---- Lagrange multipliers
2.rbf_kernel(x, y, u)函数:

函数功能:
It transforms samples into higher dimension using Gaussian (RBF) kernel.

参数解释:
x, y : array-like, shape (n_features,)
A feature vector or sample.

u : float
Parameter of the RBF kernel function.

Returns:float
Value of kernel matrix for feature vector x and y.

return np.exp(-2 * u) * np.exp(2 * u * np.dot(x, y))

如:

rbf_kernel(X,self.mat_C_t, self.gamma)
3.class BaseTSVM(BaseEstimator):
类功能:Base class for TSVM-based estimators

(1)class BaseEstimator(builtins.object): Base class for all estimators in scikit-learn
(2)使用方式:BaseTSVM(kernel, rect_kernel, C1, C2, gamma)
(3)参数解释:

kernel : str
Type of the kernel function which is either 'linear' or 'RBF'.
rect_kernel : float
    Percentage of training samples for Rectangular kernel.
C1 : float
    Penalty parameter of first optimization problem.
C2 : float
    Penalty parameter of second optimization problem.
gamma : float
    Parameter of the RBF kernel function.

(4)属性Attributes解释:

mat_C_t : array-like, shape = [n_samples, n_samples]
    A matrix that contains kernel values.
cls_name : str
    Name of the classifier.
w1 : array-like, shape=[n_features]
    Weight vector of class +1's hyperplane.
b1 : float
    Bias of class +1's hyperplane.
w2 : array-like, shape=[n_features]
    Weight vector of class -1's hyperplane.
b2 : float
    Bias of class -1's hyperplane.
类中的函数:

【1】def __init__(self, kernel, rect_kernel, C1, C2, gamma):
Initialize self.
参数含义见上。

【2】def check_clf_params(self):
Checks whether the estimator’s input parameters are valid.

【3】def get_params_names(self):
For retrieving the names of hyper-parameters of the TSVM-based estimator.
Returns:
parameters : list of str, {[‘C1’, ‘C2’], [‘C1’, ‘C2’, ‘gamma’]} Returns the names of the hyperparameters which are same as the class’ attributes.

【4】def fit(self, X, y):

It fits a TSVM-based estimator.
THIS METHOD SHOULD BE IMPLEMENTED IN CHILD CLASS.
Parameters:
X : array-like, shape (n_samples, n_features)
Training feature vectors, where n_samples is the number of samples
and n_features is the number of features.
y : array-like, shape(n_samples,)
Target values or class labels.

Impelement fit method in child class

【5】 def decision_function(self, X):

Computes distance of test samples from both non-parallel hyperplanes,返回包含测试样本到俩个超平面距离|W*K(X,X)+b|的列表:[-1负类的距离,+1正类的距离]
Parameters:
X : array-like, shape (n_samples, n_features)
Returns:
array-like, shape(n_samples, 2)
distance from both hyperplanes.

【6】def predict(self, X):
Performs classification on samples in X using the TSVM-based model.
Assign data points to class +1 or -1 based on distance from hyperplanes
Parameters:
X : array-like, shape (n_samples, n_features)
Feature vectors of test data.
Returns:
array, shape (n_samples,)
Predicted class lables(1,-1) of test data.

4.class TSVM(BaseTSVM)::
类功能:

Standard Twin Support Vector Machine for binary classification.
It inherits attributes of : class:BaseTSVM.
TSVM()是BaseTSVM()的子类,TSVM()继承父类BaseTSVM()的所有属性和方法,同时可以定义自己的属性和方法。

(1)使用方式:

TSVM(kernel='linear', rect_kernel=1, C1=1, C2=1, gamma=1)

(2)参数解释:
kernel : str, optional (default=‘linear’)
Type of the kernel function which is either ‘linear’ or ‘RBF’.

rect_kernel : float, optional (default=1.0)
Percentage of training samples for Rectangular kernel.

C1 : float, optional (default=1.0)
Penalty parameter of first optimization problem.

C2 : float, optional (default=1.0)
Penalty parameter of second optimization problem.

gamma : float, optional (default=1.0)
Parameter of the RBF kernel function.

类中定义的函数:

【1】def __init__(self, kernel='linear', rect_kernel=1, C1=2**0, C2=2**0,gamma=2**0):

Initialize self.让子类TSVM()继承BaseTSVM()的所有属性和方法,定义自己的clf_name = 'TSVM'

【2】def fit(self, X_train, y_train):

函数功能:
It fits the binary TwinSVM model according to the given training data.
Parameters:
X_train : array-like, shape (n_samples, n_features)
Training feature vectors, where n_samples is the number of samples
and n_features is the number of features.

y_train : array-like, shape(n_samples,)
Target values or class labels.

代码解释:

  mat_A= X_train[y_train == 1]
  # Matrix A or class 1 samples,类标签为1的数据矩阵
  mat_B= X_train[y_train == -1]
  # Matrix B  or class -1 data,类标签为-1的数据矩阵
# Vectors of ones
mat_e1 = np.ones((mat_A.shape[0], 1))
#与mat_A数组第一维长度相同的单位向量,即与mat_A具有相同行的单位列向量
mat_e2 = np.ones((mat_B.shape[0], 1)) 
#与mat_B数组第一维长度相同的单位向量,即与mat_B具有相同行的单位列向量

若核函数为线性核函数:

  if self.kernel == 'linear': 
     # Linear kernel如果核函数为线性核函数
        mat_H = np.column_stack((mat_A, mat_e1))
        # mat_H=[mat_A, mat_e1]
        mat_G = np.column_stack((mat_B, mat_e2))
        # mat_G=[mat_B, mat_e2]

若核函数为高斯核函数(径向基核函数RBF):

mat_C = np.row_stack((mat_A, mat_B))
#mat_A与mat_B按行合并为mat_C
self.mat_C_t = np.transpose(mat_C)[:, :int(mat_C.shape[0] * self.rect_kernel)]
#mat_C_t为mat_C的转置
                                                   
mat_H = np.column_stack((rbf_kernel(mat_A, self.mat_C_t, self.gamma), mat_e1))
#非线性TWSVM时的S=mat_H                                            
mat_G = np.column_stack((rbf_kernel(mat_B, self.mat_C_t, self.gamma), mat_e2))
#非线性TWSVM时的R=mat_G  

求解TWSVM1

Compute inverses计算逆:

# Regulariztion term used for ill-possible condition
reg_term = 2 ** float(-7)#reg_term=2**(-7)
mat_H_H = np.linalg.inv(np.dot(mat_H_t, mat_H) + (reg_term * np.identity(mat_H.shape[1])))
#mat_H_H=inv(mat_H_t*mat_H)                       

1类(正类)的Wolfe对偶问题:

mat_dual1 = np.dot(np.dot(mat_G, mat_H_H), mat_G_t)
#mat_dual1=G*(inv(H_T*H))*G_T=mat_G*mat_H_H*mat_G_t

**使用ClipDCD算法求得Lagrange乘子:alpha_d1**

#Obtaining Lagrange multipliers using ClipDCD optimizer
alpha_d1 = clipdcd.optimize(mat_dual1,self.C1).reshape(mat_dual1.shape[0], 1)

求得TWSVM1的超平面hyper_p_1:

# Obtain hyperplanes:hyper_p_1=[w(1),b(1)]_T
hyper_p_1 = -1 * np.dot(np.dot(mat_H_H, mat_G_t), alpha_d1)
#hyper_p_1 =[w(1),b(1)]_T=-1*(inv((H_T*H)))*G_T*alpha_d1

释放内存:

#释放正类的对偶问题mat_dual1,mat_H_H=inv(mat_H_t*mat_H)
del mat_dual1, mat_H_H

求解TWSVM2

Compute inverses计算逆:

# Regulariztion term used for ill-possible condition
mat_G_G = np.linalg.inv(np.dot(mat_G_t, mat_G) + (reg_term * np.identity(mat_G.shape[1])))
#mat_G_G=inv(mat_G_t*mat_G)                       

-1类(负类)的Wolfe对偶问题:

mat_dual2 = np.dot(np.dot(mat_H, mat_G_G), mat_H_t)
#mat_dual2=H*(inv(G_T*G))*H_T=mat_H*matG_G*mat_H_t

**使用ClipDCD算法求得Lagrange乘子:alpha_d1**

#Obtaining Lagrange multipliers using ClipDCD optimizer
alpha_d2 = clipdcd.optimize(mat_dual2, self.C2).reshape(mat_dual2.shape[0], 1)

求得TWSVM2的超平面hyper_p_2:

# Obtain hyperplanes:hyper_p_1=[w(1),b(1)]_T
hyper_p_2 = np.dot(np.dot(mat_G_G, mat_H_t), alpha_d2)
#hyper_p_2 =[w(2),b(2)]_T=(inv((G_T*G)))*H_T*alpha_d2

取出俩个超平面的法向量w和截距b:

# Class 1
self.w1 = hyper_p_1[:hyper_p_1.shape[0] - 1, :]
self.b1 = hyper_p_1[-1, :]

# Class -1
self.w2 = hyper_p_2[:hyper_p_2.shape[0] - 1, :]
self.b2 = hyper_p_2[-1, :]

class LSTSVM(BaseTSVM): 不作解释

参见知识点:

1.lambda表达式
lambda 参数:操作(参数)
lambda [arg1[,arg2,arg3…argN]]:expression
通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是指匿名函数
lambda所表示的匿名函数的内容简单,lambda用来定义一个匿名函数。
lambda表达式返回的是function类型。
lambda语句中,冒号前是参数,可以有多个,用逗号隔开,冒号右边的返回值。lambda语句构建的其实是一个函数对象。如

add = lambda x, y : x+y
add(1,2)  # 结果为3

如果没有参数,则lambda冒号前面就没有,返回冒号右边的返回值。如{'linear': lambda: X}

2.np.dot()函数用法
Numpy中dot()函数主要功能有两个:向量点内积和矩阵乘法。
格式:x.dot(y) 等价于 np.dot(x,y) ———x是m*n 矩阵 ,y是n*m矩阵,则x.dot(y) 得到m*m矩阵
【1】如果处理的是一维数组,则得到的是两数组的內积,点乘求出来是一个数值,即对应元素相乘后相加。
【2】如果是二维数组(矩阵)之间的运算,则得到的是矩阵乘积。表示矩阵乘法,求得的是一个矩阵。

3.np.column_stack((a,b))
返回左右根据列拼接的列表,column_stack,row_stack函数参数是一个元组

a = [[1,2,7],
     [-6,-2,-3],
     [-4,-8,-55]
     ]
b = [3,5,6]
a = np.array(a)
b = np.array(b)
a_b_column = np.column_stack((a,b))#左右根据列拼接
a_b_row = np.row_stack((a,b))#上下按照行拼接
print('a_b_column')
print(a_b_column)
print('a_b_row')
print(a_b_row)

运行结果:
在这里插入图片描述
4.numpy.argmin()函数
格式:numpy.argmin(a, axis=None, out=None)
返回:返回沿轴的最小值的索引。
Returns the indices of the minimum values along an axis.
Parameters:
a : array_like
Input array.

axis : int, optional
By default, the index is into the flattened array, otherwise along the specified axis.

out : array, optional
If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Returns:
index_array : ndarray of ints,下标组成的数组。shape与输入数组a去掉axis的维度相同。
Array of indices into the array. It has the same shape as a.shape with the dimension along axis removed.

a = np.arange(6).reshape(2,3)
print(a)
[[0 1 2]
 [3 4 5]]
print(np.argmin(a))
0
print(np.argmin(a, axis=0))#横轴方向上的
[0 0 0]
print(np.argmin(a, axis=1))#纵轴方向上的
[0 0]

注意:多个最小值时,只显示第一个,如

b = np.arange(6)
b[4] = 0
print(b)
print(np.argmin(b))
[0 1 2 3 0 5]
0

5.np.arange()函数

返回值: np.arange()函数返回一个有终点和起点的固定步长的排列,如[1,2,3,4,5],起点是1,终点是5,步长为1。
参数个数情况: np.arange()函数分为一个参数,两个参数,三个参数三种情况
1)一个参数时,参数值为终点+1,起点取默认值0,步长取默认值1。
2)两个参数时,第一个参数为起点,第二个参数为终点+1,步长取默认值1。
3)三个参数时,第一个参数为起点,第二个参数为终点+步长,第三个参数为步长。其中步长支持小数。

a = np.arange(3)
#一个参数 默认起点0,终点为3-1=2,步长为1 ,输出:[0 1 2]
print(a)
[0 1 2]

a = np.arange(3,9)
#两个参数 起点为3,终点为9-1=8,默认步长为1,输出[3 4 5 6 7 8]
print(a)
[3 4 5 6 7 8]

a = np.arange(0, 3, 0.2)
#三个参数 起点为0,终点为3-0.2=2.8,步长为0.2,输出[ 0.   0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.   1.1  1.2  1.3  1.4 1.5  1.6  1.7  1.8  1.9  2.   2.1  2.2  2.3  2.4  2.5  2.6  2.7  2.8 ]
print(a)
[0.  0.2 0.4 0.6 0.8 1.  1.2 1.4 1.6 1.8 2.  2.2 2.4 2.6 2.8]

6.Numpy.array()函数
该函数用来产生数组。
使用形式:

numpy.array(object, 
    dtype=None, 
    copy=True, 
    order='K', 
    subok=False, 
    ndmin=0)

参数解释:
object:必选参数,类型为array_like,可以有四种类型:数组,公开数组接口的任何对象,__array__方法返回数组的对象,或任何(嵌套)序列。np.array()的作用就是按照一定要求将object转换为数组

dtype:可选参数,用来表示数组元素的类型。默认类型将被确定为保持序列中的对象所需的最小类型。注: This argument can only be used to ‘upcast’ the array. For downcasting, use the .astype(t) method.

copy:可选参数,类型为bool值。如果为true(默认值),则复制对象。否则的话只有在以下三种情况下才会返回副本:(1).if array returns a copy;(2). if obj is a nested sequence;(3). if a copy is needed to satisfy any of the other requirements (dtype, order, etc.)

order:{‘K’, ‘A’, ‘C’, ‘F’},optional 。指定阵列的内存布局。

subok:可选参数,类型为bool值。如果为True,则子类将被传递,否则返回的数组将被强制为基类数组(默认)。或者说,True:使用object的内部数据类型,False:使用object数组的数据类型。

ndmin:可选参数,类型为int型。指定结果数组应具有的最小维数。

返回对象:
out:输出ndarray,满足指定要求的数组对象。

实例:

X_train = np.array(X_train, dtype=np.float64)

7. isinstance() 函数
Python 内置函数,isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type()。

isinstance() 与 type() 区别:
type() 不会认为子类是一种父类类型,不考虑继承关系。
isinstance() 会认为子类是一种父类类型,考虑继承关系。

如果要判断两个类型是否相同推荐使用 isinstance()。

使用语法:

 isinstance(object, classinfo)

参数解释:
object – 实例对象。
classinfo – 可以是直接或间接类名、基本类型或者由它们组成的元组。

返回值
如果对象的类型与参数二的类型(classinfo)相同则返回 True,否则返回 False。
实例1:

a = 2
isinstance (a,int)
True
isinstance (a,str)
False
isinstance (a,(str,int,list))    # 是元组中的一个返回 True
True

实例2:

class A:
    pass
 
class B(A):
    pass
 
isinstance(A(), A)    # returns True
type(A()) == A        # returns True
isinstance(B(), A)    # returns True
type(B()) == A        # returns False

实例3:

isinstance(X_train,   list)

8.numpy.linalg模块

NumPy 提供了线性代数函数库 linalg,该库包含了线性代数所需的所有功能。
numpy.linalg模块包含线性代数的函数。使用这个模块,可以计算逆矩阵、求特征值、解线性方程组以及求解行列式等。
numpy.linalg.inv():
numpy.linalg.inv() 函数计算矩阵的乘法逆矩阵

逆矩阵(inverse matrix):设A是数域上的一个n阶矩阵,若在相同数域上存在另一个n阶矩阵B,使得: AB=BA=E ,则我们称B是A的逆矩阵,而A则被称为可逆矩阵。注:E为单位矩阵。

实例:

import numpy as np 
x = np.array([[1,2],[3,4]]) 
y = np.linalg.inv(x) 
print (x)
print (y)
print (np.dot(x,y))
#输出:
[[1 2]
 [3 4]]
[[-2.   1. ]
 [ 1.5 -0.5]]
[[1.0000000e+00 0.0000000e+00]
 [8.8817842e-16 1.0000000e+00]]

9.numpy.identity()
使用语法:
numpy.identity(n, dtype=None)

参数解释:

n : int  int型表示的是输出的矩阵的行数和列数都是n
Number of rows (and columns) in n x n output.
dtype : data-type, optional  表示的是输出的类型,默认是float
Data-type of the output. Defaults to float.

返回值:
Return the identity array. ndarray
n x n array with its main diagonal set to one, and all other elements 0.
The identity array is a square array with ones on the main diagonal.

实例:

np.identity(3)
#输出:
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

9.numpy.reshape()
Gives a new shape to an array without changing its data.

使用语法:

numpy.reshape(a, newshape, order='C')

参数解释:

a : array_like
Array to be reshaped.
newshape : int or tuple of ints

返回值:
Returns: reshaped_array : ndarray

实例:

a = np.zeros((10, 2))
print(a)
b = a.reshape(4,5)
print(b)
c = a.reshape(20,1)
print(c)
#输出:
[[0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]]
[[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]
[[0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]]

需要完整代码或书籍PDF版的小伙伴可关注微信公众号:菜田里守望者
在这里插入图片描述
打开微信扫一扫关注吧,你们的支持就是我的动力

参考文献:
[1]https://github.com/mir-am/LIBTwinSVM/tree/master/libtsvm
[2]https://blog.csdn.net/sinat_28576553/article/details/89047893#1.%20Numpy.array()%E8%AF%A6%E8%A7%A3
[3]https://www.runoob.com/python/python-func-isinstance.html
[4]https://www.cnblogs.com/xieshengsen/p/6836430.html
[5]https://www.runoob.com/numpy/numpy-linear-algebra.html
[6]https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.reshape.html?highlight=reshape
[7]https://blog.csdn.net/qq_28618765/article/details/78083895

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值