第1关:第一题
打开右侧代码文件窗口,在 Begin 至 End 地区增补代码,完成使命。
读取文件 /data/bigfiles/iris.txt 中的内容,使用二项逻辑斯蒂回归举行二分类分析,过滤 Iris-setosa 类。通过 randomSplit 方法将其中的 70% 数据作为练习集,剩下的作为测试集举行推测,最终输出推测的正确率,输出示比方下:
实现代码如下:
- import org.apache.spark.sql.SparkSession
- import org.apache.spark.sql.types.{StructType, StructField, StringType, DoubleType}
- import org.apache.spark.ml.feature.{StringIndexer, VectorAssembler}
- import org.apache.spark.ml.classification.LogisticRegression
- import org.apache.spark.ml.evaluation.{BinaryClassificationEvaluator, MulticlassClassificationEvaluator}
- object First_Question {
- def main(args: Array[String]): Unit = {
- /******************* Begin *******************/
- // 创建SparkSession
- val spark = SparkSession.builder()
- .appName("Iris Binary Classification")
- .getOrCreate()
- // 定义schema
- val schema = new StructType(Array(
- StructField("SepalLength", DoubleType, true),
- StructField("SepalWidth", DoubleType, true),
- StructField("PetalLength", DoubleType, true),
- StructField("PetalWidth", DoubleType, true),
- StructField("Species", StringType, true)
- ))
- // 读取文件并应用schema
- val data = spark.read
- .option("inferSchema", "false")
- .option("header", "false")
- .schema(schema)
- .csv("/data/bigfiles/iris.txt")
- // 过滤掉Iris-setosa类
- val filteredData = data.filter("Species != 'Iris-setosa'")
- // 将label列转换为数值类型
- val indexer = new StringIndexer()
- .setInputCol("Species")
- .setOutputCol("label")
- val indexedData = indexer.fit(filteredData).transform(filteredData)
- // 将特征列组合为一个向量列
- val assembler = new VectorAssembler()
- .setInputCols(Array("SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
- .setOutputCol("features")
- val featureData = assembler.transform(indexedData)
- // 划分数据集为训练集和测试集
- val Array(trainingData, testData) = featureData.randomSplit(Array(0.7, 0.3), seed = 1234L)
- // 创建逻辑斯蒂回归模型
- val lr = new LogisticRegression()
- // 在训练集上拟合模型
- val model = lr.fit(trainingData)
- // 在测试集上进行预测
- val predictions = model.transform(testData)
- // 使用二分类评估器评估模型
- val binaryEvaluator = new BinaryClassificationEvaluator()
- .setLabelCol("label")
- .setRawPredictionCol("rawPrediction")
- .setMetricName("areaUnderROC")
- val auc = binaryEvaluator.evaluate(predictions)
- // 使用多分类评估器评估准确率
- val multiEvaluator = new MulticlassClassificationEvaluator()
- .setLabelCol("label")
- .setPredictionCol("prediction")
- .setMetricName("accuracy")
- val accuracy = multiEvaluator.evaluate(predictions)
- // 输出结果
- println(s"Area under ROC: $auc")
- println(s"Accuracy: $accuracy")
- // 释放资源
- spark.stop()
- /******************* End *******************/
- }
- }
复制代码 第2关:第二题
打开右侧代码文件窗口,在 Begin 至 End 地区增补代码,完成使命。
读取文件 /data/bigfiles/iris.txt 中的内容,使用决策树模子举行分析,通过 randomSplit 方法将其中的 70% 数据作为练习集,剩下的作为测试集举行推测,最终输出推测的正确率,输出示比方下:
实现代码如下:
- import org.apache.spark.sql.SparkSession
- import org.apache.spark.sql.types.{StructType, StructField, StringType, DoubleType}
- import org.apache.spark.ml.feature.{StringIndexer, VectorAssembler}
- import org.apache.spark.ml.classification.{DecisionTreeClassifier}
- import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
- object Second_Question {
- def main(args: Array[String]): Unit = {
- /******************* Begin *******************/
- // 创建SparkSession
- val spark = SparkSession.builder()
- .appName("Iris Decision Tree Classification")
- .getOrCreate()
- // 定义schema
- val schema = new StructType(Array(
- StructField("SepalLength", DoubleType, true),
- StructField("SepalWidth", DoubleType, true),
- StructField("PetalLength", DoubleType, true),
- StructField("PetalWidth", DoubleType, true),
- StructField("Species", StringType, true)
- ))
- // 读取文件并应用schema
- val data = spark.read
- .option("inferSchema", "false")
- .option("header", "false")
- .schema(schema)
- .csv("/data/bigfiles/iris.txt")
- // 将label列转换为数值类型
- val indexer = new StringIndexer()
- .setInputCol("Species")
- .setOutputCol("label")
- val indexedData = indexer.fit(data).transform(data)
- // 将特征列组合为一个向量列
- val assembler = new VectorAssembler()
- .setInputCols(Array("SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
- .setOutputCol("features")
- val featureData = assembler.transform(indexedData)
- // 划分数据集为训练集和测试集
- val Array(trainingData, testData) = featureData.randomSplit(Array(0.7, 0.3), seed = 1234L)
- // 创建决策树模型
- val dt = new DecisionTreeClassifier()
- .setLabelCol("label")
- .setFeaturesCol("features")
- // 在训练集上拟合模型
- val model = dt.fit(trainingData)
- // 在测试集上进行预测
- val predictions = model.transform(testData)
- // 使用多分类评估器评估准确率
- val evaluator = new MulticlassClassificationEvaluator()
- .setLabelCol("label")
- .setPredictionCol("prediction")
- .setMetricName("accuracy")
- val accuracy = evaluator.evaluate(predictions)
- // 输出准确率
- println(s"Accuracy: $accuracy")
- // 释放资源
- spark.stop()
- /******************* End *******************/
- }
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |