date.iterator.count.util.CalculationUtil Maven / Gradle / Ivy
package date.iterator.count.util;
import date.iterator.count.isodata.Cluster;
import date.iterator.count.isodata.ISODataConstants;
import date.iterator.count.isodata.Point;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CalculationUtil {
public void calculateAllAverage(final List clusters, final List points) {
//计算所有距离的平均值,先留着
List allDistances = new ArrayList<>();
points.forEach(point -> allDistances.addAll(point.calculateCenterDistances(clusters)));
double allDistanceValue = 0;
for (Double eachDistance : allDistances) {
allDistanceValue += eachDistance;
}
double averageDistance = allDistanceValue / allDistances.size();
}
public static Map calculateTotalAverages(final List points) {
Map pointTotalValues = new HashMap<>();
for (Point eachPoint : points) {
for (String eachTitle : GlobalKeyCenter.INSTANCE.getKeys()) {
if (pointTotalValues.containsKey(eachTitle)) {
double newValue = pointTotalValues.get(eachTitle) + eachPoint.getValues().get(eachTitle);
pointTotalValues.put(eachTitle, newValue);
continue;
}
pointTotalValues.put(eachTitle, eachPoint.getValues().get(eachTitle));
}
}
return pointTotalValues;
}
public static Map calculateTitlesTotal(final List clusters) {
Map pointTotalValues = null;
for (Cluster eachCluster : clusters) {
if (pointTotalValues == null) {
pointTotalValues = eachCluster.getPointTotalValues();
continue;
}
for (String eachTitle : GlobalKeyCenter.INSTANCE.getKeys()) {
double newValue = pointTotalValues.get(eachTitle) + eachCluster.getPointTotalValues().get(eachTitle);
pointTotalValues.put(eachTitle, newValue);
}
}
return pointTotalValues;
}
/*
* 总标准差
* */
public static Map calculateStandardDeviation(final List points, final Map totalAverageValues) {
Map totalStandardErrors = new HashMap<>();
for (String eachTitle : GlobalKeyCenter.INSTANCE.getKeys()) {
double eachSquaredError = 0;
for (Point eachPoint : points) {
Double curretValue = eachPoint.getValues().get(eachTitle);
Double average = totalAverageValues.get(eachTitle);
eachSquaredError += Math.pow(Math.abs(curretValue - average), 2);
}
totalStandardErrors.put(eachTitle, Math.sqrt(eachSquaredError / points.size()));
}
return totalStandardErrors;
}
/*
* 两点间欧式距离
* */
public static double distanceEuclidean(final Point onePoint, final Point anotherPoint) {
double powDistance = 0;
for (String eachTitle : GlobalKeyCenter.INSTANCE.getKeys()) {
Double otherValue = anotherPoint.getValues().get(eachTitle);
Double curretValue = onePoint.getValues().get(eachTitle);
powDistance += Math.abs(Math.pow(otherValue - curretValue, 2));
}
return Math.sqrt(powDistance); //勾股定理求斜边
}
/*标准化欧式距离*/
public static double distanceStandardEuclidean(final Point onePoint, final Point anotherPoint,
final Map totalStandardErrors) {
double powDistance = 0;
for (String eachTitle : GlobalKeyCenter.INSTANCE.getKeys()) {
Double otherValue = anotherPoint.getValues().get(eachTitle);
Double curretValue = onePoint.getValues().get(eachTitle);
// 两点各自减去均值,于是均值被约掉了
double standardValue = (otherValue - curretValue) / totalStandardErrors.get(eachTitle);
powDistance += Math.abs(Math.pow(standardValue, 2));
}
return Math.sqrt(powDistance); //勾股定理求斜边
}
public static List testPoints(final int pointSize) {
List result = new ArrayList<>();
for (int i = 0; i < pointSize; i++) {
Point point = new Point();
for (String eachTitle : ISODataConstants.Data_Value_Title) {
GlobalKeyCenter.INSTANCE.addKey(eachTitle);
point.getValues().put(eachTitle, Math.random()*100);
}
result.add(point);
}
return result;
}
}