本文整理汇总了Java中org.apache.spark.mllib.linalg.Vector.toArray方法的典型用法代码示例。如果您正苦于以下问题:Java Vector.toArray方法的具体用法?Java Vector.toArray怎么用?Java Vector.toArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.spark.mllib.linalg.Vector
的用法示例。
在下文中一共展示了Vector.toArray方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: hellingerDistance
import org.apache.spark.mllib.linalg.Vector; //导入方法依赖的package包/类
/**
* Calculate similarity (Hellinger Distance) between vectors
*
* @param vecA initial vector from which to calculate a similarity
* @param vecB second vector involved in similarity calculation
* @return similarity between two vectors
*/
public static double hellingerDistance(Vector vecA, Vector vecB) {
double[] arrA = vecA.toArray();
double[] arrB = vecB.toArray();
double sim = 0.0;
int arrsize = arrA.length;
for (int i = 0; i < arrsize; i++) {
double a = arrA[i];
double b = arrB[i];
double sqrtDiff = Math.sqrt(a) - Math.sqrt(b);
sim += sqrtDiff * sqrtDiff;
}
sim = sim / Math.sqrt(2);
return sim;
}
示例2: pearsonDistance
import org.apache.spark.mllib.linalg.Vector; //导入方法依赖的package包/类
/**
* Calculate similarity (Pearson Distance) between vectors
*
* @param vecA initial vector from which to calculate a similarity
* @param vecB second vector involved in similarity calculation
* @return similarity between two vectors
*/
public static double pearsonDistance(Vector vecA, Vector vecB) {
double[] arrA = vecA.toArray();
double[] arrB = vecB.toArray();
int viewA = 0;
int viewB = 0;
int viewAB = 0;
int arrsize = arrA.length;
for (int i = 0; i < arrsize; i++) {
if (arrA[i] > 0) {
viewA++;
}
if (arrB[i] > 0) {
viewB++;
}
if (arrB[i] > 0 && arrA[i] > 0) {
viewAB++;
}
}
return viewAB / (Math.sqrt(viewA) * Math.sqrt(viewB));
}
示例3: getPredictedValue
import org.apache.spark.mllib.linalg.Vector; //导入方法依赖的package包/类
/**
* This method is to predict the label of a given data point
*/
private String getPredictedValue(Vector dataPointVector, int clusterIndex, double clusterBoundary) {
String prediction;
EuclideanDistance euclideanDistance = new EuclideanDistance();
Vector[] clusterCenters = kMeansModel.clusterCenters();
double[] dataPoint = dataPointVector.toArray();
double[] clusterCenter = clusterCenters[clusterIndex].toArray();
double distance = euclideanDistance.compute(clusterCenter, dataPoint);
if (distance > clusterBoundary) {
prediction = anomalyLabel;
} else {
prediction = normalLabel;
}
return prediction;
}