【问题标题】:How to split an array in a for loop Python如何在for循环Python中拆分数组
【发布时间】:2019-07-18 19:04:07
【问题描述】:

我有以下数组:

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)

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

有没有办法按照我的要求做?

提前致谢!

【问题讨论】:

    标签: python arrays svm prediction


    【解决方案1】:

    你可以这样做:

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

    【讨论】:

      猜你喜欢
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-30
      • 1970-01-01
      相关资源
      最近更新 更多