我正在尝试使用GradientBoostingClassifier检测SQL注入。
X = dataframe.as_matrix(['token_length','entropy','sqli_g_means','plain_g_means'])
# encode categorical feature
from sklearn.preprocessing import LabelEncoder
labelencoder_y = LabelEncoder()
y = labelencoder_y.fit_transform(dataframe['type'].tolist())
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state = 0)
from sklearn.ensemble import GradientBoostingClassifier
clf = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=7, random_state=0).fit(X_train, y_train)
print "Gradient Boosting Tree Acurracy: %f" % clf.score(X_test, y_test)训练模型时出错。
Traceback (most recent call last):
File "ml_sql_injection.py", line 136, in <module>
clf = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=7, random_state=0).fit(X_train, y_train)
File "/usr/local/lib/python2.7/dist-packages/sklearn/ensemble/gradient_boosting.py", line 1404, in fit
y = self._validate_y(y, sample_weight)
File "/usr/local/lib/python2.7/dist-packages/sklearn/ensemble/gradient_boosting.py", line 1968, in _validate_y
% n_trim_classes)
ValueError: y contains 1 class after sample_weight trimmed classes with zero weights, while a minimum of 2 classes are required.我如何修复这种类型的错误?
发布于 2018-12-25 05:12:55
虽然我猜已经太晚了,但我仍然想为其他人回答这个问题。
此错误是指y_train只包含1个值,即只有1个类可用于分类,但您至少需要2个。当您将数据集拆分为训练和测试时,y_train只剩下类。
发布于 2019-09-19 10:21:49
我也有这个问题。
这是一个很好的描述性错误:Y中的所有数据都有相同的标签。现在,在现实中,Y有很多标签,但是如果你输入y的一个子样本(或者如果它在模型构建器内部使用一个样本),那么这个选择可能都有相同的标签。
我通过shuffling修复了它,它可靠地解决了这个提升错误。
对于我在Pandas中的特定数据,如下所示:
x = pd.read_csv(filename, delimiter=",", header=None)
x = x.sample(frac=1) # shuffle -> fixes boosting errors
y = x.iloc[:,[0]] #extract label from last column
x = x.drop([0],axis=1) #drop last column from X这也给我带来了巨大而可靠的估计器改进:


https://stackoverflow.com/questions/52877658
复制相似问题