[LightGBM] [Warning] Unknown parameter categorical_column= Traceback (most recent call last): File "C:\Users\86188\PycharmProjects\pythonProject8\src\lgbb.py", line 106, in <module> y_pred = lgb_model.predict(X_test) File "C:\Python37\lib\site-packages\lightgbm\sklearn.py", line 545, in predict return self._le.inverse_transform(class_index) File "C:\Python37\lib\site-packages\sklearn\preprocessing\_label.py", line 161, in inverse_transform raise ValueError("y contains previously unseen labels: %s" % str(diff)) ValueError: y contains previously unseen labels: [83394] 进程已结束,退出代码1
时间: 2023-06-24 10:04:27 浏览: 570
这个错误提示是因为你在 LightGBM 模型中使用了一个未知的参数 `categorical_column=`。这个参数在最新的 LightGBM 版本中已经被移除了。如果你想要使用分类特征,可以使用 `categorical_feature` 参数来指定它们的列索引或名称。另外,错误信息中的 `ValueError` 提示说明你的测试数据集中包含了未出现在训练数据集中的标签。你需要检查一下你的训练集和测试集的标签分布是否一致,如果不一致的话就需要进行处理。
相关问题
res=result; %% 划分训练集与测试集 P_train=res(1:30:21600,10)'; T_train=res(1:30:21600,11)'; M=size(P_train,2); P_test=res(21600:30:end,10)'; T_test=res(21600:30:end,11)'; N=size(P_test,2); %% 数据归一化 [P_train,ps_input]=mapminmax(P_train,0,1); P_test=mapminmax('apply',P_test,ps_input); t_train=categorical(T_train)'; t_test=categorical(T_test); %% 数据平铺 P_train=double(reshape(P_train,10,1,1,M)); P_test=double(reshape(P_test,10,1,1,N));错误使用 reshape 元素数不能更改。请使用 [] 作为大小输入之一,以自动计算该维度的适当大小。
这段代码中使用了错误的 reshape 函数。根据错误提示,元素数不能更改。正确的使用方法应该是将要改变的维度的大小设为 [],让 reshape 函数自动计算该维度的大小。修改后的代码如下:
```
res=result; %% 划分训练集与测试集
P_train=res(1:30:21600,10)';
T_train=res(1:30:21600,11)';
M=size(P_train,2);
P_test=res(21600:30:end,10)';
T_test=res(21600:30:end,11)';
N=size(P_test,2);
%% 数据归一化
[P_train,ps_input]=mapminmax(P_train,0,1);
P_test=mapminmax('apply',P_test,ps_input);
t_train=categorical(T_train)';
t_test=categorical(T_test);
%% 数据平铺
P_train=double(reshape(P_train,[],1,1,M));
P_test=double(reshape(P_test,[],1,1,N));
```
这样修改后,reshape 函数会自动计算要改变的维度的大小,从而避免出现错误。
Epoch 1/10 2023-07-22 21:56:00.836220: W tensorflow/core/framework/op_kernel.cc:1807] OP_REQUIRES failed at cast_op.cc:121 : UNIMPLEMENTED: Cast string to int64 is not supported Traceback (most recent call last): File "d:\AI\1.py", line 37, in <module> model.fit(images, labels, epochs=10, validation_split=0.2) File "D:\AI\env\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler raise e.with_traceback(filtered_tb) from None File "D:\AI\env\lib\site-packages\tensorflow\python\eager\execute.py", line 52, in quick_execute tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, tensorflow.python.framework.errors_impl.UnimplementedError: Graph execution error: Detected at node 'sparse_categorical_crossentropy/Cast' defined at (most recent call last): File "d:\AI\1.py", line 37, in <module> model.fit(images, labels, epochs=10, validation_split=0.2) File "D:\AI\env\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler return fn(*args, **kwargs) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1685, in fit tmp_logs = self.train_function(iterator) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1284, in train_function return step_function(self, iterator) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1268, in step_function outputs = model.distribute_strategy.run(run_step, args=(data,)) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1249, in run_step outputs = model.train_step(data) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1051, in train_step loss = self.compute_loss(x, y, y_pred, sample_weight) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1109, in compute_loss return self.compiled_loss( File "D:\AI\env\lib\site-packages\keras\engine\compile_utils.py", line 265, in __call__ loss_value = loss_obj(y_t, y_p, sample_weight=sw) File "D:\AI\env\lib\site-packages\keras\losses.py", line 142, in __call__ losses = call_fn(y_true, y_pred) File "D:\AI\env\lib\site-packages\keras\losses.py", line 268, in call return ag_fn(y_true, y_pred, **self._fn_kwargs) File "D:\AI\env\lib\site-packages\keras\losses.py", line 2078, in sparse_categorical_crossentropy return backend.sparse_categorical_crossentropy( File "D:\AI\env\lib\site-packages\keras\backend.py", line 5610, in sparse_categorical_crossentropy target = cast(target, "int64") File "D:\AI\env\lib\site-packages\keras\backend.py", line 2304, in cast return tf.cast(x, dtype) Node: 'sparse_categorical_crossentropy/Cast' Cast string to int64 is not supported [[{{node sparse_categorical_crossentropy/Cast}}]] [Op:__inference_train_function_1010]
这个错误是由于标签 `labels` 中的数据类型不正确导致的。`labels` 中的数据应该是整数类型,但是在您的代码中,它们被解析为字符串类型。为了解决这个问题,您需要将标签转换为整数类型。
请尝试在加载数据集时将标签转换为整数类型。修改以下部分的代码:
```python
for file in os.listdir(image_dir):
img_path = os.path.join(image_dir, file)
img = Image.open(img_path).convert('L') # 转换为灰度图像
img = img.resize((100, 100))
img = np.array(img, dtype=np.float32)
images.append(img)
labels.append(file.split('.')[0])
```
修改为:
```python
for file in os.listdir(image_dir):
img_path = os.path.join(image_dir, file)
img = Image.open(img_path).convert('L') # 转换为灰度图像
img = img.resize((100, 100))
img = np.array(img, dtype=np.float32)
images.append(img)
labels.append(int(file.split('.')[0]))
```
这样修改后,将标签转换为整数类型后,应该能够解决 `Cast string to int64 is not supported` 的问题。
阅读全文