我有这个脚本来使用梯度提升算法进行预测。
if GB_flag: gb_clf = GradientBoostingClassifier(n_estimators=20, learning_rate=0.5, max_features=2, max_depth=2, random_state=0) gb_clf.fit(x_train, y_train) cross_val_score(gb_clf, x_train, y_train, cv=3, scoring="accuracy") y_test_pred_gb = cross_val_predict(gb_clf, x_test, y_test, cv=3) predictions = gb_clf.predict(x_test) score = gb_clf.score(x_test, y_test) y_test_pred = y_test_pred_gb
现在我想手动测试这个模型对新数组值的预测,该如何操作?假设我想预测数组[12,44,0]的结果。请指教。
回答:
predict 函数接受形状为 (n_samples, n_features) 的数组。我猜测在你的情况下n_features=3
,因此:
print('你对 [12,44,0] 的预测是', gb_clf.predict([[12,44,0]])
示例:
from sklearn.datasets import make_hastie_10_2from sklearn.ensemble import GradientBoostingClassifierX, y = make_hastie_10_2(random_state=0) # 有10个特征X_train, X_test = X[:2000], X[2000:]y_train, y_test = y[:2000], y[2000:]clf = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0).fit(X_train, y_train)sample = [list(range(10))] # 我们为预测提供10个特征print('预测结果:', clf.predict(sample))