通常的 ML 管道涉及将 pandas 或 dask 数据帧处理成可以传递给 ML 模型的形式。然而,许多 dask-ml 模型不能接受 Dask 数据帧,因为它们不跟踪每个分区的行数。调用该fit
方法会抛出一个Cannot fit on dask.dataframe due to unknown partition lengths error
. 我应该怎么做才能将 Dask 数据帧传递给 dask-ml 模型?
这是一个例子:
import dask.dataframe as dd
import pandas as pd
from dask_ml.cluster import KMeans
df = dd.from_pandas(pd.DataFrame({'A': [1, 2, 3, 4, 5],
'B': [6, 7, 8, 9, 10]}),
npartitions=2)
kmeans = KMeans()
kmeans.fit(df)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-53-6c1545864b12> in <module>()
6
7 kmeans = KMeans()
----> 8 kmeans.fit(df)
~/anaconda3/envs/pds/lib/python3.6/site-packages/dask_ml/cluster/k_means.py in fit(self, X, y)
187
188 def fit(self, X, y=None):
--> 189 X = self._check_array(X)
190 labels, centroids, inertia, n_iter = k_means(
191 X,
~/anaconda3/envs/pds/lib/python3.6/site-packages/dask_ml/utils.py in wraps(*args, **kwargs)
298 def wraps(*args, **kwargs):
299 with _timer(f.__name__, _logger=logger, level=level):
--> 300 results = f(*args, **kwargs)
301 return results
302
~/anaconda3/envs/pds/lib/python3.6/site-packages/dask_ml/cluster/k_means.py in _check_array(self, X)
159 elif isinstance(X, dd.DataFrame):
160 raise TypeError(
--> 161 "Cannot fit on dask.dataframe due to unknown " "partition lengths."
162 )
163
TypeError: Cannot fit on dask.dataframe due to unknown partition lengths.