gpt4 book ai didi

python - 隔离林 : Categorical data

转载 作者:行者123 更新时间:2023-12-03 20:15:49 24 4
gpt4 key购买 nike

我正在尝试使用 sklearn 中的隔离森林检测乳腺癌数据集中的异常。我正在尝试将 Iolation Forest 应用于混合数据集,当我拟合模型时,它会给我值错误。

这是我的数据集:
https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer/

这是我的代码:

from sklearn.model_selection import train_test_split
rng = np.random.RandomState(42)

X = data_cancer.drop(['Class'],axis=1)
y = data_cancer['Class']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 20)
X_outliers = rng.uniform(low=-4, high=4, size=(X.shape[0], X.shape[1]))

clf = IsolationForest()
clf.fit(X_train)

这是我得到的错误:

ValueError: could not convert string to float: '30-39'



是否可以对分类数据使用隔离森林?如果是,我该怎么做?

最佳答案

您应该将分类数据编码为数字表示。
有很多方法可以对分类数据进行编码,但我建议您从sklearn.preprocessing.LabelEncoder如果基数很高并且 sklearn.preprocessing.OneHotEncoder如果基数很低。
这是一个使用示例:

import numpy as np
from numpy import argmax
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
# define example
data = ['cold', 'cold', 'warm', 'cold', 'hot', 'hot', 'warm', 'cold', 'warm', 'hot']
values = np.array(data)
print(values)
# integer encode
label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(values)
print(integer_encoded)
# binary encode
onehot_encoder = OneHotEncoder(sparse=False)
integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
print(onehot_encoded)
# invert first example
inverted = label_encoder.inverse_transform([argmax(onehot_encoded[0, :])])
print(inverted)
输出:
['cold' 'cold' 'warm' 'cold' 'hot' 'hot' 'warm' 'cold' 'warm' 'hot']

[0 0 2 0 1 1 2 0 2 1]

[[ 1. 0. 0.]
[ 1. 0. 0.]
[ 0. 0. 1.]
[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]
[ 1. 0. 0.]
[ 0. 0. 1.]
[ 0. 1. 0.]]

['cold']

关于python - 隔离林 : Categorical data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54886223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com