您可以在下面的代码中找到我在互联网上建立的简单神经网络。一切正常,但是当我编码y标签时,得到的预测结果如下:

2 0 1 2 1 2 2 0 2 1 0 0 0 1 1 1 1 1 1 1 1 2 1 2 1 0 1 0 1 0 2

因此,现在我需要将其转换回原始的花类(鸢尾属-virginica等)。我需要使用inverse_transform方法,但是您可以帮忙吗?

import pandas as pd
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import classification_report, confusion_matrix


# Location of dataset
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

# Assign colum names to the dataset
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'Class']

# Read dataset to pandas dataframe
irisdata = pd.read_csv(url, names=names)

irisdata.head()
#head_tableau=irisdata.head()
#print(head_tableau)

# Assign data from first four columns to X variable
X = irisdata.iloc[:, 0:4]

# Assign data from first fifth columns to y variable
y = irisdata.select_dtypes(include=[object])

y.head()
#afficher_y=y.head()
#print(afficher_y)

y.Class.unique()
#affiche=y.Class.unique()
#print(affiche)

le = preprocessing.LabelEncoder()

y = y.apply(le.fit_transform)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)

mlp = MLPClassifier(hidden_layer_sizes=(10, 10, 10), max_iter=1000)
mlp.fit(X_train, y_train.values.ravel())

predictions = mlp.predict(X_test)
print(predictions)

最佳答案

您走在正确的轨道上:

In [7]: le.inverse_transform(predictions[:5])
Out[7]:
array(['Iris-virginica', 'Iris-setosa', 'Iris-setosa', 'Iris-versicolor',
       'Iris-virginica'], dtype=object)

关于python - Inverse_transform方法(LabelEncoder),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52870022/

10-09 01:51