Fixing ValueError: This solver needs samples of at least 2 classes in Logistic Regression

オープン 初心者向け
#532 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@github-actions がすでに取り組んでいます。

2026年10月30日 から。

  • #533 @github-actions による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
62/100
issue の種類
ドキュメント
明瞭さ
おおむね明確
活発さ
活発
技術スタック
python
領域
documentation

調査の方向性

Issueには質問、説明、Pythonの例が示されていますが、ファイルやテストは指定されていません。まず、このリポジトリでFAQ項目をどのように保存しているか、この回答をそこに追加すべきかを確認してください。説明と例がリポジトリのFAQ形式で利用できる状態になれば完了です。

索引モデルが issue の本文から書いたものです。

説明

faq-proposal
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:

  1. 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.
  2. Target deleted too early: The target column was dropped from the DataFrame (e.g., del df_train['converted']) before its values were extracted into y_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
主要言語
Python
スター
7
フォーク
20
平均マージ
7日 9時間
マージ済み PR(30日)
8

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

DataTalksClub/faq のほかの issue

DataTalksClub/faq の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。