头歌平台Spark分类分析小节测试(8.2小节测试)

打印 上一主题 下一主题

主题 846|帖子 846|积分 2538

第1关:第一题

打开右侧代码文件窗口,在 BeginEnd 地区增补代码,完成使命。
读取文件 /data/bigfiles/iris.txt 中的内容,使用二项逻辑斯蒂回归举行二分类分析,过滤 Iris-setosa 类。通过 randomSplit 方法将其中的 70% 数据作为练习集,剩下的作为测试集举行推测,最终输出推测的正确率,输出示比方下:
   

  • Accuracy:0.912
  实现代码如下: 
  1. import org.apache.spark.sql.SparkSession
  2. import org.apache.spark.sql.types.{StructType, StructField, StringType, DoubleType}
  3. import org.apache.spark.ml.feature.{StringIndexer, VectorAssembler}
  4. import org.apache.spark.ml.classification.LogisticRegression
  5. import org.apache.spark.ml.evaluation.{BinaryClassificationEvaluator, MulticlassClassificationEvaluator}
  6. object First_Question {
  7.   def main(args: Array[String]): Unit = {
  8.     /******************* Begin *******************/
  9.     // 创建SparkSession
  10.     val spark = SparkSession.builder()
  11.       .appName("Iris Binary Classification")
  12.       .getOrCreate()
  13.     // 定义schema
  14.     val schema = new StructType(Array(
  15.       StructField("SepalLength", DoubleType, true),
  16.       StructField("SepalWidth", DoubleType, true),
  17.       StructField("PetalLength", DoubleType, true),
  18.       StructField("PetalWidth", DoubleType, true),
  19.       StructField("Species", StringType, true)
  20.     ))
  21.     // 读取文件并应用schema
  22.     val data = spark.read
  23.       .option("inferSchema", "false")
  24.       .option("header", "false")
  25.       .schema(schema)
  26.       .csv("/data/bigfiles/iris.txt")
  27.     // 过滤掉Iris-setosa类
  28.     val filteredData = data.filter("Species != 'Iris-setosa'")
  29.     // 将label列转换为数值类型
  30.     val indexer = new StringIndexer()
  31.       .setInputCol("Species")
  32.       .setOutputCol("label")
  33.     val indexedData = indexer.fit(filteredData).transform(filteredData)
  34.     // 将特征列组合为一个向量列
  35.     val assembler = new VectorAssembler()
  36.       .setInputCols(Array("SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
  37.       .setOutputCol("features")
  38.     val featureData = assembler.transform(indexedData)
  39.     // 划分数据集为训练集和测试集
  40.     val Array(trainingData, testData) = featureData.randomSplit(Array(0.7, 0.3), seed = 1234L)
  41.     // 创建逻辑斯蒂回归模型
  42.     val lr = new LogisticRegression()
  43.     // 在训练集上拟合模型
  44.     val model = lr.fit(trainingData)
  45.     // 在测试集上进行预测
  46.     val predictions = model.transform(testData)
  47.     // 使用二分类评估器评估模型
  48.     val binaryEvaluator = new BinaryClassificationEvaluator()
  49.       .setLabelCol("label")
  50.       .setRawPredictionCol("rawPrediction")
  51.       .setMetricName("areaUnderROC")
  52.     val auc = binaryEvaluator.evaluate(predictions)
  53.     // 使用多分类评估器评估准确率
  54.     val multiEvaluator = new MulticlassClassificationEvaluator()
  55.       .setLabelCol("label")
  56.       .setPredictionCol("prediction")
  57.       .setMetricName("accuracy")
  58.     val accuracy = multiEvaluator.evaluate(predictions)
  59.     // 输出结果
  60.     println(s"Area under ROC: $auc")
  61.     println(s"Accuracy: $accuracy")
  62.     // 释放资源
  63.     spark.stop()
  64.     /******************* End *******************/
  65.   }
  66. }
复制代码
 第2关:第二题

打开右侧代码文件窗口,在 BeginEnd 地区增补代码,完成使命。
读取文件 /data/bigfiles/iris.txt 中的内容,使用决策树模子举行分析,通过 randomSplit 方法将其中的 70% 数据作为练习集,剩下的作为测试集举行推测,最终输出推测的正确率,输出示比方下:
   

  • Accuracy:0.912
  实现代码如下: 
  1. import org.apache.spark.sql.SparkSession
  2. import org.apache.spark.sql.types.{StructType, StructField, StringType, DoubleType}
  3. import org.apache.spark.ml.feature.{StringIndexer, VectorAssembler}
  4. import org.apache.spark.ml.classification.{DecisionTreeClassifier}
  5. import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
  6. object Second_Question {
  7.   def main(args: Array[String]): Unit = {
  8.     /******************* Begin *******************/
  9.     // 创建SparkSession
  10.     val spark = SparkSession.builder()
  11.       .appName("Iris Decision Tree Classification")
  12.       .getOrCreate()
  13.     // 定义schema
  14.     val schema = new StructType(Array(
  15.       StructField("SepalLength", DoubleType, true),
  16.       StructField("SepalWidth", DoubleType, true),
  17.       StructField("PetalLength", DoubleType, true),
  18.       StructField("PetalWidth", DoubleType, true),
  19.       StructField("Species", StringType, true)
  20.     ))
  21.     // 读取文件并应用schema
  22.     val data = spark.read
  23.       .option("inferSchema", "false")
  24.       .option("header", "false")
  25.       .schema(schema)
  26.       .csv("/data/bigfiles/iris.txt")
  27.     // 将label列转换为数值类型
  28.     val indexer = new StringIndexer()
  29.       .setInputCol("Species")
  30.       .setOutputCol("label")
  31.     val indexedData = indexer.fit(data).transform(data)
  32.     // 将特征列组合为一个向量列
  33.     val assembler = new VectorAssembler()
  34.       .setInputCols(Array("SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
  35.       .setOutputCol("features")
  36.     val featureData = assembler.transform(indexedData)
  37.     // 划分数据集为训练集和测试集
  38.     val Array(trainingData, testData) = featureData.randomSplit(Array(0.7, 0.3), seed = 1234L)
  39.     // 创建决策树模型
  40.     val dt = new DecisionTreeClassifier()
  41.       .setLabelCol("label")
  42.       .setFeaturesCol("features")
  43.     // 在训练集上拟合模型
  44.     val model = dt.fit(trainingData)
  45.     // 在测试集上进行预测
  46.     val predictions = model.transform(testData)
  47.     // 使用多分类评估器评估准确率
  48.     val evaluator = new MulticlassClassificationEvaluator()
  49.       .setLabelCol("label")
  50.       .setPredictionCol("prediction")
  51.       .setMetricName("accuracy")
  52.     val accuracy = evaluator.evaluate(predictions)
  53.     // 输出准确率
  54.     println(s"Accuracy: $accuracy")
  55.     // 释放资源
  56.     spark.stop()
  57.     /******************* End *******************/
  58.   }
  59. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

小小小幸运

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表