Inverse_transform方法(LabelEncoder) [英] Inverse_transform method (LabelEncoder)

查看:1530
本文介绍了Inverse_transform方法(LabelEncoder)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

You can find below the code I found on the internet to build a simple Neural network. Everyhting works fine but as I encoded the y labels the predictions I get give this result:

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

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

因此,现在我需要将其转换回原始的花类(虹膜-维吉尼亚州等).我需要使用inverse_transform方法,但是您能帮忙吗?

So now I need to convert it back to the original flower class (Iris-virginica, etc). I need to use the inverse_transform method but can you help out?

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)

这篇关于Inverse_transform方法(LabelEncoder)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆