简体   繁体   中英

How to make single prediction from bagging model

I have the following line of code:

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

This is a ready-made code that I got from the internet, which is obviously predefined for testing my cross-validated TRAINING data and knowing the accuracy of the bagging algorithm.

I would like to know if I can apply it to my TEST data (data without output 'Y')

The code is a bit confusing and I can't model it.

I'm looking for something like:

# Training the model

model.fit(X, Y)

# Making predictions

Y_pred = model.predict(X_test)

I want to use the trained bagging model on top of the training data in the test data and make predictions but I don't know how to modify the code

You have everything to predict new data already. I am providing a small example with toy data and comments to make it clear.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM