python - test_train_split 将字符串类型标签转换为 np.array。有没有办法找回原来的标签名称?
问题描述
我有一个带有字符串类型标签名称的图像数据集。当我使用 sklearn 库的 test_train_split 拆分数据时,它将标签转换为 np.array 类型。有没有办法取回原始字符串类型标签名称?
以下代码拆分数据以进行训练和测试:
imgs, y = load_images()
train_img,ytrain_img,test_img,ytest_img = train_test_split(imgs,y, test_size=0.2, random_state=1)
如果我打印 y,它会给我标签名称,但如果我打印拆分后的标签值,它会给出一个数组:
for k in y:
print(k)
break
for k in ytrain_img:
print(k)
break
输出:
001.Affenpinscher
[[[ 97 180 165]
[ 93 174 159]
[ 91 169 152]
...
[[ 88 171 156]
[ 88 170 152]
[ 84 162 145]
...
[130 209 222]
[142 220 233]
[152 230 243]]
[[ 99 181 163]
[ 98 178 161]
[ 92 167 151]
...
[130 212 224]
[137 216 229]
[143 222 235]]
...
[[ 85 147 158]
[ 85 147 158]
[111 173 184]
...
[227 237 244]
[236 248 250]
[234 248 247]]
[[ 94 154 166]
[ 96 156 168]
[133 194 204]
...
[226 238 244]
[237 249 253]
[237 252 254]]
...
[228 240 246]
[238 252 255]
[241 255 255]]]
有没有办法将数组转换回原始标签名称?
解决方案
不,你在推断train_test_split
错误的输出。
train_test_split
以这种方式工作:
A_train, A_test, B_train, B_test, C_train, C_test ...
= train_test_split(A, B, C ..., test_size=0.2)
您可以提供尽可能多的数组进行拆分。对于每个给定的数组,它将首先提供训练和测试拆分,然后对下一个数组执行相同的操作,然后是第三个数组,依此类推..
所以在你的情况下实际上是:
train_img, test_img, ytrain_img, ytest_img = train_test_split(imgs, y,
test_size=0.2,
random_state=1)
但是您随后会混淆输出的名称并错误地使用它们。
推荐阅读
- flutter - 如何在列内实现多个水平列表
- python - 如何使用 python-socketio 从客户端访问我的套接字 ID?
- html - 引导切换在 iPhone X Safari 上不起作用
- java - 以编程方式更改语言环境不适用于 Android 6.0.1
- nameko - 实现(抽象)Nameko 服务继承
- html - 在 CSS 中 ID 末尾带有奇数/偶数的目标 div
- php - 如何预览和下载超过 12 MB 的 php 文件?
- c# - 带有 Web API 的角度
- mongodb - 查找和展开mongodb后如何防止重复总和
- java - POST 请求适用于 POSTMAN,但不适用于我的 Java 应用程序