【问题标题】:How do I make sure that all data is going to be the same number when transforming data for training and predicting with sklearn models?在转换数据以使用 sklearn 模型进行训练和预测时,如何确保所有数据都是相同的数字?
【发布时间】:2020-02-15 16:57:42
【问题描述】:

我想确保传入数据集中的数据与训练模型的数据相同。比如……

df = pd.Dataframe({'prediction':['red', 'green', 'blue'], 'features': ['one','two','three']})

转换后应该是这样的:

>>>df
prediction  features
1           1
2           2
3           3

现在我想确定一组新数据...

new_df = pd.Dataframe({'prediction':['yellow', 'red', 'green'], 'features': ['three','two','one']})

将被转换为与原始 DataFrame df 相同的内容。请注意,我确实在new_df 中添加了一些内容,因为模型也必须处理它。新的数据框应该看起来像这样......

>>>new_df
prediction  features
4           3
1           2
2           1

如何实现这一点,以及如何对数据进行逆变换?

【问题讨论】:

    标签: python python-3.x scikit-learn encode sklearn-pandas


    【解决方案1】:

    您可以在此处使用LabelEncoder

    import pandas as pd
    df = pd.DataFrame({'prediction':['red', 'green', 'blue'], 'features': ['one','two','three']})
    from sklearn import preprocessing
    le = preprocessing.LabelEncoder()
    le.fit(df["prediction"])
    oldData = df['prediction'].tolist()
    df["prediction"] = le.transform(df["prediction"])
    new_df = pd.DataFrame({'prediction':['yellow', 'red', 'green'], 'features': ['three','two','one']})
    newData = new_df['prediction'].tolist()
    newData = list(set(newData)- set(oldData))
    le.classes_ = np.append(le.classes_, newData )
    new_df["prediction"] = le.transform(new_df["prediction"])
    

    更新

    import pandas as pd
    df = pd.DataFrame({'prediction':['red', 'green', 'blue'], 'features': ['one','two','three']})
    from sklearn import preprocessing
    encoderDict = {}
    oldData = {}
    for col in df.columns:
        le = preprocessing.LabelEncoder()
        le.fit(df[col])
        encoderDict[col] = le
        oldData[col] = df[col].tolist()
        df[col] = le.transform(df[col])
    new_df = pd.DataFrame({'prediction':['yellow', 'red', 'green'], 'features': ['three','two','one']})
    newData = {}
    for col in new_df.columns:
        newData[col] = new_df[col].tolist()
        newData[col] = list(set(newData[col])- set(oldData[col]))
        encoderDict[col].classes_ = np.append(encoderDict[col].classes_, newData[col] )
        new_df[col] = encoderDict[col].transform(new_df[col])
    

    要对数据进行逆变换,您只需执行以下操作。

    ndf = df.append(new_df).reset_index(drop=True)
    for col in ndf:
        print(encoderDict[col].inverse_transform(ndf[col]))
    

    【讨论】:

    • 太棒了!太感谢了。您能否也将逆变换添加到代码中?我现在要对其进行测试,如果一切正常,我会将其标记为答案。更新 - 它无法正常工作。 'red' 和其他相同值的获得新的类标签。它们必须相同......另外,我很确定整个数据框需要转换为数字,否则它不适合模型。
    • 进行了更改,现在应该可以正常工作了。如果要对整个数据集运行它,请将其放入 for 循环并将 "prediction" 更改为所需的列名。
    • 我可以使用df = df.apply(lambda x: le.transform(x)) 或类似的东西吗?如果可以的话,你能把它也放入代码中吗?
    • 你可以,但你不应该。对完整数据集使用单个编码器不是一个好主意
    • 好的,但是在训练 ML 模型时,所有内容都需要进行数字编码。你建议我怎么做?我需要确保所有值都保持不变...
    猜你喜欢
    • 2017-08-29
    • 2015-05-25
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多