【问题标题】:How to make single prediction from bagging model如何从 bagging 模型进行单一预测
【发布时间】:2020-01-25 04:33:24
【问题描述】:

我有以下代码行:

# Setting the values ​​for the number of folds

num_folds = 10
seed = 7

# Separating data into folds

kfold = KFold(num_folds, True, random_state = seed)

# Create the unit model (classificador fraco)

cart = DecisionTreeClassifier()

# Setting the number of trees

num_trees = 100

# Creating the bagging model

model = BaggingClassifier(base_estimator = cart, n_estimators = num_trees, random_state = seed)

# Cross Validation

resultado = cross_val_score(model, X, Y, cv = kfold)

# Result print

print("Acurácia: %.3f" % (resultado.mean() * 100))

这是我从网上得到的现成代码,显然是为测试我的交叉验证的 TRAINING 数据和了解 bagging 算法的准确性而预定义的。

我想知道是否可以将其应用于我的 TEST 数据(没有输出“Y”的数据)

代码有点混乱,无法建模。

我正在寻找类似的东西:

# Training the model

model.fit(X, Y)

# Making predictions

Y_pred = model.predict(X_test)

我想在测试数据中的训练数据之上使用经过训练的 bagging 模型并进行预测,但我不知道如何修改代码

【问题讨论】:

  • 问题与scalabig-data 无关 - 请不要向无关标签发送垃圾邮件(已删除)。另外,请花一点时间看看如何正确格式化您的代码,这样 cmets 就不会在文本中显示为不必要的粗体内容(这次为您完成)。

标签: python scala machine-learning supervised-learning


【解决方案1】:

您已经拥有预测新数据的一切。我提供了一个带有玩具数据和 cmets 的小例子来说明清楚。

from sklearn.ensemble import BaggingClassifier

cart = BaggingClassifier()
X_train = [[0, 0], [1, 1]] # training data
Y_train = [0, 1] # training labels

cart.fit(X_train, Y_train) # model is trained

y_pred = cart.predict([ [0,1] ]) # new data
print(y_pred)

# prints [0], so it predicts the new sample (0,1) as 0 class

【讨论】:

    猜你喜欢
    • 2020-04-11
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    相关资源
    最近更新 更多