所有特征必须在 [0, 9] 或 [-10, 0] 中

时间:2021-01-19 10:26:03

标签: python python-3.x pandas dataframe

我有以下代码:

df = load_data()
pd.set_option('display.max_columns', None)
df.dtypes

intBillID                      object
chBillChargeCode               object
chBillNo                       object
chOriginalBillNo               object
sdBillDate             datetime64[ns]
sdDueDate              datetime64[ns]
sdDatePaidCancelled    datetime64[ns]
sdBillCancelledDate            object
totalDaysToPay                  int64
paidInDays                      int64
paidOnTime                      int64
chBillStatus                   object
chBillType                     object
chDebtorCode                   object
chBillGroupCode                 int64
dcTotFeeBilledAmt             float64
dcFinalBillExpAmt             float64
dcTotProgBillAmt              float64
dcTotProgBillExpAmt           float64
dcReceiveBillAmt              float64
dcTotWipHours                 float64
dcTotWipTargetAmt             float64
vcReason                       object
OperatingUnit                  object
BusinessUnit                   object
LosCode                        object
dcTotNetBillAmt               float64
dtype: object

然后我有这个:

# Separate features and labels
X, y = df[['totalDaysToPay', 'paidOnTime','dcTotFeeBilledAmt','dcFinalBillExpAmt','dcTotProgBillAmt', 'dcTotProgBillExpAmt','dcTotProgBillExpAmt','dcReceiveBillAmt','dcTotWipHours','dcTotWipTargetAmt']].values, df['paidInDays'].values
print('Features:',X[:10], '\nLabels:', y[:10], sep='\n')

然后我拆分 X,Y

从 sklearn.model_selection 导入 train_test_split

# Split data 70%-30% into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=0)

print ('Training Set: %d rows\nTest Set: %d rows' % (X_train.shape[0], X_test.shape[0]))

然后我想转换数字和类别特征:

# Train the model
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.linear_model import LinearRegression
import numpy as np
from sklearn.ensemble import GradientBoostingRegressor

# Define preprocessing for numeric columns (scale them)
numeric_features = [8,9,10,11,12,13,15,16,17,18,19,20,21,26]
numeric_transformer = Pipeline(steps=[
    ('scaler', StandardScaler())])

# Define preprocessing for categorical features (encode them)
categorical_features = [1,23,24,25]
categorical_transformer = Pipeline(steps=[
    ('onehot', OneHotEncoder(handle_unknown='ignore'))])

# Combine preprocessing steps
preprocessor = ColumnTransformer(
    transformers=[
        ('num', numeric_transformer, numeric_features),
        ('cat', categorical_transformer, categorical_features)])

# Create preprocessing and training pipeline
pipeline = Pipeline(steps=[('preprocessor', preprocessor),
                           ('regressor', GradientBoostingRegressor())])


# fit the pipeline to train a linear regression model on the training set
model = pipeline.fit(X_train, (y_train))
print (model)

但是我收到此错误:

ValueError: all features must be in [0, 9] or [-10, 0]

1 个答案:

答案 0 :(得分:2)

在这一行中,您为 X 选择了 10 个特征,因此现在更改了 X 的形状。

# Separate features and labels
X, y = df[['totalDaysToPay', 'paidOnTime','dcTotFeeBilledAmt','dcFinalBillExpAmt','dcTotProgBillAmt', 'dcTotProgBillExpAmt','dcTotProgBillExpAmt','dcReceiveBillAmt','dcTotWipHours','dcTotWipTargetAmt']].values, df['paidInDays'].values

现在,您需要根据范围 [0-9] 给出 'numeric_features' 的索引。 更具体地说,您在“numeric features”中传递的索引应该反映这个数组。

['totalDaysToPay', 'paidOnTime','dcTotFeeBilledAmt','dcFinalBillExpAmt','dcTotProgBillAmt', 'dcTotProgBillExpAmt','dcTotProgBillExpAmt','dcReceiveBillAmt','dcTotWipHours','dcTotWipTargetAmt']

此数组对于原始“df”是正确的:[8,9,10,11,12,13,15,16,17,18,19,20,21,26] 不适用于 X

相关问题