Fixing ValueError: feature_names must not contain [, ], or < in XGBoost

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

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

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

2026年10月30日 から。

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

評価

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

調査の方向性

このissueには質問とPythonの例を含む回答案がありますが、追加先のファイルやテストは指定されていません。まず、リポジトリ内の既存のFAQ項目がどのように整理されているかを確認し、その後、適切な場所にこのQ&Aを追加してください。項目で機能名に使用できない文字を説明し、ドキュメントに記載されたクリーンアップの例を示せば完了です。

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

説明

faq-proposal
Course

machine-learning-zoomcamp

Question

When creating a DMatrix for XGBoost in Module 6 using the features outputted by DictVectorizer:
dtrain = xgb.DMatrix(X_train, label=y_train, feature_names=dv.get_feature_names_out())
I get this error:
ValueError: feature_names must not contain [, ], or <
How do I fix this?

Answer

This error occurs because XGBoost has strict rules for feature names and does not allow certain characters (like [, ], <, or =). When using DictVectorizer with text or categorical columns, it often generates feature names containing these exact characters (e.g., origin=USA).

To resolve this, you must clean the feature names by replacing the invalid characters before passing them to DMatrix. You can do this easily using Python's re (regex) module:

import re
import xgboost as xgb

# 1. Get original feature names from DictVectorizer
feature_names = dv.get_feature_names_out().tolist()

# 2. Replace [, ], <, >, and = with underscores
clean_feature_names = [re.sub(r'[\[\]<>=]', '_', f) for f in feature_names]

# 3. Create the DMatrix using the clean names
dtrain = xgb.DMatrix(X_train, label=y_train, feature_names=clean_feature_names)
dval = xgb.DMatrix(X_val, label=y_val, feature_names=clean_feature_names)

### 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
スター
8
フォーク
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 を短くまとめたダイジェスト。