我正在尝试运行梯度提升分类器

时间:2018-11-06 00:59:04

标签: python-3.x jupyter-notebook

from sklearn.ensemble import GradientBoostingRegressor

gbrt = GradientBoostingRegressor(max_depth=2, n_estimators=3, learning_rate=1.0)
gbrt.fit(X, y)

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
X_train, X_val, y_train, y_val = train_test_split(X, y)

gbrt = GradientBoostingRegressor(max_depth=2, n_estimators=120)
gbrt.fit(X_train, y_train)

errors = [mean_sqaured_error(y_val, y_pred)
     for y_pred in gbrt.staged_predict(X_val)]
bst_n_estimators = np.argim(errors)

gbrt_best = GradientBoostingRegressor(max_depth = 2, n_estimators = bst_n_estimators)
gbrt_best.fit(X_train, y_train)

运行此代码时,出现以下错误

ValueError: could not convert string to float: '<=50K'

我正在使用以下数据

https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data

在提升分类器之后,我想检查曲线下面积的性能提升,但是上述错误需要首先解决

1 个答案:

答案 0 :(得分:0)

基于提供的代码和数据预览,发生ValueError,因为您正在将字符串值/分类数据输入到GBM模型中。建议先进行一次热编码(http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html)或pd.get_dummies(https://pandas.pydata.org/pandas-docs/stable/generated/pandas.get_dummies.html),然后再拟合模型。

有关ROC曲线,请签出:http://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html#sphx-glr-auto-examples-model-selection-plot-roc-py。该示例应该非常简单,可以满足您的需求。

df = pd.read_csv(['PLEASE SPECIFY YOUR FILE PATH'], thousands = ',')
df.columns = ['V' + str(col) for col in df.columns]
list_cat = ['V1', 'V3', 'V4', 'V5', 'V6', 'V7', 'V8', 'V9', 'V13', 'V14']
list_target = ['V0']
df = pd.get_dummies(df, columns = list_cat, drop_first = True)
X = df.loc[:, df.columns != list_target[0]].values
y = df[list_target].values
print(df.shape)
df.head()

import numpy as np
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

gbrt = GradientBoostingRegressor(max_depth=2, n_estimators=3, learning_rate=1.0)
gbrt.fit(X, y)

enter image description here enter image description here