【问题标题】:Error: AttributeError: 'DataFrame' object has no attribute '_jdf'错误:AttributeError:“DataFrame”对象没有属性“_jdf”
【发布时间】:2019-04-10 03:51:36
【问题描述】:

我想使用 pyspark 执行 k 折交叉验证来微调参数,我正在使用 pyspark.ml。 我收到属性错误。

AttributeError: 'DataFrame' 对象没有属性 '_jdf'

我最初尝试使用 pyspark.mllib,但未能成功执行 k 折交叉验证

import pandas as pd
from pyspark import SparkConf, SparkContext
from pyspark.ml.classification import DecisionTreeClassifier

data=pd.read_csv("file:///SparkCourse/wdbc.csv", header=None)
type(data)
print(data)

conf = SparkConf().setMaster("local").setAppName("SparkDecisionTree")
sc = SparkContext(conf = conf)

# Create initial Decision Tree Model
dt = DecisionTreeClassifier(labelCol="label", featuresCol="features", 
maxDepth=3)

# Train model with Training Data
dtModel = dt.fit(data)

# I expect the model to be trained but I'm getting the following error 
AttributeError: 'DataFrame' object has no attribute '_jdf'

注意:我可以打印数据。错误在 dtModel 中

【问题讨论】:

  • 您需要将 pandas 数据帧转换为 spark 数据帧
  • 我会尝试这样做。谢谢
  • 如果它对某人有帮助。如果您在加载后将 DataFrame 转换为 pandas 以进行显示,也可能会引发此错误。例如,通过使用df.limit(5).toPandas()

标签: pyspark


【解决方案1】:

将 Panadas 转换为 Spark

from pyspark.sql import SQLContext
sc = SparkContext.getOrCreate()
sqlContext = SQLContext(sc)

spark_dff = sqlContext.createDataFrame(panada_df)

【讨论】:

    【解决方案2】:

    如果出现指标评估错误,您可能:

    1. 在测试集上使用 Spark 正确转换,然后使用 Pandas DF 进行查看。
    # Spark model, transformed test, converted to pandas df
    predictions = model.transform(test)
    predDF = predictions.toPandas()
    predDF.head()
    
    1. 然后尝试:
    eval_acc = MulticlassClassificationEvaluator(
                labelCol='Label_index',
                predictionCol='prediction',
                metricName='accuracy'
    )
    
    # Evaluate Performance
    acc = eval_acc.evaluate(predDF) # Error
    print(f"accuracy: {acc}")
    

    我忘了 predDF 是 Pandas DataFrame。 需要预测,因为它是 Spark Dataframe。

    acc = eval_acc.evaluate(predictions) # Works
    print(f"accuracy: {acc}")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      • 2019-12-03
      • 1970-01-01
      • 2023-02-10
      • 2017-01-24
      相关资源
      最近更新 更多