我理解如何使用多种技术(包括独热编码、标签编码、序数编码等)将标记数据编码为数值数据。我想知道如何将数值数据转换回标记数据。以下是一个简单的示例。
import pandas as pdimport numpy as np# Load Libraryimport pandas as pdimport numpy as npimport matplotlib.pyplot as pltfrom sklearn.datasets import make_moonsfrom sklearn.metrics import accuracy_scorefrom sklearn.model_selection import train_test_splitfrom sklearn.tree import DecisionTreeClassifierfrom sklearn.ensemble import RandomForestClassifier,AdaBoostClassifier,GradientBoostingClassifier# Step1: Create data set# Define the headers since the data does not have anyheaders = ["symboling", "normalized_losses", "make", "fuel_type", "aspiration", "num_doors", "body_style", "drive_wheels", "engine_location", "wheel_base", "length", "width", "height", "curb_weight", "engine_type", "num_cylinders", "engine_size", "fuel_system", "bore", "stroke", "compression_ratio", "horsepower", "peak_rpm", "city_mpg", "highway_mpg", "price"]# Read in the CSV file and convert "?" to NaNdf = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data", header=None, names=headers, na_values="?" )df.head()df.columnsdf_fin = pd.DataFrame({col: df[col].astype('category').cat.codes for col in df}, index=df.index)df_finX = df_fin[['symboling', 'normalized_losses', 'make', 'fuel_type', 'aspiration', 'num_doors', 'body_style', 'drive_wheels', 'engine_location', 'wheel_base', 'length', 'width', 'height', 'curb_weight', 'engine_type', 'num_cylinders', 'engine_size', 'fuel_system', 'bore', 'stroke', 'compression_ratio', 'horsepower', 'peak_rpm']]y = df_fin['city_mpg']X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)# Fit a Decision Tree modelclf = DecisionTreeClassifier()clf.fit(X_train, y_train)y_pred = clf.predict(X_test)accuracy_score(y_test, y_pred)
现在,如何根据自变量(独立变量)对目标变量(因变量)进行预测呢?
我认为像这样的方法应该有效,但它不起作用…
clf.predict([[2,164,'audi','gas','std','four','sedan','fwd','front',99.8,176.6,66.2,54.3,2337,'ohc','four',109,'mpfi',3.19,3.4,10,102,5500,24,30,13950,]])
如果我们保持数值为数值,并在标签周围加上引号,我希望预测因变量,但由于标记数据的原因,我无法做到。如果数据全部是数值,并且这是一个回归问题,那它就会工作!!我的问题是,如何将分类代码转换回标记的数值数据,并进行预测?
回答:
您用来预测目标变量的输入数据需要与用于训练模型的数据格式相同。
我建议使用例如sklearn OneHotEncoder
(用于独热编码,但也有OrdinalEncoder
和LabelEncoder
等)对分类数据进行编码。这允许您首先在分类数据上fit()
预处理器,然后稍后使用它来transform()
您希望预测的数据。
使用独热编码的示例:
您可以使用已拟合编码器上的get_feature_names_out()
方法来获取列名数组。以下是基于上述示例的扩展:
df_encoded = pd.DataFrame(X_enc, columns=enc.get_feature_names_out())print(df_encoded) car_make_audi car_make_bmw car_make_renault car_country_DE car_country_FR0 1.0 0.0 0.0 1.0 0.01 0.0 1.0 0.0 1.0 0.02 0.0 1.0 0.0 1.0 0.03 0.0 0.0 1.0 0.0 1.0# getting our original values:df_orig = enc.inverse_transform(X_enc)print(df_orig)[['audi' 'DE'] ['bmw' 'DE'] ['bmw' 'DE'] ['renault' 'FR']]
如果您想将值转换回原始值,可以在编码数据上使用inverse_transform
来返回它们。
我建议查看文档以获取更多详细信息和使用案例:https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html#sklearn.preprocessing.OneHotEncoder
使用sklearn预处理器将为您节省很多未来的麻烦!