【问题标题】:Seeing an error that says: 'numpy.ndarray' object has no attribute 'map'看到一条错误消息:“numpy.ndarray”对象没有属性“map”
【发布时间】:2020-05-15 01:46:25
【问题描述】:

我正在从一个更大的数据框中选择一个数据子集。

dataset = df.select('RatingScore',
             'CategoryScore',
             'CouponBin',
             'TTM',
             'Price',
             'Spread',
             'Coupon', 
             'WAM', 
             'DV')

dataset = dataset.fillna(0)
dataset.show(5,True)
dataset.printSchema()

现在,我将其纳入我的 KMeans 模型

from numpy import array
from math import sqrt
from pyspark.mllib.clustering import KMeans, KMeansModel
import numpy as np

data_array=np.array(dataset)

#data_array =  np.array(dataset.select('RatingScore', 'CategoryScore', 'CouponBin', 'TTM', 'Price', 'Spread', 'Coupon', 'WAM', #'DV').collect())

# Build the model (cluster the data)
clusters = KMeans.train(data_array, 2, maxIterations=10, initializationMode="random")

# Evaluate clustering by computing Within Set Sum of Squared Errors
def error(point):
    center = clusters.centers[clusters.predict(point)]
    return sqrt(sum([x**2 for x in (point - center)]))

WSSSE = data_array.map(lambda point: error(point)).reduce(lambda x, y: x + y)
print("Within Set Sum of Squared Error = " + str(WSSSE))

这一行:clusters = KMeans.train(data_array, 2, maxIterations=10, initializationMode="random")

抛出此错误:AttributeError: 'numpy.ndarray' object has no attribute 'map'

从代码中,您可以看到我尝试了两种不同的方式来创建数组。都没有奏效。如果我尝试直接从子集数据框中收取项目费用,我会收到此错误:

AttributeError: 'DataFrame' object has no attribute 'map'

我在这里错过了什么?

【问题讨论】:

标签: python python-3.x k-means


【解决方案1】:

我认为有两种方法:

  1. 按照建议的in other similar situationspandas.DataFrame 转换为spark_df.rdd
  2. 根据its official docpandas.DataFrame转化为多个pandas.Series

【讨论】:

    猜你喜欢
    • 2021-12-10
    • 1970-01-01
    • 2018-05-21
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    相关资源
    最近更新 更多