python - 在数据框中使用 z 变换

标签 python numpy

我使用 RetailRocket 作为我的数据集。我为每个事件分配了一个值,view = 1,addtocart = 2,transaction = 3。现在我想使用 z 变换来规范化这些值。不幸的是我得到了一个错误。我的错误在哪里?

这是我的 z 变换代码:

df = df.sample(frac=1, random_state=42)
x = df[["visitorid", "itemid"]].values
#y = df["code"].values
y = df["code"].apply(lambda x: (x - x.mean()) / x.std()).values
# Assuming training on 90% of the data and validating on 10%.
train_indices = int(0.9 * df.shape[0])
x_train, x_val, y_train, y_val = (
    x[:train_indices],
    x[train_indices:],
    y[:train_indices],
    y[train_indices:],
)
print(y)

我用 numpy 找到了这个 z 变换的公式:

X = (X - X.mean()) / X.std()

错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-2712d78bf2a4> in <module>()
      2 x = df[["visitorid", "itemid"]].values
      3 #y = df["code"].values
----> 4 y = df["code"].apply(lambda x: (x - x.mean()) / x.std()).values
      5 # Assuming training on 90% of the data and validating on 10%.
      6 train_indices = int(0.9 * df.shape[0])

1 frames
pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

<ipython-input-7-2712d78bf2a4> in <lambda>(x)
      2 x = df[["visitorid", "itemid"]].values
      3 #y = df["code"].values
----> 4 y = df["code"].apply(lambda x: (x - x.mean()) / x.std()).values
      5 # Assuming training on 90% of the data and validating on 10%.
      6 train_indices = int(0.9 * df.shape[0])

AttributeError: 'int' object has no attribute 'mean'

最佳答案

由于您使用apply(lambda x: ...)x 将只是一个值。当您尝试对该单个值使用 x.mean() 时,将会出现错误。

您要做的是在整个列上使用 meanstd。使用 apply,可以按如下方式完成:

col = 'code'
df['z_score'] = df[col].apply(lambda x: (x - df[col].mean()) / df[col].std())

但是,不使用 apply 会更快:

df['z_score'] = (df[col] - df[col].mean())/df[col].std()

关于python - 在数据框中使用 z 变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63914972/

相关文章:

python - 修改默认 Ttk Clam 小部件颜色

python - 在 Python 中,如何创建从现在起 15 分钟后的日期时间? 1小时后?

python - 我们如何杀死 python 中 subprocess.call() 函数产生的进程?

python - 使用 cython 从 c 调用 python 代码

python - 如何获取 boolean numpy 数组和另一个 boolean 数组的相对补集?

python - 更快的 numpy 解决方案而不是 itertools.combinations?

python - 我可以使用 lambdify 来评估 python 函数的导数吗?

python - 如何将日期数组传递给 pcolor 图?

python - 如何使用 7zip 而不是 zip 进行压缩,更改代码

python - 时间序列为 2 个 numpy 数组 ('Date' 和 'Data' ),然后从指定的 'Data' 范围中提取 'Date' ?