ValueError:sklearn Python中的错误输入形状

问题描述 投票:0回答:2

我有2个列表featureslabelsfeatures包含疾病,年龄,性别,PIN。 labels包含健康计划。

用户传递user_input,格式为features。因此,代码应该使用DecisionTreesklearn API为用户预测健康计划。

features中的参数很少是Strings。例如疾病和性别。我使用LabelEncoder编码它们以避免错误'ValueError: could not convert string to float'。

现在,在使用Label Encoder后,我得到以下异常'ValueError: bad input shape'

如何解决问题并再次反转编码以避免String to Float错误。请帮忙。

from sklearn import tree
from sklearn.preprocessing import LabelEncoder
features = [['TB' , 28, 'MALE', 121001], ['TB' , 28, 'FEMALE', 121002], ['CANCER' , 28, 'MALE', 121001], ['CANCER' , 28, 'FEMALE', 121001]]
labels = ['X125434', 'X125436','X125437' , 'X125437']
user_input = ['TB' , 28, 'MALE', 121001]

le = LabelEncoder()

Y = le.fit_transform(features)
X = le.fit_transform(labels)
new_user_input = le.fit_transform(user_input)

clf = tree.DecisionTreeClassifier()
clf = clf.fit(new_features, new_labels)

print(clf.predict([new_ui]))
python python-3.x scikit-learn
2个回答
3
投票

建议不要对数据集中的所有功能使用相同的标签编码器。为每列创建标签编码器是安全的,因为每个功能在值方面都有所不同。

from sklearn import tree
from sklearn.preprocessing import LabelEncoder
import pandas as pd

features = [['TB' , 28, 'MALE', 121001], ['TB' , 28, 'FEMALE', 121002], ['CANCER' , 28, 'MALE', 121001], ['CANCER' , 28, 'FEMALE', 121001]]
labels = ['X125434', 'X125436','X125437' , 'X125437']
feature_names=['Disease','Age','Gender','PIN']

user_input = ['TB' , 28, 'MALE', 121001]


train=pd.DataFrame(data=features,columns=['Disease','Age','Gender','PIN'])
train['Labels']=labels

test=pd.DataFrame(columns=['Disease','Age','Gender','PIN'])
test.loc[len(test)]=user_input

le_disease = LabelEncoder()
le_gender = LabelEncoder()
le_labels = LabelEncoder()

train['Disease'] = le_disease.fit_transform(train['Disease'])
train['Gender'] = le_gender.fit_transform(train['Gender'])
train['Labels'] = le_labels.fit_transform(train['Labels'])


test['Disease'] = le_disease.transform(test['Disease'])
test['Gender'] = le_gender.transform(test['Gender'])


clf = tree.DecisionTreeClassifier()
clf = clf.fit(train[feature_names], train['Labels'])

print(le_labels.inverse_transform(clf.predict(test[feature_names])))

LabelEncoder.inverse_transform()可用于获取原始数据。


2
投票

根据LabelEncoder documentation的说法,你似乎是以错误的方式使用它,所以你得到的例外是正确的说法。

在你的情况下,我认为你想编码DiseasesGenderHealth-Plan作为整数:例如,TBCANCER将成为01MALEFEMALE也将成为01; X125434X125436X125437将编码为012

例:

from sklearn import tree
from sklearn.preprocessing import LabelEncoder

features = [
    ['TB' , 28, 'MALE', 121001],
    ['TB' , 28, 'FEMALE', 121002],
    ['CANCER' , 28, 'MALE', 121001],
    ['CANCER' , 28, 'FEMALE', 121001]]
labels = ['X125434', 'X125436','X125437' , 'X125437']
user_input = ['TB' , 28, 'MALE', 121001]

# use different encoders for different data
le = LabelEncoder()
le_diseases = LabelEncoder()
le_gender = LabelEncoder()

diseases = [features_list[0] for features_list in features]
gender = [features_list[2] for features_list in features]

features_preprocessed = []
diseases_labels = le_diseases.fit_transform(diseases)
gender_labels = le_gender.fit_transform(gender)
for i, features_list in enumerate(features):
    features_preprocessed.append([
        diseases_labels[i],
        features[i][1],
        gender_labels[i],
        features[i][3]])

labels_preprocessed = le.fit_transform(labels)

# ... then use features_preprocessed, labels_preprocessed and the label encoders above

附:我建议你使用pandas数据框而不是列表:正如你从上面的例子中看到的那样,在这种情况下使用列表看起来并不是很干净。您的功能如下所示:

import pandas as pd
features_df = pd.DataFrame({
    'Diseases': ['TB' , 'TB', 'CANCER', 'CANCER'],
    'Age': [28, 28, 28, 28],
    'Gender': ['MALE', 'FEMALE', 'MALE', 'FEMALE'],
    'PIN': [121001, 121002, 121001, 121001]
})
© www.soinside.com 2019 - 2024. All rights reserved.