python - 如何在 keras 中使用有序分类列(“无法将字符串转换为浮点数:'CATEGORY'”)
问题描述
我正在参与Kaggle 房价竞赛,数据集有很多分类数据。我正在尝试将它们设置为这样的有序类别:
for col in ordered_category_rating_cols:
data[col] = data[col].astype(pd.api.types.CategoricalDtype(ordered=True, categories = ["GLQ", "ALQ", "BLQ", "Rec", "LwQ", "Unf", "NA"]))
但是,当我将数据传递到model.fit()
is 时会引发此错误(完整堆栈如下):
ValueError:无法将字符串转换为浮点数:'GLQ'
通过删除一堆列,我将其缩小到一个 - 但如果我dtype
为此打印,它看起来是正确的:
> train_x["BsmtFinType1"].dtype
> CategoricalDtype(categories=['GLQ', 'ALQ', 'BLQ', 'Rec', 'LwQ', 'Unf', 'NA'], ordered=True)
我已经搜索了高低,但找不到任何解决方案。我需要做些什么来告诉 Keras 将类别视为浮动吗?
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-144-c86afee8eb19> in <module>()
4 batch_size=128,
5 epochs=6,
----> 6 validation_split=0.1
7 )
8
3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
778 validation_steps=validation_steps,
779 validation_freq=validation_freq,
--> 780 steps_name='steps_per_epoch')
781
782 def evaluate(self,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training_arrays.py in model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq, mode, validation_in_fit, prepared_feed_values_from_dataset, steps_name, **kwargs)
361
362 # Get outputs.
--> 363 batch_outs = f(ins_batch)
364 if not isinstance(batch_outs, list):
365 batch_outs = [batch_outs]
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py in __call__(self, inputs)
3275 tensor_type = dtypes_module.as_dtype(tensor.dtype)
3276 array_vals.append(np.asarray(value,
-> 3277 dtype=tensor_type.as_numpy_dtype))
3278
3279 if self.feed_dict:
/usr/local/lib/python3.6/dist-packages/numpy/core/numeric.py in asarray(a, dtype, order)
536
537 """
--> 538 return array(a, dtype, copy=False, order=order)
539
540
ValueError: could not convert string to float: 'GLQ'
解决方案
我用来将分类列转换为数据的方式
import pandas as pd
df = pd.DataFrame(data={"gender":["male","female"]})
df['gender'] = df['gender'].astype('category').cat.codes
gender
0 1
1 0
如果多列包含分类数据
category_columns = list(df.select_dtypes(['category']).columns)
df[category_columns] = df[category_columns].apply(lambda x: x.cat.codes)
推荐阅读
- wso2 - WSO2 在 MYSQL 数据库中使用请求者 IP 保存每个请求和响应
- vue.js - 如何在 vue-table-2 中显示列的计算值
- sql - 替换函数不适用于 ORACLE/PLSQL 中的长查询?
- linker - 为什么链接 .so 文件需要 -ldl 而 .dylib 文件不需要?
- php - PHP MySQL:如果API数据中不存在则删除行
- php - 如何使用 PHP 规范化 SQL 列中的电话号码?
- javascript - JS Currying 一个静态函数
- nginx - Nginx 代理通行证与 Curl 一起使用,而不是与浏览器一起使用
- php - 无法在 Windows 上加载 PHP 的事件扩展 php_event.dll
- sql - sql怎么写选择哪里