Anonymous
Ошибка в коде конвейера в ScikitLearn с использованием Python
Сообщение
Anonymous » 19 янв 2025, 22:31
В приведенном ниже коде конвейера. Несмотря на то, что я закодировал столбец пола, я получаю сообщение об ошибке плавающей строки.
Код: Выделить всё
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder, MinMaxScaler
from sklearn.tree import DecisionTreeClassifier
import numpy as np
# Step 1: Imputation
trf1 = ColumnTransformer([
('impute_age', SimpleImputer(), [2]), # Impute Age
('impute_embarked', SimpleImputer(strategy='most_frequent'), [6]) # Impute Embarked
], remainder='passthrough')
# Step 2: One-Hot Encoding
trf2 = ColumnTransformer([
('onehot_sex_embarked', OneHotEncoder(sparse_output=False, handle_unknown='ignore'), [1, 6]) # Encode Sex and Embarked
], remainder='passthrough')
# Step 3: Scaling
trf3 = ColumnTransformer([
('scale', MinMaxScaler(), slice(0, None)) # Scale all columns
], remainder='passthrough')
# Step 4: Classifier
trf4 = DecisionTreeClassifier()
# Create pipeline
pipe = Pipeline([
('trf1', trf1), # Step 1: Imputation
('trf2', trf2), # Step 2: One-hot encoding
('trf3', trf3), # Step 3: Scaling
('trf4', trf4) # Step 4: Model
])
# Ensure proper ha
# Fit the pipeline
pipe.fit(X_train, y_train)
Ошибка:
В чем причина ошибки?
Подробнее здесь:
https://stackoverflow.com/questions/793 ... ing-python
1737315064
Anonymous
В приведенном ниже коде конвейера. Несмотря на то, что я закодировал столбец пола, я получаю сообщение об ошибке плавающей строки. [code]from sklearn.compose import ColumnTransformer from sklearn.pipeline import Pipeline from sklearn.impute import SimpleImputer from sklearn.preprocessing import OneHotEncoder, MinMaxScaler from sklearn.tree import DecisionTreeClassifier import numpy as np # Step 1: Imputation trf1 = ColumnTransformer([ ('impute_age', SimpleImputer(), [2]), # Impute Age ('impute_embarked', SimpleImputer(strategy='most_frequent'), [6]) # Impute Embarked ], remainder='passthrough') # Step 2: One-Hot Encoding trf2 = ColumnTransformer([ ('onehot_sex_embarked', OneHotEncoder(sparse_output=False, handle_unknown='ignore'), [1, 6]) # Encode Sex and Embarked ], remainder='passthrough') # Step 3: Scaling trf3 = ColumnTransformer([ ('scale', MinMaxScaler(), slice(0, None)) # Scale all columns ], remainder='passthrough') # Step 4: Classifier trf4 = DecisionTreeClassifier() # Create pipeline pipe = Pipeline([ ('trf1', trf1), # Step 1: Imputation ('trf2', trf2), # Step 2: One-hot encoding ('trf3', trf3), # Step 3: Scaling ('trf4', trf4) # Step 4: Model ]) # Ensure proper ha # Fit the pipeline pipe.fit(X_train, y_train) [/code] Ошибка: [img]https://i.sstatic.net/TMvRgQBJ.png [/img] В чем причина ошибки? Подробнее здесь: [url]https://stackoverflow.com/questions/79369011/error-in-pipeline-code-in-scikitlearn-using-python[/url]