gpt4 book ai didi

python - 使用 dask ml StandardScaler 时出现属性错误

转载 作者:行者123 更新时间:2023-12-01 07:34:58 24 4
gpt4 key购买 nike

我正在尝试重现 dask-ml 文档中的示例:https://dask-ml.readthedocs.io/en/latest/modules/api.html由于某种原因,这是用 sklearn 制作的:

from sklearn.preprocessing import StandardScaler
data = [[0, 0], [0, 0], [1, 1], [1, 1]]
scaler = StandardScaler()
print(scaler.fit(data))
StandardScaler(copy=True, with_mean=True, with_std=True)
print(scaler.mean_)

这是我用于 dask 的代码:

from dask_ml.preprocessing import StandardScaler
data = [[0, 0], [0, 0], [1, 1], [1, 1]]
scaler = StandardScaler()
print(scaler.fit(data))
StandardScaler(copy=True, with_mean=True, with_std=True)

这会引发以下错误:

属性错误:“列表”对象没有属性“平均值”

然后我尝试使用这篇中等帖子中的示例: https://towardsdatascience.com/speeding-up-your-algorithms-part-4-dask-7c6ed79994ef

df = dd.read_csv("test.csv",assume_missing=True)
sc = StandardScaler()
df["MSSubClass"] = sc.fit_transform(df["MSSubClass"])

这会引发此错误:

属性错误:“标量”对象没有属性“复制”

最佳答案

该示例的问题在于数据的类型不正确。转换为 numpy 数组并转换为 float 可以消除两个错误。有趣的是,尽管数据是整数列表,但转换步骤仍然有效。

import numpy as np
from dask_ml.preprocessing import StandardScaler

data = np.array([[0, 0], [0, 0], [1, 1], [1, 1]]).astype('float')

scaler = StandardScaler()

print(scaler.fit(data))
print(scaler.mean_)
print(scaler.transform(data))
print(scaler.transform([[2, 2]]))

关于python - 使用 dask ml StandardScaler 时出现属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57029101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com