Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

python - how to convert a x^2 long list to a list of shape (x,x)

Im in python, I have a list with a length 784 (which i extracted from a 28x28 image ) and now i want to arrange it in a way i can use it in tensor-flow to be used with a trained model but the problem is that the NN needs to have a 28,28 shaped array but my current array is of shape (784,)

I tried to use some for loops for this but i have no luck in successfully creating a system to carry this out , please help

i figured out that i need to use this structure

for i in range(res):
   for a in range(0,res):

       mnistFormat.append(grascalePix[a]) #mnistFormat is an innitially empty list
#and grascale has my 784 grayscale pixles

but i cant figure out what should go in the range function of the for loop to make this possible

For example lets say i have a sample 4x4 image's pixel list >

grayscalePix = [255,255,255,255,255,100,83,200,255,50,60,255,30,1,46,255]

this is a Row by Row representation , which means the first 4 elements are the first ---- row

i want to arrange them into a list of shape (4,4) mnistFormat = [255,255,255,255],[255,100,83,200],[255,50,60,255],[30,1,46,255]

Just keep in mind this is sample , the real data is 784 elements long and i dont have much experince in numpy


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

numpy might help you there very easily:

mnistFormat = np.array(grascalePix).reshape((np.sqrt(len()),-1))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...