correct this code: import xgboost as xgb import pandas as pd read by default 1st sheet…
- User
correct this code: import xgboost as xgb import pandas as pd
read by default 1st sheet of an excel file
X = pd.read_excel('DATA1.xlsx',sheet_name='X') X.drop(columns='Unnamed: 0',inplace=True) Y = pd.read_excel('DATA1.xlsx',sheet_name='Y') Y.drop(columns='Unnamed: 0',inplace=True) X['Y']=Y X.to_numpy() print(X) YPar = pd.read_excel('DATA1.xlsx',sheet_name='MAXMINY') MAXY=YPar['MAXY'][0] MINY=YPar['MINY'][0] XTR=X.iloc[0:45] XTS=X.iloc[46,] param = {'max_depth': 2, 'eta': 1, 'objective': 'reg:tweedie'} param['eval_metric'] = 'rmse' evallist = [(XTR, 'train'), (XTS, 'eval')] num_round = 10 bst = xgb.train(param, XTR, num_round, evallist) ypred = bst.predict(XTS)
error: /usr/local/lib/python3.10/dist-packages/xgboost/core.py:727: FutureWarning: Pass
evalsas keyword args. warnings.warn(msg, FutureWarning)
TypeError Traceback (most recent call last)
in <cell line: 5>() 3 evallist = [(XTR, 'train'), (XTS, 'eval')] 4 num_round = 10 ----> 5 bst = xgb.train(param, XTR, num_round, evallist) 6 ypred = bst.predict(XTS)
2 frames
/usr/local/lib/python3.10/dist-packages/xgboost/core.py in init(self, params, cache, model_file) 1653 for d in cache: 1654 if not isinstance(d, DMatrix): -> 1655 raise TypeError(f"invalid cache item: {type(d).name}", cache) 1656 1657 dmats = c_array(ctypes.c_void_p, [d.handle for d in cache])
TypeError: ('invalid cache item: DataFrame', [ x1 x2 x3 x4 x5 x6 Y 0 0.205682 0.57626
- Assistant
import xgboost as xgb import pandas as pd
read by default 1st sheet of an excel file
data = pd.read_excel('DATA1.xlsx', sheet_name='Sheet1') X = data.drop(columns='Unnamed: 0') Y = data['Y']
X_np = X.to_numpy()
print(X)
YPar = pd.read_excel('DATA1.xlsx', sheet_name='MAXMINY') MAXY = YPar['MAXY'][0] MINY = YPar['MINY'][0]
XTR = X.iloc[0:45] XTS = X.iloc[46,]
param = {'max_depth': 2, 'eta': 1, 'objective': 'reg:tweedie'} param['eval_metric'] = 'rmse'
evallist = [(XTR, 'train'), (XTS, 'eval')] num_round = 10
XTR = xgb.DMatrix(XTR, label=Y.iloc[0:45]) XTS = xgb.DMatrix(XTS, label=Y.iloc[46,])
bst = xgb.train(param, XTR, num_round, evallist) ypred = bst.predict(XTS)
- User
this error arises: XGBoostError: [13:02:18] /workspace/src/data/data.cc:501: Check failed: this->labels.Size() % this->num_row_ == 0 (1 vs. 0) : Incorrect size for labels. Stack trace: at the line XTS = xgb.DMatrix(XTS, label=Y.iloc[46,])
- Assistant
The XGB library requires the label parameter to have the same length as the data passed for the DMatrix. In the provided code snippet, the XTS DatFrame only contains one row, while the label contains multiple values. To resolve this issue, you can modify the way data is passed to the DMatrix constructor. Here's the updated code snippet that fixes this issue:
import xgboost as xgb import pandas as pd # read by default 1st sheet of an excel file data = pd.read_excel('DATA1.xlsx', sheet_name='Sheet1') X = data.drop(columns='Unnamed: 0') Y = data['Y'] X_np = X.to_numpy() print(X) YPar = pd.read_excel('DATA1.xlsx', sheet_name='MAXMINY') MAXY = YPar['MAXY'][0] MINY = YPar['MINY'][0] XTR = X.iloc[0:45] XTS = X.iloc[46,] param = {'max_depth': 2, 'eta': 1, 'objective': 'reg:tweedie'} param['eval_metric'] = 'rmse' evallist = [(XTR, 'train'), (XTS, 'eval')] num_round = 10 XTR = xgb.DMatrix(XTR, label=Y.iloc[0:45]) XTS = xgb.DMatrix(XTS) bst = xgb.train(param, XTR, num_round, evallist) ypred = bst.predict(XTS)In this updated code, we are passing the label only for the training data (XTR) and not for the evaluation data (XTS) as the evaluation dataset does not require labels for prediction.
- User
TypeError: ('invalid cache item: DataFrame', [<xgboost.core.DMatrix object at 0x7f75a0006200>, x1 x2 x3 x4 x5 x6 Y 0 0.205682 0.576266 0.000000 0.177285 0.286164 0.550050 1.000000 1 0.246281 0.603815 0.333333 0.195336 1.000000 0.286164 0.155981 2 0.192541 0.534004 0.333333 0.177764 0.155981 1.000000 0.728
- Assistant
The error seems to point out that the data type being passed to the XGBoost
DMatrixis incorrect. To resolve this issue, you can directly pass the DataFrame values for X and Y without mixing types. Here's an updated code snippet:import xgboost as xgb import pandas as pd # read by default 1st sheet of an excel file data = pd.read_excel('DATA1.xlsx', sheet_name='Sheet1') X = data.drop(columns='Unnamed: 0').astype(float) Y = data['Y'].astype(float) print(X) YPar = pd.read_excel('DATA1.xlsx', sheet_name='MAXMINY') MAXY = YPar['MAXY'][0] MINY = YPar['MINY'][0] XTR = X.iloc[0:45] XTS = X.iloc[46] param = {'max_depth': 2, 'eta': 1, 'objective': 'reg:tweedie'} param['eval_metric'] = 'rmse' evallist = [(XTR, 'train'), (XTS, 'eval')] num_round = 10 XTR = xgb.DMatrix(XTR, label=Y[0:45]) XTS = xgb.DMatrix(XTS) bst = xgb.train(param, XTR, num_round, evallist) ypred = bst.predict(XTS) print(ypred)In this code snippet, we explicitly convert the X and Y data to float types before creating the
DMatrix. This ensures that there is a consistent data type being passed to thexgb.DMatrixconstructor.