package org.jeecg.sy.scala

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions._
import org.apache.spark.ml.feature.VectorAssembler
import org.apache.spark.ml.regression.LinearRegression
import org.apache.spark.ml.Pipeline
import org.apache.spark.sql.Encoders
import org.jeecg.sy.java.weather_1

import java.util

object WeatherPrediction {

  def forecast(): Unit = {
    val list = new util.ArrayList[Weather1]()
    val spark = SparkSession.builder()
      .appName("Weather Temperature Prediction")
      .master("local[*]") // 适用于本地开发和测试
      .getOrCreate()

    import spark.implicits._

    // 将List转换为DataFrame
    val weatherData = null

    // 定义特征向量
    val assembler = new VectorAssembler()
      .setInputCols(Array("lowTemperature", "windSpeed")) // 根据你的数据选择合适的特征
      .setOutputCol("features")

    // 创建线性回归模型
    val lr = new LinearRegression()
      .setLabelCol("highTemperature")
      .setFeaturesCol("features")
      .setMaxIter(10)
      .setRegParam(0.3)
      .setElasticNetParam(0.8)

    // 创建Pipeline
    val pipeline = new Pipeline()
      .setStages(Array(assembler, lr))

    // 训练模型
    val model = pipeline.fit(weatherData)

    // 使用模型进行预测
    val predictions = model.transform(weatherData)

    // 显示预测结果
    predictions.select("data", "highTemperature", "features", "prediction").show()

    // 停止SparkSession
    spark.stop()
  }
}

