无法将pyspark数据帧加载到决策树算法。它说不能与pyspark数据帧一起使用

我正在研究IBM的数据平台。我能够将数据加载到pyspark数据框中,并生成了一个spark SQL表。分割数据集后,将其输入到分类算法中。它会引发错误,例如无法加载Spark SQL数据。必需的ndarrays。

from sklearn.ensemble import RandomForestRegressor`
from sklearn.model_selection import train_test_split`
from sklearn import preprocessing`
import numpy as np`

X_train,y_train,X_test,y_test = train_test_split(x,y,test_size = 0.1,random_state = 42)
RM = RandomForestRegressor()
RM.fit(X_train.reshape(1,-1),y_train)`

错误:

  

TypeError:预期的序列或类似数组的类型,得到了{

此错误发生后,我做了这样的事情:

x = spark.sql('select Id,YearBuilt,MoSold,YrSold,Fireplaces FROM Train').toPandas()
y = spark.sql('Select SalePrice FROM Train where SalePrice is not null').toPandas()

错误:

  

AttributeError跟踪(最近一次通话)    在()中         5 X_train,y_train,X_test,y_test = train_test_split(x,y,test_size = 0.1,random_state = 42)         6 RM = RandomForestRegressor()   ----> 7 RM.fit(X_train.reshape(1,-1),y_train)   / strong / getattr 中的/opt/ibm/conda/miniconda3.6/lib/python3.6/site-packages/pandas/core/generic.py(自身,名称)      第5065章(小幸运)心有灵犀      5066返回自身[名称]   -> 5067返回对象。 getattribute ((自身,名称)      5068      5069 def setattr ((自身,名称,值):   AttributeError:“ DataFrame”对象没有属性“ reshape”

routernsn 回答:无法将pyspark数据帧加载到决策树算法。它说不能与pyspark数据帧一起使用

如sklearn文档所述:

"""
    X : array-like or sparse matrix,shape = [n_samples,n_features]
"""
regr = RandomForestRegressor()
regr.fit(X,y)

因此,首先,您尝试将pandas.DataFrame而不是array用作X参数。

第二,reshape()方法不是DataFrame对象的属性,而是numpy array的属性。

import numpy as np
x = np.array([[2,3,4],[5,6,7]]) 
np.reshape(x,(3,-1))

希望这会有所帮助。

本文链接:https://www.f2er.com/3053467.html

大家都在问