2017-01-26 135 views
0

在我解釋我的情況之前,我想告訴我這個代碼不是我的,我僅用作實驗目的的參考。這些代碼屬於合法的ownerPython IOError:[Errno 2]沒有這樣的文件或目錄

我正在試驗機器學習。我使用這段代碼來獲得關於單次學習的想法。

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」。我試圖閱讀評論部分,以防萬一我錯過了一些東西,任何人都有同樣的問題。但我找不到任何相關的東西。我想找到一個解決方案,或者我錯過了一些東西。

謝謝你的時間。

回答

0

這個txt文件沒有創建代碼。您應該檢出整個回購並使用它運行其中一個運行,該文件已存在或手動創建此文件。

相關問題