Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何利用套袋模型进行单次预测_Python_Scala_Machine Learning_Supervised Learning - Fatal编程技术网

Python 如何利用套袋模型进行单次预测

Python 如何利用套袋模型进行单次预测,python,scala,machine-learning,supervised-learning,Python,Scala,Machine Learning,Supervised Learning,我有以下代码行: # 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() # Set

我有以下代码行:

# 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))
这是我从互联网上获得的现成代码,显然是为了测试我的交叉验证训练数据和了解bagging算法的准确性而预定义的

我想知道我是否可以在不输出“Y”的情况下将其应用于我的测试数据

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

我要找的东西是:

# Training the model

model.fit(X, Y)

# Making predictions

Y_pred = model.predict(X_test)

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

您已经拥有了预测新数据的一切。我提供了一个玩具数据和评论的小例子,以使它清楚

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

这个问题与scala或大数据无关——请不要删除不相关的垃圾标签。另外,请花一分钟来看看如何正确地格式化您的代码,这样注释就不会在文本中显示为不必要的粗体内容,这一次是为您完成的。