0

My data file has a sample for each row. Each row is 400 float number. It's a 20x20 image on a single line. I have to write a numpy array with dimensions (number of row, 20, 20, 1). 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)
10
  • If you know this is wrong? Why do you think that? Please provide any exception you encountered while running the code? Also what is nrow Commented May 6, 2019 at 11:22
  • I know it's wrong because with one row the shape of the X array is (400, 4) and i would like to get (1,20,20,1). nrow is the number of row in the file. Each row is 400 float number. Commented May 6, 2019 at 11:26
  • so nrow=400 ? Commented May 6, 2019 at 11:27
  • No. Sorry but I am a beginner. nrow is not fixed. Presently it's 1 just for testing, Each row has 400 float number that should go in a 20x20 matrix. Commented May 6, 2019 at 11:30
  • If the dimensions have to include the "number of row", that means the dimension is variable, since there are 20 rows, each with a different number. So I'm unclear what your actual intention is. Perhaps it should be "numbers of rows"? Commented May 6, 2019 at 11:32

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

9 Comments

ValueError: cannot reshape array of size 1600 into shape (400,20,20,1).
X.shape[0] is 400
what is X.shape ?
X.shape is (400, 4) so I can't reshape to (1,20,20,1)
your 400 floats are in a column not in a row
|
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.