简体   繁体   English

根据一行数据创建一个numpy数组

[英]create a numpy array from data on a row

My data file has a sample for each row. 我的数据文件每行都有一个样本。 Each row is 400 float number. 每行是400个浮点数。 It's a 20x20 image on a single line. 它是一行上的20x20图像。 I have to write a numpy array with dimensions (number of row, 20, 20, 1). 我必须编写一个具有尺寸(行数,20、20、1)的numpy数组。 Last dimension is the value (the float number in the file). 最后一个维度是值(文件中的浮点数)。

I tried something like: 我尝试了类似的东西:

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)

but I know this is wrong. 但我知道这是错误的。

EDIT: 编辑:

Maybe this is the solution: 也许这是解决方案:

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)

try using reshape 尝试使用重塑

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

it will give you a numpy array of reshaped images with same number of lines 它会为您提供一整行的重塑图像,其中包含相同数量的行

This seems to be the correct answer, thanks to people who helped. 感谢提供帮助的人,这似乎是正确的答案。

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]))
X = np.array(X1)
X = X.reshape(Nsamples,20,20,1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM