๐Ÿ€Zerve chosen as NCAA's Agentic Data Platform for 2026 Hackathonยท๐ŸงฎMeet the Zerve Team at Data Decoded Londonยท๐Ÿ“ˆWe're hiring โ€” awesome new roles just gone live!
Back
Scikit Learn

ValueError: Could Not Convert String to Float (Sklearn Encoding) - How to Fix It

Answer

This sklearn error means you're passing categorical or text data to a model that expects numeric input. Fix it by encoding categorical columns using LabelEncoder, OneHotEncoder, or pandas get_dummies() before fitting your model.

Why This Happens

Sklearn models work with numeric arrays only. When your DataFrame contains columns like "red", "blue", "green" or "yes", "no", sklearn can't process them directly. Unlike some other ML libraries, sklearn doesn't auto-encode categorical variables. You must convert them yourself.

Solution

The rule: check for object dtype columns before fitting. Use get_dummies() for quick one-hot encoding, LabelEncoder for ordinal data, or ColumnTransformer for complex preprocessing pipelines.

import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer

df = pd.DataFrame({
    'color': ['red', 'blue', 'green', 'red'],
    'size': ['S', 'M', 'L', 'M'],
    'price': [10, 20, 30, 15]
})
y = [0, 1, 1, 0]

# โŒ Problematic: passing strings to sklearn
model = RandomForestClassifier()
model.fit(df, y)
# ValueError: could not convert string to float: 'red'

# โœ… Fix 1: pandas get_dummies (one-hot encoding)
df_encoded = pd.get_dummies(df, columns=['color', 'size'])
model.fit(df_encoded, y)

# โœ… Fix 2: LabelEncoder (for ordinal categories)
le = LabelEncoder()
df['color_encoded'] = le.fit_transform(df['color'])
df['size_encoded'] = le.fit_transform(df['size'])
model.fit(df[['color_encoded', 'size_encoded', 'price']], y)

# โœ… Fix 3: ColumnTransformer (most flexible)
preprocessor = ColumnTransformer(
    transformers=[
        ('cat', OneHotEncoder(handle_unknown='ignore'), ['color', 'size']),
        ('num', 'passthrough', ['price'])
    ])

X_transformed = preprocessor.fit_transform(df)
model.fit(X_transformed, y)

# โœ… Fix 4: Identify which columns are causing issues
for col in df.columns:
    if df[col].dtype == 'object':
        print(f"String column: {col} - unique values: {df[col].unique()}")

# โœ… Fix 5: Full pipeline approach
from sklearn.pipeline import Pipeline

pipeline = Pipeline([
    ('preprocessor', preprocessor),
    ('classifier', RandomForestClassifier())
])
pipeline.fit(df, y)

Better Workflow

In Zerve, test multiple encoding strategies simultaneously in parallel branches: one-hot, label encoding, target encoding. Each runs on serverless compute without manual threading. Modify one approach without affecting others. The visual DAG shows your entire pipeline from data loading to preprocessing to model training. An aggregator block collects results from all approaches, making A/B testing trivial. The DAG structure serves as documentation, so anyone can see and reproduce your exact methodology.

Better workflow

Related Topics

Decision-grade data work

Explore, analyze and deploy your first project in minutes