首页 > 解决方案 > 从一行上的数据创建一个 numpy 数组

问题描述

我的数据文件的每一行都有一个样本。每行是 400 个浮点数。这是单行上的 20x20 图像。我必须编写一个带有维度的 numpy 数组(行数,20、20、1)。最后一个维度是值(文件中的浮点数)。

我试过类似的东西:

X1=[]
for x in range (1,nrow+1):
    for a in range (1,21):
        for b in range (1,21):
           index = a*b-1
           X1.append((x,a,b,X[x,index]))
X = np.array(X1)

但我知道这是错误的。

编辑:

也许这是解决方案:

X1=[]
for x in range (1,Nsamples+1):
    for a in range (1,21):
        for b in range (1,21):
           index = a*b-1
           X1.append((X[x,index]))
           #X1.append((X[x,index], x))
X = np.array(X1)
X = X.reshape(Nsamples,20,20,1)

标签: pythonnumpy-ndarray

解决方案


尝试使用重塑

X = X.reshape(X.shape[0],20,20,1)

它将为您提供具有相同行数的重塑图像的 numpy 数组


推荐阅读