机器学习-RBF高斯核函数处理

SVM高斯核函数-RBF优化 
重要了解数学的部分: 
协方差矩阵,高斯核函数公式。 
个人建议具体的求法还是看下面的核心代码吧,更好理解,反正就我个人而言,烦躁的公式,还不如一段代码来的实际。本来想用Java的一个叫jblas的矩阵包,但是想了想,还是自己动手写一下吧。加深一下自己理解。实现的语言用的是java孪生兄弟Scala。我想应该不难懂。矩阵变换用二位数组将就。 
以下代码建议用scala命令行调试

核心代码

def TransposedMatrix(a:Array[Array[Double]]):Array[Array[Double]]={//返回转置矩阵
val length=a.length
val width=a(0).length
var TransposedM =Array.ofDim[Double](width,length)
for(i <- 0 to length-1)
for(j <- 0 to width-1) {
TransposedM(j)(i) = a(i)(j)
}
TransposedM
} def R1(i:Int,j:Int,M:Array[Array[Double]]):Double={//R(i,j)=(第i列-第j列)*[(第i列-第j列)转置]
var sum:Double=0
for(x <- 0 to M.length-1)
sum+=Math.pow((M(x)(i)-M(x)(j)),2)
sum } def RowAverage(a:Array[Array[Double]]):Array[Double]={//返回列的均值,返回一个列矩阵
val length=a.length
val width=a(0).length
var b=new Array[Double](width)
for(i <- 0 to width-1)
for(j <- 0 to length-1)
b(i) += a(j)(i) for(i <- 0 to width-1)
b(i)=b(i)/length
b
} def sumOfRow(a:Array[Array[Double]]):Array[Double]={//返回矩阵列的和,返回一个列矩阵
val length=a.length
val width=a(0).length
var b=new Array[Double](width)
for(i <- 0 to width-1)
for(j <- 0 to length-1)
b(i) += a(j)(i) for(i <- 0 to width-1)
b(i)=b(i)
b
} def sum(i:Int,j:Int,a:Array[Array[Double]]):Double={//i列乘j列的转置
var result:Double=0
for(x<- 0 to a.length-1)
result +=(a(x)(i)*a(x)(j))
result
} def cov(a:Array[Array[Double]]):Array[Array[Double]]={//将特征矩阵作为参数,返回协方差矩阵
val m1=TransposedMatrix(a)
val m2=RowAverage(m1)
val m3=datasort(m1,m2)//将矩阵中心化
val width=m3(0).length
var b =Array.ofDim[Double](width,width)
for(i <- 0 to width-1)
for(j <- 0 to width-1)
b(i)(j)=sum(i,j,m3)
b
} def datasort(a:Array[Array[Double]],b:Array[Double]):Array[Array[Double]]={//矩阵中心化,将每列减去列的均值
for(i <- 0 to a(0).length-1)
for(j <- 0 to a.length-1)
a(j)(i) -= b(i)
a
} def gaussMatrix(a:Array[Array[Double]],delta:Array[Double]):Array[Array[Double]]={//a为特征矩阵,delta为协方差矩阵列之和,返回高斯核函数矩阵
val b=TransposedMatrix(a)
val length=b(0).length
var R =Array.ofDim[Double](length,length)
for(i <- 0 to length-1)
for(j <- 0 to length-1)
R(i)(j)=Math.exp(-R1(i,j,b)/delta(j))
R
} val test=Array(Array(2.0, 8.0), Array(3.0, 6.0), Array(9.0, 2.0))
val test2=cov(test)
val rowOfsum=sumOfRow(res65)
gaussMatrix(test,rowOfsum)

欢迎各位看官大爷批评指教。

感谢下面百度知道回复的朋友,实现的代码段很多得到他的启示。 
http://zhidao.baidu.com/link?url=-u5LznclWQ0LbvEx3DB8sofohyP7nJCWws78TsWBNaDR15rDn-7ENoRealHRIM8W8ycioegl_NGAFzQJ33PbZ90ACQQ7eLf8HgR7DAQUJjS

05-20 09:12