1-of-k编码 Apache Spark 在 Java 中

我在尝试测试我在Spark文档中找到的代码,以便在Apache Spark中使用Java处理分类特征:

SparkSession spark = SparkSession            .builder().master("local[4]")            .appName("1-of-K encoding Test")            .getOrCreate();List<Row> data = Arrays.asList(            RowFactory.create(0, "a"),            RowFactory.create(1, "b"),            RowFactory.create(2, "c"),            RowFactory.create(3, "a"),            RowFactory.create(4, "a"),            RowFactory.create(5, "c")    );StructType schema = new StructType(new StructField[]{ new StructField("id", DataTypes.IntegerType, false,Metadata.empty()),new StructField("category", DataTypes.StringType, false, Metadata.empty())    });Dataset<Row> df = spark.createDataFrame(data, schema);StringIndexerModel indexer = new StringIndexer().setInputCol("category").setOutputCol("categoryIndex").fit(df);

但是我遇到了这个错误;无法调用fit函数

enter image description here

你有什么想法吗?


回答:

你为什么要用这种较长的方式创建df?更有效的方法是:

  import sparkSession.implicits._  val df = sparkSession.sparkContext.parallelize(Seq((0, "a"), (1, "b"), (2, "c"), (3, "d"), (4, "e"), (5, "f"))).toDF("id", "category")  val newDf = new StringIndexer()    .setInputCol("category")    .setOutputCol("categoryIndex")    .fit(df)    .transform(df)    .show;

这会给出以下输出:

+---+--------+-------------+| id|category|categoryIndex|+---+--------+-------------+|  0|       a|          2.0||  1|       b|          3.0||  2|       c|          4.0||  3|       d|          5.0||  4|       e|          0.0||  5|       f|          1.0|+---+--------+-------------+

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注