Add Regression Prediction lab

This commit is contained in:
2025-11-11 16:56:46 +01:00
parent 378228b208
commit dece3dba69
6 changed files with 131 additions and 1 deletions

View File

@@ -0,0 +1,62 @@
package com.example.regression.prediction;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.apache.commons.math3.stat.regression.SimpleRegression;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvException;
import org.springframework.core.io.ClassPathResource;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
@SpringBootApplication
public class CustomerPurchasePredictor {
public static void main(String[] args) {
try {
// 1. Load the dataset
List<String[]> data = loadCSV("/customer_purchases.csv");
// 2. Prepare the regression model
SimpleRegression regression = new SimpleRegression();
// Skip header row and add data points
for (int i = 1; i < data.size(); i++) {
String[] row = data.get(i);
double income = Double.parseDouble(row[2]); // Independent variable (X)
double purchaseAmount = Double.parseDouble(row[3]); // Dependent variable (Y)
regression.addData(income, purchaseAmount);
}
// 3. Print model statistics
System.out.println("=== Model Summary ===");
System.out.printf("R-squared: %.4f\n", regression.getRSquare());
System.out.printf("Intercept: %.2f\n", regression.getIntercept());
System.out.printf("Slope: %.4f\n", regression.getSlope());
System.out.printf("Standard Error: %.4f\n\n", regression.getRegressionSumSquares());
// 4. Make predictions for new customers
System.out.println("=== Predictions ===");
predictPurchase(regression, 40000); // $40,000 income
predictPurchase(regression, 55000); // $55,000 income
predictPurchase(regression, 80000); // $80,000 income
} catch (IOException | CsvException e) {
e.printStackTrace();
}
}
private static List<String[]> loadCSV(String filePath) throws IOException, CsvException {
try (CSVReader reader = new CSVReader(new InputStreamReader(new ClassPathResource(filePath).getInputStream()))) {
return reader.readAll();
}
}
private static void predictPurchase(SimpleRegression regression, double income) {
double predictedAmount = regression.predict(income);
System.out.printf("Predicted purchase for $%,.2f income: $%,.2f\n",
income, predictedAmount);
}
}

View File

@@ -0,0 +1 @@
spring.application.name=prediction

View File

@@ -0,0 +1,21 @@
customer_id,age,income,purchase_amount
1,25,30000,150
2,30,35000,180
3,35,40000,210
4,40,45000,250
5,45,50000,280
6,50,55000,320
7,55,60000,350
8,60,65000,380
9,65,70000,420
10,70,75000,450
11,28,32000,160
12,32,38000,190
13,38,42000,230
14,42,48000,260
15,48,52000,290
16,52,58000,330
17,58,62000,360
18,62,68000,390
19,68,72000,430
20,72,78000,460
1 customer_id age income purchase_amount
2 1 25 30000 150
3 2 30 35000 180
4 3 35 40000 210
5 4 40 45000 250
6 5 45 50000 280
7 6 50 55000 320
8 7 55 60000 350
9 8 60 65000 380
10 9 65 70000 420
11 10 70 75000 450
12 11 28 32000 160
13 12 32 38000 190
14 13 38 42000 230
15 14 42 48000 260
16 15 48 52000 290
17 16 52 58000 330
18 17 58 62000 360
19 18 62 68000 390
20 19 68 72000 430
21 20 72 78000 460

View File

@@ -0,0 +1,13 @@
package com.example.regression.prediction;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class PredictionApplicationTests {
@Test
void contextLoads() {
}
}