训练了模型,但如何手动预测新实例?

我有这个脚本来使用梯度提升算法进行预测。

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))

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注