python - 处理要预测的元组中的分类值
问题描述
我正在使用经过训练的 sklearn 模型构建 API。我已将模型保存为 .joblib 格式,并在进行预测之前将其加载到 API 后端。但问题是我的数据包含分类列,我get_dummies()
在使用库中的方法对这些分类列进行一次热编码后训练了我的模型pandas
。我的 API 接收带有分类列值的 JSON 数据,没有任何编码。在将元组传递给模型之前,我应该如何对要预测的元组进行编码?有人可以帮我吗?谢谢你。
我使用的数据集在编码前后有以下一组列:
前:
Index(['age', 'sex', 'cp', 'trestbps', 'chol', 'fbs', 'restecg', 'thalach',
'exang', 'oldpeak', 'slope', 'ca', 'thal'],
dtype='object')
后:
Index(['age', 'sex', 'trestbps', 'chol', 'fbs', 'restecg', 'thalach', 'exang','oldpeak', 'ca', 'cp_0', 'cp_1', 'cp_2', 'cp_3', 'thal_0',
'thal_1', 'thal_2', 'thal_3', 'slope_0', 'slope_1', 'slope_2'],
dtype='object')
解决方案
在你的 get_dummies on predict 之后试试这个
df.reindex(columns=features, fill_value=0)
features
功能名称列表在哪里
推荐阅读
- c++ - 挂起/恢复来自不同进程中另一个线程的线程或进程
- html - 如何保持 input:checkbox 和 a 标签在同一行?
- c - 尝试根据 C 中的增量分配 char[] 的值
- php - 换行符在 Laravel Blade.php 中不起作用
- thread-safety - 线程是否在服务器方法的请求之间重用?
- android - 如何将firestore的新Timestamp对象类型存储在android的本地数据库Room中?
- spyder - 32 位 Anaconda 可以安装 64 位 Spyder 吗?
- c# - 如何在本地管理 Azure 持久功能任务中心?
- cassandra - 如何查看 cassandra 数据库中存在的 thingsboard 数据
- r - nls 函数和幂最小二乘函数有什么区别?