Fehler im Pipeline-Code in ScikitLearn mit PythonPython

Python-Programme
Guest
 Fehler im Pipeline-Code in ScikitLearn mit Python

Post by Guest »

Im folgenden Code der Pipeline. Obwohl ich die Spalte „Geschlecht“ codiert habe, erhalte ich den Fehler „String to Float“.

Code: Select all

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)
Fehler:
Image

Was ist der Grund für den Fehler?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post