Python3-尝试保存文本文件时出现IndexError

时间:2018-10-05 11:39:45

标签: arrays python-3.x function text cntk

我正在尝试使用自己的本地数据文件来学习本教程:

CNTK tutorial

我具有以下功能,可将我的数据数组保存到可馈送给CNTK的txt文件中:

# Save the data files into a format compatible with CNTK text reader

def savetxt(filename, ndarray):
    dir = os.path.dirname(filename)

    if not os.path.exists(dir):
        os.makedirs(dir)

    if not os.path.isfile(filename):
        print("Saving", filename )
        with open(filename, 'w') as f:
            labels = list(map(' '.join, np.eye(11, dtype=np.uint).astype(str)))
            for row in ndarray:
                row_str = row.astype(str)
                label_str = labels[row[-1]]
                feature_str = ' '.join(row_str[:-1])
                f.write('|labels {} |features {}\n'.format(label_str, feature_str))
    else:
        print("File already exists", filename)

我有以下形状的2个ndarray,我想提供给模型:

train.shape
(1976L, 15104L)

test.shape
(1976L, 15104L)

然后我尝试实现以下功能:

# Save the train and test files (prefer our default path for the data)

data_dir = os.path.join("C:/Users", 'myself', "OneDrive", "IA Project", 'data', 'train')
if not os.path.exists(data_dir):
    data_dir = os.path.join("data", "IA Project")

print ('Writing train text file...')
savetxt(os.path.join(data_dir, "Train-128x118_cntk_text.txt"), train)

print ('Writing test text file...')
savetxt(os.path.join(data_dir, "Test-128x118_cntk_text.txt"), test)

print('Done')

然后出现以下错误:

Writing train text file...
Saving C:/Users\A702628\OneDrive - Atos\Microsoft Capstone IA\Capstone data\train\Train-128x118_cntk_text.txt
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-24-b53d3c69b8d2> in <module>()
      6 
      7 print ('Writing train text file...')
----> 8 savetxt(os.path.join(data_dir, "Train-128x118_cntk_text.txt"), train)
      9 
     10 print ('Writing test text file...')

<ipython-input-23-610c077db694> in savetxt(filename, ndarray)
     12             for row in ndarray:
     13                 row_str = row.astype(str)
---> 14                 label_str = labels[row[-1]]
     15                 feature_str = ' '.join(row_str[:-1])
     16                 f.write('|labels {} |features {}\n'.format(label_str, feature_str))

IndexError: list index out of range

有人可以告诉我这部分代码怎么了吗?我该如何解决?预先非常感谢。

1 个答案:

答案 0 :(得分:1)

由于您使用自己的输入数据-它们的标记范围是0到9吗?标签数组中只有10个条目,因此可能会导致超出范围的问题。