package libsvm;
import java.io.*;
import java.util.*;
//
// Kernel Cache
//
// l is the number of total data items
// size is the cache size limit in bytes
//
class Cache {
private final int l;
private long size;
private final class head_t
{
head_t prev, next; // a cicular list
float[] data;
int len; // data[0,len) is cached in this entry
}
private final head_t[] head;
private head_t lru_head;
Cache(int l_, long size_)
{
l = l_;
size = size_;
head = new head_t[l];
for(int i=0;i<l;i++) head[i] = new head_t();
size /= 4;
size -= l * (16/4); // sizeof(head_t) == 16
size = Math.max(size, 2* (long) l); // cache must be large enough for two columns
lru_head = new head_t();
lru_head.next = lru_head.prev = lru_head;
}
private void lru_delete(head_t h)
{
// delete from current location
h.prev.next = h.next;
h.next.prev = h.prev;
}
private void lru_insert(head_t h)
{
// insert to last position
h.next = lru_head;
h.prev = lru_head.prev;
h.prev.next = h;
h.next.prev = h;
}
// request data [0,len)
// return some position p where [p,len) need to be filled
// (p >= len if nothing needs to be filled)
// java: simulate pointer using single-element array
int get_data(int index, float[][] data, int len)
{
head_t h = head[index];
if(h.len > 0) lru_delete(h);
int more = len - h.len;
if(more > 0)
{
// free old space
while(size < more)
{
head_t old = lru_head.next;
lru_delete(old);
size += old.len;
old.data = null;
old.len = 0;
}
// allocate new space
float[] new_data = new float[len];
if(h.data != null) System.arraycopy(h.data,0,new_data,0,h.len);
h.data = new_data;
size -= more;
do {int _=h.len; h.len=len; len=_;} while(false);
}
lru_insert(h);
data[0] = h.data;
return len;
}
void swap_index(int i, int j)
{
if(i==j) return;
if(head[i].len > 0) lru_delete(head[i]);
if(head[j].len > 0) lru_delete(head[j]);
do {float[] _=head[i].data; head[i].data=head[j].data; head[j].data=_;} while(false);
do {int _=head[i].len; head[i].len=head[j].len; head[j].len=_;} while(false);
if(head[i].len > 0) lru_insert(head[i]);
if(head[j].len > 0) lru_insert(head[j]);
if(i>j) do {int _=i; i=j; j=_;} while(false);
for(head_t h = lru_head.next; h!=lru_head; h=h.next)
{
if(h.len > i)
{
if(h.len > j)
do {float _=h.data[i]; h.data[i]=h.data[j]; h.data[j]=_;} while(false);
else
{
// give up
lru_delete(h);
size += h.len;
h.data = null;
h.len = 0;
}
}
}
}
}
//
// Kernel evaluation
//
// the static method k_function is for doing single kernel evaluation
// the constructor of Kernel prepares to calculate the l*l kernel matrix
// the member function get_Q is for getting one column from the Q Matrix
//
abstract class QMatrix {
abstract float[] get_Q(int column, int len);
abstract double[] get_QD();
abstract void swap_index(int i, int j);
};
abstract class Kernel extends QMatrix {
private svm_node[][] x;
private final double[] x_square;
// svm_parameter
private final int kernel_type;
private final int degree;
private final double gamma;
private final double coef0;
abstract float[] get_Q(int column, int len);
abstract double[] get_QD();
void swap_index(int i, int j)
{
do {svm_node[] _=x[i]; x[i]=x[j]; x[j]=_;} while(false);
if(x_square != null) do {double _=x_square[i]; x_square[i]=x_square[j]; x_square[j]=_;} while(false);
}
private static double powi(double base, int times)
{
double tmp = base, ret = 1.0;
for(int t=times; t>0; t/=2)
{
if(t%2==1) ret*=tmp;
tmp = tmp * tmp;
}
return ret;
}
double kernel_function(int i, int j)
{
switch(kernel_type)
{
case svm_parameter.LINEAR:
return dot(x[i],x[j]);
case svm_parameter.POLY:
return powi(gamma*dot(x[i],x[j])+coef0,degree);
case svm_parameter.RBF:
return Math.exp(-gamma*(x_square[i]+x_square[j]-2*dot(x[i],x[j])));
case svm_parameter.SIGMOID:
return Math.tanh(gamma*dot(x[i],x[j])+coef0);
case svm_parameter.PRECOMPUTED:
return x[i][(int)(x[j][0].value)].value;
default:
return 0; // java
}
}
Kernel(int l, svm_node[][] x_, svm_parameter param)
{
this.kernel_type = param.kernel_type;
this.degree = param.degree;
this.gamma = param.gamma;
this.coef0 = param.coef0;
x = (svm_node[][])x_.clone();
if(kernel_type == svm_parameter.RBF)
{
x_square = new double[l];
for(int i=0;i<l;i++)
x_square[i] = dot(x[i],x[i]);
}
else x_square = null;
}
static double dot(svm_node[] x, svm_node[] y)
{
double sum = 0;
int xlen = x.length;
int ylen = y.length;
int i = 0;
int j = 0;
while(i < xlen && j < ylen)
{
if(x[i].index == y[j].index)
sum += x[i++].value * y[j++].value;
else
{
if(x[i].index > y[j].index)
++j;
else
++i;
}
}
return sum;
}
static double k_function(svm_node[] x, svm_node[] y,
svm_parameter param)
{
switch(param.kernel_type)
{
case svm_parameter.LINEAR:
return dot(x,y);
case svm_parameter.POLY:
return powi(param.gamma*dot(x,y)+param.coef0,param.degree);
case svm_parameter.RBF:
{
double sum = 0;
int xlen = x.length;
int ylen = y.length;
int i = 0;
int j = 0;
while(i < xlen && j < ylen)
{
if(x[i].index == y[j].index)
{
double d = x[i++].value - y[j++].value;
sum += d*d;
}
else if(x[i].index > y[j].index)
{
sum += y[j].value * y[j].value;
++j;
}
else
{
sum += x[i].value * x[i].value;
++i;
}
}
while(i < xlen)
{
sum += x[i].value * x[i].value;
++i;
}
while(j < ylen)
{
sum += y[j].value * y[j].value;
++j;
}
return Math.exp(-param.gamma*sum);
}
case svm_parameter.SIGMOID:
return Math.tanh(param.gamma*dot(x,y)+param.coef0);
case svm_parameter.PRECOMPUTED:
return x[(int)(y[0].value)].value;
default:
return 0; // java
}
}
}
// An SMO algorithm in Fan et al., JMLR 6(2005), p. 1889--1918
// Solves:
//
// min 0.5(\alpha^T Q \alpha) + p^T \alpha
//
// y^T \alpha = \delta
// y_i = +1 or -1
// 0 <= alpha_i <= Cp for y_i = 1
// 0 <= alpha_i <= Cn for y_i = -1
//
// Given:
//
// Q, p, y, Cp, Cn, and an initial feasible point \alpha
// l is the size of vectors and matrices
// eps is the stopping tolerance
//
// solution will be put in \alpha, objective value will be put in obj
//
class Solver {
int active_size;
byte[] y;
double[] G; // gradient of objective function
static final byte LOWER_BOUND = 0;
static final byte UPPER_BOUND = 1;
static final byte FREE = 2;
byte[] alpha_status; // LOWER_BOUND, UPPER_BOUND, FREE
double[] alpha;
QMatrix Q;
double[] QD;
double eps;
double Cp,Cn;
double[] p;
int[] active_set;
double[] G_bar; // gradient, if we treat free variables as 0
int l;
boolean unshrink; // XXX
static final double INF = java.lang.Double.POSITIVE_INFINITY;
double get_C(int i)
{
return (y[i] > 0)? Cp : Cn;
}
void update_alpha_status(int i)
{
if(alpha[i] >= get_C(i))
alpha_status[i] = UPPER_BOUND;
else if(alpha[i] <= 0)
alpha_status[i] = LOWER_BOUND;
else alpha_status[i] = FREE;
}
boolean is_upper_bound(int i) { return alpha_status[i] == UPPER_BOUND; }
boolean is_lower_bound(int i) { return alpha_status[i] == LOWER_BOUND; }
boolean is_free(int i) { return alpha_status[i] == FREE; }
// java: information about solution except alpha,
// because we cannot return multiple values otherwise...
static class SolutionInfo {
double obj;
double rho;
double upper_bound_p;
double upper_bound_n;
double r; // for Solver_NU
}
void swap_index(int i, int j)
{
Q.swap_index(i,j);
do {byte _=y[i]; y[i]=y[j]; y[j]=_;} while(false);
do {double _=G[i]; G[i]=G[j]; G[j]=_;} while(false);
do {byte _=alpha_status[i]; alpha_status[i]=alpha_status[j]; alpha_status[j]=_;} while(false);
do {double _=alpha[i]; alpha
libsvm-3.12



《libsvm-3.12:跨平台的高效支持向量机库》 libsvm,全称为“Library for Support Vector Machines”,是由台湾大学的Chih-Chung Chang和Chih-Jen Lin开发的一个开源软件库,专门用于实现支持向量机(Support Vector Machine,简称SVM)算法。该库在版本3.12中提供了对多种编程语言的支持,包括MATLAB、C++、Java以及Python等,使其成为了一个极具灵活性和广泛适用性的机器学习工具。 支持向量机是一种监督学习模型,广泛应用于分类和回归问题。它的核心思想是找到一个超平面,使得数据集中的实例点能够被最优地划分。这个超平面是通过最大化边界距离(也称为间隔)来确定的,从而使得模型具有较好的泛化能力。libsvm不仅实现了基本的SVM算法,还包含了多项优化和扩展,如核函数的选择、多分类策略、在线学习等。 在libsvm-3.12中,对于MATLAB用户,可以方便地利用提供的接口进行模型训练和预测,其简洁的API设计使得调用和使用十分便捷。对于C++开发者,libsvm提供了静态库和动态库,可以直接在C++代码中嵌入SVM模块,实现高效的数据处理和模型构建。Java版本的libsvm则使得SVM可以在各种Java环境中运行,包括Web应用和大数据分析平台。Python版本的libsvm,结合了Python的易用性和SVM的强大功能,是数据科学领域常用的选择。 在libsvm-3.12的压缩包中,通常会包含以下内容: 1. 源代码:包含了各个编程语言的接口实现,以及SVM的核心算法代码。 2. 示例:提供了一些示例数据集和脚本,帮助用户快速理解和使用libsvm。 3. 文档:详细的用户手册和API文档,解释了如何安装、配置和使用libsvm。 4. 库文件:编译好的库文件,供不同平台和语言环境直接使用。 5. 测试:包含了测试用例,用于验证库的正确性。 libsvm-3.12的多语言支持使得它能够无缝集成到各种项目中,无论是学术研究还是工业应用。同时,其高效的算法实现和丰富的功能,使其在机器学习领域具有极高的声誉和广泛的影响力。无论是初学者还是经验丰富的数据科学家,libsvm都是实现和支持向量机算法的理想选择。通过深入理解并熟练掌握libsvm,我们可以更好地利用SVM解决实际问题,提升模型的预测能力和泛化性能。





































































































- 1

- 粉丝: 2634
- 资源: 38
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- LabVIEW与西门子S7-1200通信:无需编写通信程序,上位机直接读写DB块,简单易用的LabVIEW S7协议,LabView与西门子S7 PLC直接读写DB块:简易通信协议实现上位机通信控制
- 1st Prize Project of National Cloud Computing Appl.zip
- Smart Home Energy Management System (2015微软创新杯陕西大.zip
- 全国大学生服创大赛前台——This project was developed for the pu.zip
- 毕马威2023年香港银行业展望报告28页.pdf
- 大数据企业实训项目:基于SpringMVC+Spring+HBase+Maven搭建的Hadoop分.zip
- 本C++和Unity是智能手术室部分源码,获得湖南省创新创业大赛冠军,全国创新创业生物医药类优胜奖。.zip
- 能源经济创意大赛.zip
- 北京邮电大学大创项目.zip
- 大创意学期项目.zip
- 原创的关于全国大学生数学建模竞赛的文章和代码.zip
- 通过python对多种模型的指标dice、iou、pa等绘制柱状图
- 标准化智慧养老运营平台项目.pdf
- 波士顿咨询银行业生成式AI应用报告202319页.pdf
- 财富管理系统.pdf
- 不停车收费系统(ETC)系统架构及未来生态模式.pdf



- 1
- 2
- 3
- 4
- 5
- 6
前往页