Browse Source

新增:归一化算法批量计算

master
wxz 5 years ago
parent
commit
9cf5ada85b
  1. 2
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/ScoreCalculator.java
  2. 13
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/ScoreConstants.java
  3. 35
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/batch/BatchScoreCalculator.java
  4. 39
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/batch/IndexInputVO.java
  5. 23
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/batch/IndexOutputVO.java
  6. 51
      epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/normalizing/DemoScoreCal.java

2
epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/ScoreCalculator.java

@ -79,7 +79,7 @@ public abstract class ScoreCalculator<T> {
if (correlation == Correlation.POSITIVE) {
// 正相关
BigDecimal x = coefficient.multiply(convertValue2BigDecimal(sourceValue).subtract(minValue));
BigDecimal score = minScore.add(x, MathContext.DECIMAL32);
BigDecimal score = minScore.add(x);
return score;
} else if (correlation == Correlation.NEGATIVE) {
// 负相关

13
epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/ScoreConstants.java

@ -0,0 +1,13 @@
package com.epmet.support.normalizing;
import java.math.BigDecimal;
/**
* 分值常量
*/
public class ScoreConstants {
public static final BigDecimal MIN_SCORE = new BigDecimal(0);
public static final BigDecimal MAX_SCORE = new BigDecimal(100);
}

35
epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/batch/BatchScoreCalculator.java

@ -0,0 +1,35 @@
package com.epmet.support.normalizing.batch;
import com.epmet.support.normalizing.ScoreCalculator;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
public class BatchScoreCalculator {
/**
* 每个指标都是一条数据每一条数据中包含该指标的一组样本
*/
private List<IndexInputVO> indexVOS;
/**
* 执行计算
* @return 每一条都是一个指标的所有样本对应的得分值
*/
public List<IndexOutputVO> exec(List<IndexInputVO> indexVOS) {
this.indexVOS = indexVOS;
return indexVOS.stream().map(i -> {
String indexId = i.getIndexId();
List indexValues = i.getIndexValues();
BigDecimal weight = i.getWeight();
ScoreCalculator scoreCalculator = i.getScoreCalculator();
List scores4OneIndex = scoreCalculator.normalize(indexValues, weight);
return new IndexOutputVO(indexId, scores4OneIndex);
}).collect(Collectors.toList());
}
}

39
epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/batch/IndexInputVO.java

@ -0,0 +1,39 @@
package com.epmet.support.normalizing.batch;
import com.epmet.support.normalizing.ScoreCalculator;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class IndexInputVO<T> {
/**
* 指标标记由使用者传入用以标记该条指标的独有特性一般用id或者code
* 非必填
*/
private String indexId;
/**
* 指标的样本值
*/
private List<T> indexValues;
/**
* 权重
*/
private BigDecimal weight;
private ScoreCalculator<T> scoreCalculator;
public IndexInputVO(List<T> indexValues, BigDecimal weight, ScoreCalculator<T> scoreCalculator) {
this.indexValues = indexValues;
this.weight = weight;
this.scoreCalculator = scoreCalculator;
}
}

23
epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/batch/IndexOutputVO.java

@ -0,0 +1,23 @@
package com.epmet.support.normalizing.batch;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.List;
@Data
@AllArgsConstructor
public class IndexOutputVO<T> {
/**
* 指标标记由使用者传入用以标记该条指标的独有特性一般用id或者code
* 非必填
*/
private String indexId;
/**
* 指标的样本值
*/
private List<BigDecimal> indexScores;
}

51
epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/normalizing/DemoScoreCal.java

@ -1,26 +1,29 @@
package com.epmet.stats.test.normalizing;
import com.epmet.support.normalizing.Correlation;
import com.epmet.support.normalizing.DoubleScoreCalculator;
import com.epmet.support.normalizing.IntegerScoreCalculator;
import com.epmet.support.normalizing.ScoreCalculator;
import com.epmet.support.normalizing.*;
import com.epmet.support.normalizing.batch.BatchScoreCalculator;
import com.epmet.support.normalizing.batch.IndexInputVO;
import com.epmet.support.normalizing.batch.IndexOutputVO;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
public class DemoScoreCal {
public static void main(String[] args) {
//demoInteger();
demoIntegerPartical();
//demoIntegerPartical();
//demoDouble();
}
/**
* 整数类型,完整源数据列表的归一算法
*/
public static void demoInteger() {
Integer[] iArray = {4,8,1,3,2};
@Test
public void demoInteger() {
Integer[] iArray = {4, 8, 1, 3, 2};
BigDecimal minScore = new BigDecimal(5);
BigDecimal maxScore = new BigDecimal(10);
@ -32,12 +35,13 @@ public class DemoScoreCal {
/**
* 整数类型,基于边界值的部分列表的的归一算法
*/
public static void demoIntegerPartical() {
Integer[] iArray = {4,1,2};
@Test
public void demoIntegerPartical() {
Integer[] iArray = {4, 1, 8};
BigDecimal minScore = new BigDecimal(5);
BigDecimal maxScore = new BigDecimal(10);
ScoreCalculator sc = new IntegerScoreCalculator(1, 8 ,minScore, maxScore, Correlation.NEGATIVE);
ScoreCalculator sc = new IntegerScoreCalculator(1, 8, minScore, maxScore, Correlation.NEGATIVE);
BigDecimal[] scores = sc.normalize(iArray);// 此处也可以直接使用list参数的重载方法,计算阶段没有任何区别,区别在于new IntegerScoreCalculator()阶段
Arrays.stream(scores).forEach(s -> System.out.println(s));
}
@ -46,7 +50,7 @@ public class DemoScoreCal {
* double类型归一算法
*/
public static void demoDouble() {
Double[] iArray = {1.9,8.9,1.0,3.0,2.0};
Double[] iArray = {1.9, 8.9, 1.0, 3.0, 2.0};
BigDecimal minScore = new BigDecimal(5);
BigDecimal maxScore = new BigDecimal(10);
@ -61,6 +65,31 @@ public class DemoScoreCal {
System.out.println(normalize2);
}
/**
* 批量计算demo
*/
@Test
public void testBatchCalculate() throws InterruptedException {
//{4,8,1,3,2}
// 每个指标需要单独的分支计算器,因为每个指标的最大最小值是不同的
ScoreCalculator<Integer> sc1 = new IntegerScoreCalculator(1, 8, ScoreConstants.MIN_SCORE, ScoreConstants.MAX_SCORE, Correlation.NEGATIVE);
ScoreCalculator<Integer> sc2 = new IntegerScoreCalculator(1, 8, ScoreConstants.MIN_SCORE, ScoreConstants.MAX_SCORE, Correlation.NEGATIVE);
// 每个指标的信息,包括样本列表,权重,指标标记
IndexInputVO<Integer> index1VO = new IndexInputVO<>("aaa", Arrays.asList(4, 1, 8), new BigDecimal(1), sc1);
IndexInputVO<Integer> index2VO = new IndexInputVO<>("bbb", Arrays.asList(1, 8, 3), new BigDecimal(1), sc2);
List<IndexInputVO> indexInputVOS = Arrays.asList(index1VO, index2VO);
BatchScoreCalculator batchScoreCalculator = new BatchScoreCalculator();
List<IndexOutputVO> result = batchScoreCalculator.exec(indexInputVOS);
System.err.println("--------------------------------");
result.stream().forEach(r -> System.out.println(r));
Thread.sleep(10);
System.err.println("--------------------------------");
}
}

Loading…
Cancel
Save