我正在编写代码来解决一个简单的问题,即预测库存中物品丢失的概率。
我正在使用XGBoost预测模型来执行此操作。
我将数据拆分为两个 .csv 文件,一个包含训练数据,另一个包含测试数据
这是代码:
import pandas as pd
import numpy as np
train = pd.read_csv('C:/Users/pedro/Documents/Pedro/UFMG/8o periodo/Python/Trabalho Final/train.csv', index_col='sku').fillna(-1)
test = pd.read_csv('C:/Users/pedro/Documents/Pedro/UFMG/8o periodo/Python/Trabalho Final/test.csv', index_col='sku').fillna(-1)
X_train, y_train = train.drop('isBackorder', axis=1), train['isBackorder']
import xgboost as xgb
xg_reg = xgb.XGBRegressor(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1,
max_depth = 10, alpha = 10, n_estimators = 10)
xg_reg.fit(X_train,y_train)
y_pred = xg_reg.predict(test)
# Create file for the competition submission
test['isBackorder'] = y_pred
pred = test['isBackorder'].reset_index()
pred.to_csv('competitionsubmission.csv',index=False)
这是我尝试测量问题准确性的函数(使用 RMSE 和 accuracy_scores 函数并进行 KFold 交叉验证
#RMSE
from sklearn.metrics import mean_squared_error
rmse = np.sqrt(mean_squared_error(y_train, y_pred))
print("RMSE: %f" % (rmse))
#Accuracy
from sklearn.metrics import accuracy_score
# make predictions for test data
predictions = [round(value) for value in y_pred]
# evaluate predictions
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
#KFold
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
# CV model
kfold = KFold(n_splits=10, random_state=7)
results = cross_val_score(xg_reg, X_train, y_train, cv=kfold)
print("Accuracy: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
但我遇到了一些问题。
上述准确性测试均无效。
使用RMSE函数和Accuracy函数时,出现如下错误: ValueError: Found input variables with contrast numbers of samples: [1350955, 578982]
我猜我使用的训练和测试数据拆分结构不正确。
由于我没有 y_test (而且我不知道如何在我的问题中创建它),我不能在函数的上述参数中使用它。
K 折叠验证也不起作用。
有人能帮助我吗?