IllegalArgumentException: u'requirement failed' on kmeans.fit



使用Zeppelin Notebook中的Spark,我从昨天开始遇到此错误。这是我的代码:

from pyspark.ml.clustering import KMeans
from pyspark.ml.feature import VectorAssembler
df = sqlContext.table("rfmdata_clust")
k = 4
# Set Kmeans input/output columns
vecAssembler = VectorAssembler(inputCols=["v1_clust", "v2_clust", "v3_clust"], outputCol="features")
featuresDf = vecAssembler.transform(df)
# Run KMeans
kmeans = KMeans().setInitMode("k-means||").setK(k)
model = kmeans.fit(featuresDf)
resultDf = model.transform(featuresDf)
# KMeans WSSSE
wssse = model.computeCost(featuresDf)
print("Within Set Sum of Squared Errors = " + str(wssse))

这是错误:

Traceback (most recent call last):
  File "/tmp/zeppelin_pyspark-8890997346928959256.py", line 346, in <module>
    raise Exception(traceback.format_exc())
Exception: Traceback (most recent call last):
  File "/tmp/zeppelin_pyspark-8890997346928959256.py", line 334, in <module>
    exec(code)
  File "<stdin>", line 8, in <module>
  File "/usr/lib/spark/python/pyspark/ml/base.py", line 64, in fit
    return self._fit(dataset)
  File "/usr/lib/spark/python/pyspark/ml/wrapper.py", line 236, in _fit
    java_model = self._fit_java(dataset)
  File "/usr/lib/spark/python/pyspark/ml/wrapper.py", line 233, in _fit_java
    return self._java_obj.fit(dataset._jdf)
  File "/usr/lib/spark/python/lib/py4j-0.10.4-src.zip/py4j/java_gateway.py", line 1133, in __call__
    answer, self.gateway_client, self.target_id, self.name)
  File "/usr/lib/spark/python/pyspark/sql/utils.py", line 79, in deco
    raise IllegalArgumentException(s.split(': ', 1)[1], stackTrace)
IllegalArgumentException: u'requirement failed'

抛出错误的线是kmeans.fit((。我检查了rfmdata_clust dataframe,这似乎根本不奇怪。

df.printSchema()给出:

root
 |-- id: string (nullable = true)
 |-- v1_clust: double (nullable = true)
 |-- v2_clust: double (nullable = true)
 |-- v3_clust: double (nullable = true)

featuresDf.printSchema()给出:

root
 |-- id: string (nullable = true)
 |-- v1_clust: double (nullable = true)
 |-- v2_clust: double (nullable = true)
 |-- v3_clust: double (nullable = true)
 |-- features: vector (nullable = true)

另一个有趣的点是,在功能DF的定义下方添加featuresDf = featuresDf.limit(10000)使代码在没有错误的情况下运行。也许它与数据的大小有关?

希望这已经解决,如果没有解决,请尝试此

    df=df.na.fill(1)

这将填充NAN的所有值,当然可以选择任何其他值。错误是由于您在功能向量中有NAN的事实。您可能需要导入必要的软件包。这也应该有所帮助。

让我知道这是否失败。

最新更新