如何将 StandardScaler() 转换转换回数据框?

How can I convert the StandardScaler() transformation back to dataframe?

我正在处理一个模型,在分成训练和测试之后,我想应用 StandardScaler()。但是,此转换将我的数据转换为数组,我想保留以前的格式。我该怎么做?

基本上,我有:

from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split

X = df[features]
y = df[["target"]]

X_train, X_test, y_train, y_test = train_test_split(
    X, y, train_size=0.7, random_state=42
)

sc = StandardScaler()
X_train_sc = sc.fit_transform(X_train)
X_test_sc = sc.transform(X_test)

如何让 X_train_sc 恢复到 X_train 的格式?

更新:我不想让 X_train_sc 返回到缩放之前。我只想 X_train_sc 以最简单的方式成为数据框。

正如您所提到的,在 numpy 数组中应用缩放结果,以获得您可以初始化一个新数据框的数据框:

import pandas as pd

cols = X_train.columns
sc = StandardScaler()
X_train_sc = pd.DataFrame(sc.fit_transform(X_train), columns=cols)
X_test_sc = pd.DataFrame(sc.transform(X_test), columns=cols)