我正在试图解决以下错误,我已经看到了这方面的一些帖子,但无法解决。 org.apache.spark.sql.AnalysisException:无法解析*所有列中的列名“features”
我尝试过的事情:
tempList = []
for col in Df.columns:
new_name = col.strip()
new_name = "".join(new_name.split())
new_name = new_name.replace('.','')
tempList.append(new_name)
Df = Df.toDF(*tempList)
我的Spark数据框中只有6列,所有的只有有字符和下划线。 数据帧的方案:
StructType(List(StructField(A,ShortType,true),StructField(B,ShortType,true),StructField(C,IntegerType,true),StructField(D,IntegerType,true),StructField(E,StringType,true),StructField(F,DoubleType,true),StructField(G,IntegerType,true)))
我正试图从这里实现PCA,https://www.nodalpoint.com/pca-in-spark-1-5/
参考代码:
df = sc.parallelize([[1,2,3], [2,3,4]]).toDF(("a_1", "b", "c"))
def estimateCovariance(df):
m = df.select(df['features']).map(lambda x: x[0]).mean()
dfZeroMean = df.select(df['features']).map(lambda x: x[0]).map(lambda x: x-m) # subtract the mean
return dfZeroMean.map(lambda x: np.outer(x,x)).sum()/df.count()
def pca(df, k=2):
cov = estimateCovariance(df)
col = cov.shape[1]
eigVals, eigVecs = eigh(cov)
inds = np.argsort(eigVals)
eigVecs = eigVecs.T[inds[-1:-(col+1):-1]]
components = eigVecs[0:k]
eigVals = eigVals[inds[-1:-(col+1):-1]] # sort eigenvalues
score = df.select(df['features']).map(lambda x: x[0]).map(lambda x: np.dot(x, components.T) )
scoreDF = sqlContext.createDataFrame(score.map(lambda x: (DenseVector(x),)), ['pca_features'])
# Return the `k` principal components, `k` scores, and all eigenvalues
return components.T, scoreDF, eigVals
comp, score, eigVals = pca(df)
score.collect()
对可能出现的问题有什么想法吗
从您链接到的文章中:
再进一步,我们将为您提供一个如何构建示例数据集的示例:
数据集包含许多不同列中的数据。你需要把它转换成一列向量。Spark ML有一个用于此的工具,即}
pyspark.ml.feature.
^{在您的情况下,您需要以下内容:
看起来您没有列
features
-如果我正确理解了这个问题,那么本例中的所有列都是特性,因此您可能希望选择所有列相关问题 更多 >
编程相关推荐