반응형
Linear Regression
설명
- Linear Regression 설명 상세
- apache commons-math3 라이브러리를 활용하여 간단한 Linear Regression을 구현하는 예제
- testSimpleRegression()는 인풋이 1차원인 경우에 대한 예제
- testMultipleLinearRegression()는 인풋이 다차원인 경우에 대한 예제
- 지정된 데이터로 학습 후 입력한 데이터에 대한 값을 예측하는 곳에서 참고할 수 있다.
예제
Dependency<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
RegressionTest
public class RegressionTest {
@Test
public void testSimpleRegression() {
// x : 1, y : 2
// x : 2, y : 3
// ...
double[][] data = {
{1, 2},
{2, 3},
{3, 4},
{4, 5},
};
SimpleRegression regression = new SimpleRegression(true);
regression.addData(data);
IntStream.range(0, data.length).forEach(i -> {
double actualY = data[i][1];
double predictY = regression.predict(data[i][0]);
Assertions.assertEquals(predictY, actualY, 0.001);
});
}
@Test
public void testMultipleLinearRegression() {
// x : [0, 0, 0, 0, 0], y : 11.0
// x : [2.0, 0, 0, 0, 0], y : 12.0
// ...
double[] y = {11.0, 12.0, 13.0, 14.0, 15.0, 16.0};
double[][] x = {
{0, 0, 0, 0, 0},
{2.0, 0, 0, 0, 0},
{0, 3.0, 0, 0, 0},
{0, 0, 4.0, 0, 0},
{0, 0, 0, 5.0, 0},
{0, 0, 0, 0, 6.0},
};
OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
regression.newSampleData(y, x);
double[] beta = regression.estimateRegressionParameters();
IntStream.range(0, y.length).forEach(i -> {
double actualY = y[i];
double predictY = predict(beta, x[i]);
Assertions.assertEquals(predictY, actualY, 0.001);
});
}
private double predict(double[] beta, double[] x) {
// intercept at beta[0]
double prediction = beta[0];
for (int i = 1; i < beta.length; i++) {
prediction += beta[i] * x[i - 1];
}
return prediction;
}
}
참고
- https://stackoverflow.com/questions/30859029/use-common-math-library-in-java
- https://github.com/mahmoudparsian/data-algorithms-book/blob/master/src/main/java/org/dataalgorithms/machinelearning/linear/OLS/OrdinaryLeastSquaresRegressionModel.java
- https://alvinalexander.com/java/jwarehouse/commons-math3-3.6.1/src/test/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegressionTest.java.shtml
Clustering
설명
- Clustering(군집)은 비지도 학습 방법으로 label이 알려져 있지 않을 때 각 요소들의 그룹을 예측하는 방법.
- Classification은 Clustering처럼 각 요소들의 그룹을 예측한다는 점이 동일하지만 labeling이 필요한 지도학습 방법이다.
- 아래 예제에서도 마찬가지로 apache commons-math3 라이브러리를 사용한다.
예제
Dependency<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
ClusteringTest
public class ClusteringTest {
@Test
public void testKMeansPlusPlusClustering() {
// K-Means++ 알고리즘에서 K는 몇 개의 그룹으로 집합을 나눌 것인지를 결정하는 값
// 아래는 2개의 그룹으로 집합을 나누는 설정
Clusterer<DoublePoint> clusterer = new KMeansPlusPlusClusterer<>(2);
List<DoublePoint> points = new ArrayList<>();
points.add(new DoublePoint(new double[]{1}));
points.add(new DoublePoint(new double[]{1.1}));
points.add(new DoublePoint(new double[]{1.2}));
points.add(new DoublePoint(new double[]{3.5}));
points.add(new DoublePoint(new double[]{3.6}));
points.add(new DoublePoint(new double[]{4}));
points.add(new DoublePoint(new double[]{4.2}));
List<? extends Cluster<DoublePoint>> clusters = clusterer.cluster(points);
clusters.sort(Comparator.comparingDouble(o -> o.getPoints().get(0).getPoint()[0]));
Assertions.assertEquals("[[1.0], [1.1], [1.2]]", clusters.get(0).getPoints().toString());
Assertions.assertEquals("[[3.5], [3.6], [4.0], [4.2]]", clusters.get(1).getPoints().toString());
}
}
참고
반응형
'Development > Java' 카테고리의 다른 글
[Java] Annotation Processor (0) | 2021.03.04 |
---|---|
[Java] Reflection (0) | 2021.03.03 |
[Java] Regular Expression (0) | 2020.12.28 |
[Java] Thread Dump (0) | 2020.12.28 |
[Java] Heap Dump (0) | 2020.12.28 |