列表的数组列表 Python

Array List of Lists Python

您好,我正在学习他们使用 MNIST 数据库的教程。现在他们使用 cPickle 解压缩数据并获得列表元组等。我想使用我自己的数据,但我有它的 csv 格式,我不确定如何转换为 MNIST 格式。我使用 48 个单元作为我的训练数据,最后一个是我想要的结果

我如何拥有 csv 文件的示例:

1,2,3..........48,1

在我遵循的教程中,他们使用了这个:

training_data, validation_data, test_data = cPickle.load(f)

如果我

打印training_data

我明白了:

(array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       ..., 
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.]], dtype=float32), array([5, 0, 4, ..., 8, 4, 8]))

打印training_data[0]

[[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ..., 
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]]

print training_data[0][0]是一个numpy.array()的每组值,而training_data[1][0]是和float32一起的期望每组输出。

我正在做的代码是这样的,但我对提示中显示的 array() 和 dtype=float32 感到困惑

def load(filename):
    finalDataset = []
    res = 0
    with open(filename, 'rb') as csvfile:
        lines = csv.reader(csvfile)
        datos = list(lines)
        for i in datos:
            tempA = np.array(i[:len(datos)-2]).astype(np.float64)
            tempB = np.float64(i[len(datos)-1:])
            finalDataset.append((tempA,tempB))
    return finalDataset

为了理解数据结构,您可以(最初)忽略 array() 和 dtype 信息。基本上你似乎需要一个由两部分组成的元组(我们称它们为 LEFT 和 RIGHT)。 LEFT 是列表的(矩形)列表(即二维数组),RIGHT 是列表(即一维数组)。

对于 csv 中的每一行,您将前 n-1 个条目作为新行添加到左侧,最后一个条目添加到右侧。

关于您的加载函数: 您的代码将每一行创建为单独的 1dim 数组 + 值,然后将它们作为元组添加到 (python) 列表中。这不是你想要的。如上所述,您需要一个包含数据的 2dim 数组和一个包含预期结果的 1dim 数组。

逐行扩展一个 numpy 数组有点棘手,在大多数情况下应该避免。然后,您可以选择将数据构建为列表的 (python) 列表,然后将其整体转换为一个 numpy 数组,或者更好的是,创建一个预先确定大小的空 numpy 数组,然后用你的数据。

我在下面使用的后期概念:

def load(filename):
    # read file into a list of rows
    with open(filename, 'r') as csvfile:
        lines = csv.reader(csvfile, delimiter=';')
        rows = list(lines)

    # create empty numpy arrays of the required size
    data = np.empty((len(rows), len(rows[0])-1), dtype=np.float64)
    expected = np.empty((len(rows),), dtype=np.int64)

    # fill array with data from the csv-rows
    for i, row in enumerate(rows):
        data[i,:] = row[:-1]
        expected[i] = row[-1]

    training_data = data, expected
    return training_data

编辑: 请注意普通 python 列表和特别是 numpy 数组的非常简洁的索引功能。负索引从列表末尾开始计数,-1 是最后一个条目 -2 是倒数第二个,依此类推。 a:b 索引切片,即索引为 a、a+1、... b-1 的条目。如果 a 在该表示法中留空,则表示 "from the first",如果 b 被遗漏,则表示 "to the last" 条目。因此 : 表示 "everything"。虽然普通 python 列表在一维中支持这种语法,但 numpy 数组通过用冒号分隔每个维度的索引将其扩展到多维。

EDIT2:空 numpy 数组的大小略有偏差。

EDIT3:回应您的评论:

type(training_data) = <type 'tuple'> 表示 training_data 是一个元组(有两个元素)。

type(training_data[0]) = <type 'numpy.ndarray'> 表示 training_data[0] 是一个 numpy 数组(为了简化理解,认为这等同于一个列表)。

type(training_data[1]) = <type 'numpy.ndarray'> 表示 training_data[1] 也是一个 numpy array/list.

type(training_data[0][0]) = <type 'numpy.ndarray'> 表示 training_data[0] 的每个元素本身就是一个 array/list.

type(training_data[1][0]) = <type 'numpy.int64'> 表示 training_data[1] 的每个元素都是一个值。

使用 len(training_data[0])len(training_data[1]) 您可以看到 training_data[0] 中的每个条目(=这是一个值列表,即您的输入数据)都有一个对应的条目在 training_data[1] 中(这是一个单一的值,即您的预期结果值)。