【发布时间】: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'
我在这里错过了什么?
【问题讨论】:
-
这些对象都没有
.map属性,或者就此而言,reduce -
您从这些错误消息中了解(不)什么?
标签: python python-3.x k-means