java plus方法_Java Vector.plus方法代码示例

import gov.sandia.cognition.math.matrix.Vector; //导入方法依赖的package包/类

public ArrayList initializeClusters(

int numClusters,

Collection extends Vector> elements)

{

if (numClusters < 0)

{

// Error: Bad number of clusters.

throw new IllegalArgumentException(

"The number of clusters cannot be negative.");

}

else if (elements == null)

{

// Error: Bad elements.

throw new NullPointerException("The elements cannot be null.");

}

else if (numClusters == 0 || elements.size() == 0)

{

// No clusters to create.

return new ArrayList();

}

ArrayList elementsArray =

new ArrayList(elements);

// Initialize the cluster objects.

ArrayList clusterList =

new ArrayList();

for (int k = 0; k < numClusters; k++)

{

// keep selecting random point until and ensure that the

// means aren't on top of eachother

Vector mean = null;

double minDiff = 0.0;

while (minDiff <= 0.0)

{

// select a new data point at random and place the cluster

// on top of it

int index = this.random.nextInt( elements.size() );

// add some noise to the point

Vector data = elementsArray.get(index);

Vector randomNoise = VectorFactory.getDefault().createUniformRandom(

data.getDimensionality(),

-this.getRandomRange(), this.getRandomRange(), this.random );

mean = data.plus(randomNoise);

minDiff = Double.POSITIVE_INFINITY;

for (int i = 0; i < k; i++)

{

Vector otherMean =

clusterList.get(i).getGaussian().getMean();

double diff = mean.euclideanDistance(otherMean);

if (minDiff > diff)

{

minDiff = diff;

}

}

}

// create a diagonal covariance matrix with "defaultCovariance"

// on the diagonal, and zeros elsewhere

int M = mean.getDimensionality();

Matrix covariance = MatrixFactory.getDefault().createIdentity(M, M).scale(this.getDefaultCovariance());

MultivariateGaussian.PDF gaussian =

new MultivariateGaussian.PDF(mean, covariance);

GaussianCluster cluster = new GaussianCluster(null, gaussian);

clusterList.add(cluster);

}

return clusterList;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值