python - 如何在for循环Python中拆分数组
问题描述
我有以下数组:
prediction = [0 1 0 0 0 1 0 0 0 0 0 1 1 1 1 1 0 0 1 0]
我正在循环测试数据集中的图像,我想要的是每个图像,以获得相应的预测。例如,对于 image1,预测为 0,对于 image2,预测为 1,依此类推。
我一直在尝试这样的事情,我知道这是错误的,但它让你知道我想要什么:
clf = svm.SVC(kernel='linear', C=100)
for file in glob.glob(test_path + "/*.jpg"):
.
.
.
prediction = clf.predict(X_test)
for i in prediction:
prediction = prediction[i]
print(prediction)
(我省略了不相关的代码部分,但如果您需要查看它,我将编辑帖子并将其添加)
有没有办法做我要求的?
提前致谢!
解决方案
你可以做:
for index,file in enumerate(glob.glob(test_path + "/*.jpg")):
prediction[index] #your prediction for the indexth image
这适用于任何可迭代的:
for i, each in enumerate(iterable):
print('The'+str(i)+'th element in your iterable is'+str(each))
推荐阅读
- javascript - 垂直导航栏,垂直居中文本?
- c# - 如何更改用户的密码,在输入输入中保留 Configure IdentityOptions 选项
- python - 如何使用 Pyspark 将分数设置为 3 位数
- r - 计算R中按天分组的多列中的缺失数据
- mysql - 如何获取所有具有浪漫和喜剧的电影 ID 的计数?
- javascript - 使用 Playwright 获取列表元素的列表
- google-cloud-platform - 使用无服务器框架为 VPC 添加出口设置
- r - R中的日期搞砸了
- java - 如何生成随机数,它是 Zn 的元素(部分盲签名程序)
- postgresql - 列文斯坦搜索 postgres