首页 > 解决方案 > 如何在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)

(我省略了不相关的代码部分,但如果您需要查看它,我将编辑帖子并将其添加)

有没有办法做我要求的?

提前致谢!

标签: pythonarrayssvmprediction

解决方案


你可以做:

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))

推荐阅读