XGBoost的新手,请原谅我。我已经在Boston房屋数据集中训练了一个模型,并将其保存在本地。现在,我想加载模型,并使用结构相似的新数据集来预测其标签。我将如何在Python 3.6中执行此操作?到目前为止,我已经从培训步骤中获得了这个信息:
已更新,请尝试使用泡菜代替
更新2:添加了错误原因,即预处理。
更新3:请参见下面的评论
print('Splitting the features and label columns...')
X, y = data.iloc[:,:-1],data.iloc[:,-1]
print('Converting dataset to Dmatrix structure to use later on...')
data_dmatrix = xgb.DMatrix(data=X,label=y)
#....
# Some more stuff here.
#....
print('Now, train the model...')
grid = xgb.train(params=params, dtrain=data_dmatrix, num_boost_round=10)
# Now, save the model for later use on unseen data
import pickle
model = pickle.dump(grid, open("pima.pickle.dat", "wb"))
#.....after some time has passed
# Now, load the model for use on a new dataset
loaded_model = pickle.load(open("pima.pickle.dat", "rb"))
print(loaded_model.feature_names)
# Now, load a new dataset to run the model on and make predictions for
dataset = pd.read_csv('Boston Housing Data.csv', skiprows=1))
# Split the dataset into features and label
# X = use all rows, up until the last column, which is the label or predicted column
# y = use all rows in the last column of the dataframe ('Price')
print('Splitting the new features and label column up for predictions...')
X, y = dataset.iloc[:,:-1],dataset.iloc[:,-1]
# Make predictions on labels of the test set
preds = loaded_model.predict(X)
现在我得到了回溯:
preds = loaded_model.predict(X)
AttributeError: 'DataFrame' object has no attribute 'feature_names'
有什么想法吗?我注意到当我打印loading_model.feature_names时得到:
['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT']
...但是实际的.csv文件有一个额外的列“ PRICE”,该列在培训之前被添加并在培训期间用作标签。这意味着什么吗?
我认为我不必经历整个训练并测试拆分后的事情,因为我并不想真正地重新训练模型,只需在新数据集上使用它来进行预测并显示来自新数据集的实际值的RMSE。我在网上看到的所有教程都没有涉及对新数据实施模型的步骤。有什么想法吗?谢谢!
答案 0 :(得分:1)
您需要对测试集使用与训练集相同的预处理,以便进行任何类型的预测。您的问题是因为您在训练中使用了DMatrix结构,这是BTW所必需的。
print('Converting dataset to Dmatrix structure to use later on...')
data_dmatrix = xgb.DMatrix(data=X,label=y)
,但未能在测试集上使用该预处理。对所有训练集,验证集和测试集使用相同的预处理。您的模型将是金色的。