Python 的 Xgoost:ValueError('feature_names may not contain [, ] or <')
Python's Xgoost: ValueError('feature_names may not contain [, ] or <')
Python 的 XGBClassifier does not accept 字符 [, ] or <'
作为特征名称的实现。
如果发生这种情况,它会引发以下内容:
ValueError('feature_names may not contain [, ] or <')
似乎显而易见的解决方案是传递等效的 numpy 数组,并完全删除列名,但如果他们没有这样做,那一定是有原因的。
XGBoost 对特征名称有什么用,简单地传递 Numpy 数组而不是 Pandas DataFrames 的缺点是什么?
我知道现在已经晚了,但在这里为可能遇到此问题的其他人写下这个答案。这是我遇到这个问题后发现的:
如果您的列名称具有符号 [ or ] or <
,通常会发生此错误。
这是一个例子:
import pandas as pd
import numpy as np
from xgboost.sklearn import XGBRegressor
# test input data with string, int, and symbol-included columns
df = pd.DataFrame({'0': np.random.randint(0, 2, size=100),
'[test1]': np.random.uniform(0, 1, size=100),
'test2': np.random.uniform(0, 1, size=100),
3: np.random.uniform(0, 1, size=100)})
target = df.iloc[:, 0]
predictors = df.iloc[:, 1:]
# basic xgb model
xgb0 = XGBRegressor(objective= 'reg:linear')
xgb0.fit(predictors, target)
上面的代码会抛出一个错误:
ValueError: feature_names may not contain [, ] or <
但是,如果您从 '[test1]'
中删除那些方括号,那么它就可以正常工作。下面是从列名称中删除 [, ] or <
的通用方法:
import re
import pandas as pd
import numpy as np
from xgboost.sklearn import XGBRegressor
regex = re.compile(r"\[|\]|<", re.IGNORECASE)
# test input data with string, int, and symbol-included columns
df = pd.DataFrame({'0': np.random.randint(0, 2, size=100),
'[test1]': np.random.uniform(0, 1, size=100),
'test2': np.random.uniform(0, 1, size=100),
3: np.random.uniform(0, 1, size=100)})
df.columns = [regex.sub("_", col) if any(x in str(col) for x in set(('[', ']', '<'))) else col for col in df.columns.values]
target = df.iloc[:, 0]
predictors = df.iloc[:, 1:]
# basic xgb model
xgb0 = XGBRegressor(objective= 'reg:linear')
xgb0.fit(predictors, target)
有关更多信息,请阅读 xgboost core.py 中的代码行:
xgboost/core.py。那是抛出错误的检查失败。
这是另一个正则表达式解决方案。
import re
regex = re.compile(r"\[|\]|<", re.IGNORECASE)
X_train.columns = [regex.sub("_", col) if any(x in str(col) for x in set(('[', ']', '<'))) else col for col in X_train.columns.values]
另一种解决方案:
X.columns = X.columns.str.translate("".maketrans({"[":"{", "]":"}","<":"^"}))
如果您有兴趣了解哪些是罪魁祸首:
X.columns[X.columns.str.contains("[\[\]<]")]
Python 的 XGBClassifier does not accept 字符 [, ] or <'
作为特征名称的实现。
如果发生这种情况,它会引发以下内容:
ValueError('feature_names may not contain [, ] or <')
似乎显而易见的解决方案是传递等效的 numpy 数组,并完全删除列名,但如果他们没有这样做,那一定是有原因的。
XGBoost 对特征名称有什么用,简单地传递 Numpy 数组而不是 Pandas DataFrames 的缺点是什么?
我知道现在已经晚了,但在这里为可能遇到此问题的其他人写下这个答案。这是我遇到这个问题后发现的:
如果您的列名称具有符号 [ or ] or <
,通常会发生此错误。
这是一个例子:
import pandas as pd
import numpy as np
from xgboost.sklearn import XGBRegressor
# test input data with string, int, and symbol-included columns
df = pd.DataFrame({'0': np.random.randint(0, 2, size=100),
'[test1]': np.random.uniform(0, 1, size=100),
'test2': np.random.uniform(0, 1, size=100),
3: np.random.uniform(0, 1, size=100)})
target = df.iloc[:, 0]
predictors = df.iloc[:, 1:]
# basic xgb model
xgb0 = XGBRegressor(objective= 'reg:linear')
xgb0.fit(predictors, target)
上面的代码会抛出一个错误:
ValueError: feature_names may not contain [, ] or <
但是,如果您从 '[test1]'
中删除那些方括号,那么它就可以正常工作。下面是从列名称中删除 [, ] or <
的通用方法:
import re
import pandas as pd
import numpy as np
from xgboost.sklearn import XGBRegressor
regex = re.compile(r"\[|\]|<", re.IGNORECASE)
# test input data with string, int, and symbol-included columns
df = pd.DataFrame({'0': np.random.randint(0, 2, size=100),
'[test1]': np.random.uniform(0, 1, size=100),
'test2': np.random.uniform(0, 1, size=100),
3: np.random.uniform(0, 1, size=100)})
df.columns = [regex.sub("_", col) if any(x in str(col) for x in set(('[', ']', '<'))) else col for col in df.columns.values]
target = df.iloc[:, 0]
predictors = df.iloc[:, 1:]
# basic xgb model
xgb0 = XGBRegressor(objective= 'reg:linear')
xgb0.fit(predictors, target)
有关更多信息,请阅读 xgboost core.py 中的代码行: xgboost/core.py。那是抛出错误的检查失败。
这是另一个正则表达式解决方案。
import re
regex = re.compile(r"\[|\]|<", re.IGNORECASE)
X_train.columns = [regex.sub("_", col) if any(x in str(col) for x in set(('[', ']', '<'))) else col for col in X_train.columns.values]
另一种解决方案:
X.columns = X.columns.str.translate("".maketrans({"[":"{", "]":"}","<":"^"}))
如果您有兴趣了解哪些是罪魁祸首:
X.columns[X.columns.str.contains("[\[\]<]")]