GradientBoostingClassifier实现

时间:2019-02-15 12:40:02

标签: python scikit-learn

我想在基于sklearn库的Titanic ML解决方案中实现梯度提升分类器。

我在Ubuntu 18.04上使用VS Code。

我尝试过:

# Splitting the Training Data
from sklearn.model_selection import train_test_split

predictors = train.drop(['Survived', 'PassengerId'], axis=1)
target = train["Survived"]
x_train, x_val, y_train, y_val = train_test_split(predictors, 
target, test_size = 0.22, random_state = 0)

# Gradient Boosting Classifier
from sklearn.ensemble import GradientBoostingClassifier

gbk = GradientBoostingClassifier()
gbk.fit(x_train, y_train)

..它返回:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sj/anaconda3/lib/python3.7/site-packages/sklearn/ensemble/gradient_boosting.py", line 1395, in fit
    X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'], dtype=DTYPE)
  File "/home/sj/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py", line 756, in check_X_y
    estimator=estimator)
  File "/home/sj/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py", line 527, in check_array
    array = np.asarray(array, dtype=dtype, order=order)
  File "/home/sj/anaconda3/lib/python3.7/site-packages/numpy/core/numeric.py", line 501, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: 'Baby'

我们将不胜感激。我是DS的新手。

1 个答案:

答案 0 :(得分:0)

我认为您的火车数据中可能没有数值。您的分类器可以采用数字输入。这就是为什么它尝试将此处'Baby'的字符串转换为浮点数的原因。由于不支持此操作,因此失败。

也许再次查看您的数据。