我使用 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()
时,将会出现错误。
您要做的是在整个列上使用 mean
和 std
。使用 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/