数字营销实训算法第一轮修改
parent
c67e2436b9
commit
4a9095d9b0
@ -0,0 +1,23 @@
|
||||
package com.sztzjy.marketing.entity.dto;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author tz
|
||||
* @date 2024/8/17 14:16
|
||||
*/
|
||||
@Data
|
||||
public class AprioriDTO {
|
||||
@ApiModelProperty("最小支持度阀值")
|
||||
private double support;
|
||||
@ApiModelProperty("最小置信度")
|
||||
private double confidence;
|
||||
@ApiModelProperty("用户ID")
|
||||
private String userId;
|
||||
@ApiModelProperty("数据集")
|
||||
private List<Map<String, Object>> deduplicatedDataList;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.sztzjy.marketing.entity.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author tz
|
||||
* @date 2024/8/17 16:30
|
||||
*/
|
||||
@Data
|
||||
public class RAnalysisDTO {
|
||||
private double intercept;
|
||||
private double slopes;
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.sztzjy.marketing.util.algorithm;
|
||||
|
||||
public class Data {
|
||||
public double[] features;
|
||||
public double label;
|
||||
|
||||
public Data(double[] features, double label) {
|
||||
this.features = features;
|
||||
this.label = label;
|
||||
}
|
||||
}
|
@ -1,71 +1,121 @@
|
||||
package com.sztzjy.marketing.util.algorithm;
|
||||
|
||||
import java.util.Arrays;
|
||||
import com.sztzjy.marketing.util.BigDecimalUtils;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
public class LogisticRegression {
|
||||
private double[] weights;
|
||||
private double bias;
|
||||
private double theta0; // 截距项
|
||||
private double theta1; // 斜率
|
||||
private double learningRate; // 学习率
|
||||
|
||||
private int currentEpoch; // 声明currentEpoch变量
|
||||
|
||||
public LogisticRegression(int numFeatures) {
|
||||
weights = new double[numFeatures];
|
||||
bias = 0;
|
||||
public LogisticRegression(double learningRate) {
|
||||
this.learningRate = learningRate;
|
||||
}
|
||||
|
||||
public void fit(double[][] X, double[] y, double learningRate, int numIterations) {
|
||||
int numSamples = X.length;
|
||||
int numFeatures = X[0].length;
|
||||
|
||||
for (int iter = 0; iter < numIterations; iter++) {
|
||||
double[] gradientWeights = new double[numFeatures];
|
||||
double gradientBias = 0;
|
||||
|
||||
for (int i = 0; i < numSamples; i++) {
|
||||
double dotProduct = dotProduct(X[i], weights) + bias;
|
||||
double prediction = sigmoid(dotProduct);
|
||||
double error = y[i] - prediction;
|
||||
|
||||
for (int j = 0; j < numFeatures; j++) {
|
||||
gradientWeights[j] += error * X[i][j];
|
||||
}
|
||||
gradientBias += error;
|
||||
}
|
||||
public LogisticRegression(double learningRate, double initialTheta0, double initialTheta1) {
|
||||
this.learningRate = learningRate;
|
||||
this.theta0 = initialTheta0;
|
||||
this.theta1 = initialTheta1;
|
||||
this.currentEpoch = 0; // 在构造函数中初始化currentEpoch
|
||||
}
|
||||
|
||||
for (int j = 0; j < numFeatures; j++) {
|
||||
weights[j] += learningRate * gradientWeights[j] / numSamples;
|
||||
}
|
||||
bias += learningRate * gradientBias / numSamples;
|
||||
}
|
||||
// 添加一个方法来获取截距项
|
||||
public double getIntercept() {
|
||||
return theta0;
|
||||
}
|
||||
|
||||
public double predict(double[] x) {
|
||||
double dotProduct = dotProduct(x, weights) + bias;
|
||||
return sigmoid(dotProduct);
|
||||
// 添加一个方法来获取第一个特征的系数
|
||||
public double getCoefficient() {
|
||||
return theta1;
|
||||
}
|
||||
|
||||
private double sigmoid(double x) {
|
||||
return 1 / (1 + Math.exp(-x));
|
||||
// Sigmoid函数
|
||||
private double sigmoid(double z) {
|
||||
return 1 / (1 + Math.exp(-z));
|
||||
}
|
||||
|
||||
private double dotProduct(double[] x, double[] y) {
|
||||
double sum = 0;
|
||||
for (int i = 0; i < x.length; i++) {
|
||||
sum += x[i] * y[i];
|
||||
// 预测函数
|
||||
public double predict(double x) {
|
||||
BigDecimalUtils bigDecimalUtils=new BigDecimalUtils();
|
||||
|
||||
Double mul = bigDecimalUtils.mul(this.theta0, x);
|
||||
|
||||
return bigDecimalUtils.add(this.theta1, mul);
|
||||
|
||||
}
|
||||
|
||||
private double dotProduct(double[] a, double[] b) {
|
||||
double result = 0.0;
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
result += a[i] * b[i];
|
||||
}
|
||||
return sum;
|
||||
return result;
|
||||
}
|
||||
|
||||
// 梯度下降更新参数
|
||||
public void updateParameters(double x, double y) {
|
||||
double h = predict(x);
|
||||
double error = y - h;
|
||||
theta1 += learningRate * error * h * (1 - h) * x;
|
||||
theta0 += learningRate * error * h * (1 - h);
|
||||
}
|
||||
private double exponentialDecayLearningRate(double currentEpoch, int totalEpochs) {
|
||||
return learningRate * Math.exp(-(currentEpoch / totalEpochs));
|
||||
}
|
||||
|
||||
// 训练模型
|
||||
public void train(double[][] data, double[] labels, int epochs) {
|
||||
double learningRateCurrentEpoch = exponentialDecayLearningRate(currentEpoch, epochs);
|
||||
for (int epoch = 0; epoch < epochs; epoch++) {
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
double x = data[i][0];
|
||||
double y = labels[i];
|
||||
double h = sigmoid(theta0 + theta1 * x);
|
||||
double error = y - h;
|
||||
theta1 += learningRateCurrentEpoch * error * h * (1 - h) * x;
|
||||
theta0 += learningRateCurrentEpoch * error * h * (1 - h);
|
||||
}
|
||||
// 可以添加打印语句查看训练情况
|
||||
// System.out.println("Epoch: " + (epoch + 1) + ", Loss: " + calculateLoss(data, labels));
|
||||
}
|
||||
}
|
||||
public double calculateLoss(double[][] data, double[] labels) {
|
||||
double loss = 0;
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
double x = data[i][0];
|
||||
double y = labels[i];
|
||||
double h = sigmoid(theta0 + theta1 * x);
|
||||
loss += -y * Math.log(h) - (1 - y) * Math.log(1 - h);
|
||||
}
|
||||
return loss / data.length;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
double[][] X = {{1, 1}, {1, 2}, {2, 1}, {2, 3}};
|
||||
double[] y = {0, 0, 0, 1};
|
||||
LogisticRegression model = new LogisticRegression(0.1); // 创建一个逻辑回归模型实例
|
||||
|
||||
double[][] data = {{1}, {2}, {3}, {4}, {5}};
|
||||
double[] labels = {0, 0, 1, 1, 1}; // 假设这是一个二分类问题
|
||||
|
||||
// double[][] data = {{1, 19}, {1, 21}, {2, 20}, {2, 23}, {2, 31}};
|
||||
// double[] labels = {15, 15, 16, 16, 17};
|
||||
|
||||
model.train(data, labels, 1000); // 训练模型
|
||||
|
||||
LogisticRegression model = new LogisticRegression(2);
|
||||
model.fit(X, y, 0.01, 10000);
|
||||
DecimalFormat df = new DecimalFormat("#.0");
|
||||
|
||||
System.out.println("Weights: " + Arrays.toString(model.weights));
|
||||
System.out.println("Bias: " + model.bias);
|
||||
// 获取并打印截距项和系数
|
||||
double intercept = model.getIntercept();
|
||||
double coefficient = model.getCoefficient();
|
||||
System.out.println("常数项值: " + df.format(intercept));
|
||||
System.out.println("系数值 " + df.format(coefficient));
|
||||
|
||||
double[] newSample = {1, 2};
|
||||
double prediction = model.predict(newSample);
|
||||
System.out.println("Prediction for " + Arrays.toString(newSample) + ": " + prediction);
|
||||
double prediction = model.predict(2);
|
||||
System.out.println("x=3.5的预测: " + df.format(prediction));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue