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

Viewed 1006

I have the following code:

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

Then I have this:

# 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')

then I split X,Y

from sklearn.model_selection import 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]))

Then I want to transform numeric and categorial features:

# 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)

However I get this error:

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

In this line, you are selecting 10 features for X, so the shape of X is changed now.

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

Now, you need to give indexes of 'numeric_features' according to the range [0-9]. To be more specific, the indexes you are passing in 'numeric features' should reflect this array.

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

This array is correct for the origial 'df': [8,9,10,11,12,13,15,16,17,18,19,20,21,26] not for X.

Related