我当时在研究IBM的数据平台。我能够将数据加载到pyspark数据框中,并生成了一个火花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:期望的序列或类似数组的类型,得到了{<} class'pyspark.sql.dataframe.DataFrame'>
发生此错误后,我做了这样的事情:
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 Traceback(最近一次通话最后一次)()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)/opt/ibm/conda/miniconda3.6/lib/python3.6/site-packages/pandas/core/generic.py in getattr(self,name)5065 if self ._info_axis._can_hold_identifiers_and_holds_name(name):5066返回self [name]-> 5067返回对象。getattribute(自身,名称)5068 5069 def setattr(自身,名称,值):AttributeError:'DataFrame'对象没有属性'reshape'
如sklearn文档所述:
"""
X : array-like or sparse matrix, shape = [n_samples, n_features]
"""
regr = RandomForestRegressor()
regr.fit(X, y)
因此,首先,您尝试使用apandas.DataFrame
代替X作为参数array
。
其次,reshape()方法不是DataFrame
对象的属性,而是numpy array
。
import numpy as np
x = np.array([[2,3,4], [5,6,7]])
np.reshape(x, (3, -1))
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句