ValueError: x имеет 7 функций, но Collontransformer ожидает 13 функцийPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 ValueError: x имеет 7 функций, но Collontransformer ожидает 13 функций

Сообщение Anonymous »

У меня есть следующий код, где я пытаюсь предсказать цену инструментов, для которых я использую регрессию Пуассона. < /p>

Код: Выделить всё

# --- Load and Prepare Data ---
y = train['PriceToday']
X = train.drop(columns=['PriceToday'])

# Define non-standard types
non_standard_types = ["Nar", "Orch", "Fru","Comp"]

# Create a flag feature for non-standard
X["Non_Standard_Flag"] = X["Type_LS"].isin(non_standard_types).astype(int)

# Identify numerical and categorical columns
num_features = ["AGE", "POWER", "Hours", "Non_Standard_Flag"]
cat_features = ["BRAND", "Country", "Final_Trans"]

# Define preprocessing pipeline
preprocessor = ColumnTransformer(
transformers=[
('num', StandardScaler(), num_features),
('cat', OneHotEncoder(handle_unknown='ignore'), cat_features)
], remainder="drop"
)

# --- Train/Test Split ---
# Create a weight column
train["sample_weight"] = train["Type_LS"].apply(lambda x: 1 if x == "Standard" else 5)

train["stratify_group"] = train["BRAND"].astype(str)
X_train, X_val, y_train, y_val, train_weights, val_weights = train_test_split(
X, y, train["sample_weight"], test_size=0.2, random_state=42, stratify=train["stratify_group"]
)
# Fit the preprocessor once on training data
X_train_preprocessed = preprocessor.fit_transform(X_train)
X_val_preprocessed = preprocessor.transform(X_val)

# Define models
models = {
"Poisson": PoissonRegressor(alpha=0.01)
}

# Train and evaluate models
model_results = {}

for model_name, model in models.items():
model.fit(X_train_preprocessed, y_train, sample_weight=train_weights)

# Predictions
predictions = model.predict(X_val_preprocessed)
# Calculate Metrics
r2 = r2_score(y_val, predictions)

model_results[model_name] = {
"model": model,
"R2": r2
}
< /code>
У меня есть тестовые данные, для которых я хочу сравнить цены на их с прогнозируемой ценой из модели.
Мои тестовые данные - это примерно так: < /p>
# Ensure new data has the correct format
new_data = pd.DataFrame({
"AGE": [12, 24, 36, 48, 60, 72, 84, 12, 24, 36, 48, 60, 72, 84],
"Hours": [500, 1000, 1500, 2000, 2500, 3000, 3500, 500, 1000, 1500, 2000, 2500, 3000, 3500],
"BRAND": ["NH"] * 14,
"POWER": [150] * 7 + [80] * 7,
"Final_Trans": ["Cv"] * 14,
"Country": ["DEU"] * 14,
"Type_LS": [Nar, Nar, Nar, ST, ST, ST, ST, ST,ST, ST, ST, ST, ST, ST]
"Current_Pred": [105614, 96681, 88504, 81018, 74165, 67892, 62150, 42608, 39728, 37043, 34540, 32206, 30029, 28000]
})
< /code>
Мой код: < /p>
new_df = pd.DataFrame(new_data)
# Create the 'Non_Standard_Flag'
new_df["Non_Standard_Flag"] = new_df["Type_LS"].isin(non_standard_types).astype(int)

# Select the columns required by the preprocessor
X_new = new_df[['AGE', 'POWER', 'Hours', 'Non_Standard_Flag', 'BRAND', 'Country', 'Final_Trans']]

X_new_preprocessed = preprocessor.transform(X_new)

# Get the column names after one-hot encoding from the training data
ohe = preprocessor.named_transformers_['cat']
encoded_cat_columns = ohe.get_feature_names_out(cat_features)

# Create column names for numeric features
num_columns = num_features

# Combine the column names
all_columns = num_columns + list(encoded_cat_columns)

# Create a DataFrame from the preprocessed data
X_new_preprocessed_df = pd.DataFrame(X_new_preprocessed, columns=all_columns)

# --- Predict with the Poisson Model ---
poisson_model = model_results["Poisson"]["model"]  # Access the trained Poisson model
predicted_prices = poisson_model.predict(X_new_preprocessed_df)

# Compare and Store Results ---
new_df['Predicted_Price'] = predicted_prices

# Calculate the difference between predicted and current prices
new_df['Price_Difference'] = new_df['Predicted_Price'] - new_df['Current_Pred']
< /code>
Но после этого я получу ошибку: < /p>
X has 7 features, but ColumnTransformer expects 13 features
У меня такое же количество столбцов, поэтому я не понимаю, почему у меня есть эта ошибка.

Подробнее здесь: https://stackoverflow.com/questions/794 ... 3-features
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»