我有以下代码:
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]
2条答案
按热度按时间dauxcl2d1#
在此行中,您为X选择了10个要素,因此X的形状现在已更改。
现在,您需要根据范围[0-9]给予'
numeric_features
'的索引。更具体地说,您在'numeric features
'中传递的索引应该反映此数组。此数组对于原始'
df
'是正确的:[8,9,10,11,12,13,15,16,17,18,19,20,21,26]
不是X
。zlwx9yxi2#
Yh. @Luis瓦伦西亚,我认为问题来自于一个热编码,它将为一个分类特征的所有唯一值创建新的列,这反过来又改变了输出的形状,使其与您提供的输入不同。因此,除非您愿意更改编码类型,否则必须在不使用管道的情况下预处理分类数据。
例如:
encoder = OneHotEncoder(sparse_output=False)
encoded = encoder.fit_transform(target_train)