Python IOError:[Errno 2]没有这样的文件或目录

时间:2017-01-26 11:45:19

标签: python machine-learning

在我解释我的情况之前,我想告知这些代码不是我的,我仅用作实验目的的参考。这些代码属于合法的owner

我试图尝试机器学习。我使用这段代码来了解一次性学习。

import numpy as np
import copy
from scipy.ndimage import imread
from scipy.spatial.distance import cdist

nrun = 20
fname_label = 'class_labels.txt'

def LIAP(fn):
    I = imread(fn, flatten=True)
    I = np.array(I, dtype=bool)
    I = np.logical_not(I)
    (row, col) = I.nonzero()
    D = np.array(row, col)
    D = np.transpose(D)
    D = D.astype(float)
    D = D.shape[0]
    mean = np.mean(D, axis=0)
    for i in mean(D, axis=0):
        D[i, :] = D[i, :] - mean
    return D

def MHD(itemA, itemB):
    D = cdist(itemA, itemB)
    mindist_A = D.min(axis=1)
    mindist_B = D.min(axis=0)
    mean_A = np.mean(mindist_A)
    mean_B = np.mean(mindist_B)
    return max(mean_A, mean_B)

def classification_run(folder, f_load, f_cost, ftype='cost'):
    assert ((ftype == 'cost') | (ftype == 'score'))

    with open(folder+'/'+fname_label) as f:
        content = f.read().splitlines()
    pairs = (line.split() for line in content)
    test_files = [pair[0] for pair in pairs]
    train_files = [pair[1] for pair in pairs]
    answers_files = copy.copy(train_files)
    test_files.sort()
    train_files.sort()
    ntrain = len(train_files)
    ntest = len(test_files)

    train_items = [f_load(f) for f in train_files]
    test_items = [f_load(f) for f in test_files]

    costM = np.zeros((ntest, ntrain), float)
    for i in range(ntest):
        for c in range(ntrain):
            costM[i, c] = f_cost(test_items[i], train_items[c])
    if ftype == 'cost':
        YHAT = np.argmin(costM, axis=1)
    elif ftype == 'score':
        YHAT = np.argmax(costM, axis=1)
    else:
        assert False

    correct = 0.0
    for i in range(ntest):
        if train_files[YHAT[i]] == answers_files[i]:
            correct += 1.0
    pcorrect = 100 * correct / ntest
    perror = 100 - pcorrect
    return perror

if __name__ == "__main__":
    print 'One-shot classification demo with Modified Hausdorff Distance'
    perror = np.zeros(nrun)
    for r in range(1, nrun+1):
        rs = str(r)
        if len(rs) == 1:
            rs = '0' + rs
        perror[r-1] = classification_run('run'+rs, LIAP, MHD, 'cost')
        print " run " + str(r) + " (error" + str(perror[r-1]) + "%)"
    total = np.mean(perror)
    print " average error" + str(total) + "%"

但显然,我收到了一个IOError。

One-shot classification demo with Modified Hausdorff Distance
Traceback (most recent call last):
  File "demo.py", line 121, in <module>
    'cost')
  File "demo.py", line 31, in classification_run
    with open(os.path.join(path_to_all_runs, folder, fname_label)) as f:
IOError: [Errno 2] No such file or directory: '/Users/gilangrilhami/Documents/MachineLearning/ml_projects/one/all_runs/run01/class_labels.txt'

据我所知,它应该为每次运行创建一个文件夹并创建它自己的'class_labels.txt'。我试图阅读评论部分,以防我错过了什么,任何人都有同样的问题。但我找不到任何相关的东西。我想找到一个解决方案,或者我错过了一些东西。

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

此txt文件没有创建代码。您应该检查整个仓库并使用它来运行其中一个已存在此文件的运行或手动创建此文件。