Fixing ValueError: This solver needs samples of at least 2 classes in Logistic Regression
Maintainers usually reply within 2 days
Nobody has claimed this yet.
- #533 by @github-actions — closed without merging
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 62/100
- Issue type
- Documentation
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- python
- Domain
- documentation
Research direction
The issue provides the question, explanation, and a Python example, but does not name a file or test. Start by checking how this repository stores FAQ entries and whether this answer belongs there; done means the explanation and example are available in the repository’s FAQ format.
Written by the indexing model from the issue text.
Description
Course
machine-learning-zoomcamp
Question
When training LogisticRegression on the course_lead_scoring_2026.csv dataset in Module 4, the model.fit(X_train, y_train) step fails with:
ValueError: This solver needs samples of at least 2 classes in the data, but the data contains only one class: np.int64(0)
How can I fix this?
Answer
This error occurs because y_train contains only zeros and is missing the positive class (1). This typically happens due to one of two common data preparation mistakes:
- Target overwritten during imputation: The target column (
converted) was accidentally included in the numerical features list and overwritten when executing.fillna(0.0)for missing values. - Target deleted too early: The target column was dropped from the DataFrame (e.g.,
del df_train['converted']) before its values were extracted intoy_train.
To resolve this, ensure you explicitly exclude the target column when defining your numerical features:
numerical = [c for c in df.select_dtypes(include=['number']).columns if c != 'converted']
### Checklist
- [x] I have searched existing FAQs and this question is not already answered
- [x] The answer provides accurate, helpful information
- [x] I have included any relevant code examples or links
- Dominant language
- Python
- Stars
- 8
- Forks
- 20
- Avg merge
- 4d 9h
- Merged PRs (30d)
- 27
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from DataTalksClub/faq
-
Two different inputs get the same XGBoost churn probability in my Flask API—does that mean my model is broken?Possibly taken @github-actions claimed this today. Openfaq-proposal
Difficulty 1/5 1-3 hours Newbie friendliness 85/100
DataTalksClub/faq#571 · 2 comments ·
Maintainers usually reply within 2 days
-
[FAQ] How do I use `GridSearchCV` with parallel processing, and how do I fix the scikit-learn parallel-processing warning?May be free again A pull request for this issue was closed without being merged. Openfaq-proposal
Difficulty 1/5 1-3 hours Newbie friendliness 86/100
DataTalksClub/faq#551 · 4 comments ·
Maintainers usually reply within 2 days
-
Difficulty 1/5 Under an hour Newbie friendliness 86/100
DataTalksClub/faq#550 · 1 comment ·
Maintainers usually reply within 2 days
-
[FAQ] Commit ID for ProjectsMay be free again A pull request for this issue was closed without being merged. Openfaq-proposal
Difficulty 1/5 Under an hour Newbie friendliness 84/100
DataTalksClub/faq#503 · 3 comments ·
Maintainers usually reply within 2 days
-
How can I verify that my deployed XGBoost model is using the correct saved `DictVectorizer`?Possibly taken @github-actions claimed this 1 day ago. Openfaq-proposal
Difficulty 1/5 Under an hour Newbie friendliness 1/100
DataTalksClub/faq#569 · 2 comments ·
Maintainers usually reply within 2 days
All issues in DataTalksClub/faq
Similar issues
-
release
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
MichaelYcJo/SpecSeal#1535 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
valentinfrlch/ha-llmvision#769 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 75/100
daniel-ospina/tortoise#11937 · 1 comment ·
Maintainers usually reply within 1 day
-
good first issue help wanted python up-for-grabs
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
andrewsiah/agentswarm-data-tasks#238 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
cadence-workflow/cadence-ai-samples#175 ·
Maintainers usually reply within 1 day