首页 > 解决方案 > 应用聚类算法后尝试使用标签编码器进行逆变换时出现值错误

问题描述

我在哪里使用标签编码器来标记分类列。但是,在将其转换回来时,我遇到了价值错误

我使用了 sklearn 的标签编码器

代码:

    from sklearn.preprocessing import LabelEncoder
    enc = LabelEncoder()
    label_encoder = enc.fit(Final.iloc[:,3])
    print ("Categorical classes:", label_encoder.classes_)
    integer_classes = label_encoder.transform(label_encoder.classes_)
    print ("Integer classes:", integer_classes)
    t = label_encoder.transform(Final.iloc[:,3])
    Final.iloc[:, 3] = t

    data = Final.iloc[:,3:11]
    from sklearn.ensemble import IsolationForest
    import numpy as np


    clf=IsolationForest(n_estimators=100, max_samples='auto', contamination=float(.03))
    clf.fit(data)
    pred = clf.predict(data)

    Final['anomaly']=pred
    outliers=Final.loc[Final['anomaly']==-1]
    outlier_index=list(outliers.index)

    print(Final['anomaly'].value_counts())


    t = label_encoder.inverse_transform(Final.iloc[:,3])
    Final.iloc[:,3] = t

错误:

     ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

标签: pythonpython-3.xpandassklearn-pandasencoder

解决方案


推荐阅读