Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I'm running into some trouble converting values in columns into a Numpy 2d array. What I have as an output from my code is something the following:

38617.0 0 0
40728.0 0 1
40538.0 0 2
40500.5 0 3
40214.0 0 4
40545.0 0 5
40352.5 0 6
40222.5 0 7
40008.0 0 8
40017.0 0 9
40126.0 0 10
40029.0 0 11
39681.5 0 12
39973.0 0 13
39903.0 0 14
39766.5 0 15
39784.0 0 16
39528.5 0 17
39513.5 0 18

And this continues for ~300,000 lines. The coords of the data are arranged as (z,x,y), and I want to convert it into a 2d array with dimensions 765X510 (x,y) so that the z-coordinates are sitting at their respective (x,y) coordinates so that I may write it to an image file.

Any ideas? I've been looking around and I haven't found anything on the matter.


EDIT:

This is the while-loop that's creating the above columns of data (it's actually two, a function is called within another while-loop):

def make_median_image(x,y):
        while y < 509:
                y = y + 1 # Makes the first value (x,0), b/c Python is indexed at 0
                median_first_row0 = sc.median([a11[y,x],a22[y,x],a33[y,x],a44[y,x],a55[y,x],a66[y,x],a77[y,x],a88[y,x],a99[y,x],a1010[y,x]])
                print median_first_row0,x,y
                list1 = [median_first_row0,x,y]
                list = list1.append(

while x < 764:
        x = x + 1
        make_median_image(x,y)
share|improve this question
    
so you have a text file or a list? –  Padraic Cunningham Aug 20 '14 at 16:17
    
I have a text file right now, but I would like to take the output from the while-loop and create a list with these values in the code instead of having to read it from a text file. In other words, I don't want to have to create a text file every time I run it, I just want to save the list to a variable etc. –  bjd2385 Aug 20 '14 at 16:25
    
@PadraicCunningham Would it be possible to append and add the (z,x,y) to the end of an empty list (to start with) every time the while-loop cycles through? –  bjd2385 Aug 20 '14 at 16:29
    
The dimensions of the images are 765 X 510. The function takes the image arrays a11,a22,a33 etc. and gets the median for every individual pixel. For example, it will look at the pixels at (0,0) in all 10 images, read the associated pixel values, and then find the median of all of the pixels at (0,0), then do the same for the rest of the pixels. The goal of this code is to create a "median image", pixel-by-pixel, by not comparing the median of all of the images and then picking the image nearest to median, but to build an image from the median values of all of the pixels. –  bjd2385 Aug 20 '14 at 16:35
    
what do you mean by add? –  Padraic Cunningham Aug 20 '14 at 18:15

2 Answers 2

import numpy as np
l = [[1,2,3], [4,5,6], [7,8,9], [0,0,0]]

You can directly pass a python 2D list into a numpy array.

>>> np.array(l)
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9],
       [0, 0, 0]])

If you only want the latter two columns (which are your x,y values)

>>> np.array([[i[1],i[2]] for i in l])
array([[2, 3],
       [5, 6],
       [8, 9],
       [0, 0]])
share|improve this answer
    
I understand what you're saying here, and I'll utilize this when I've created the list you have above, i.e. l = [[1,2,3],[4,5,6],[7,8,9],[0,0,0]], but first I need to make this list from the output of the while-loop. How might I go about constructing this? –  bjd2385 Aug 20 '14 at 16:27
    
I don't know without seeing your while loop :) Can you edit your code to include how that loop is outputting the data you showed in your original post? –  CoryKramer Aug 20 '14 at 16:28
    
Sure, give me one second... –  bjd2385 Aug 20 '14 at 16:30
    
@Cyber if you want the two columns you can also do a[:, 1:] where a is the numpy array you created from the list... –  Saullo Castro Aug 20 '14 at 16:40

Would it be possible to create an array from the data below like so?

Data:

38617.0 0 0
40728.0 0 1
40538.0 0 2
40500.5 0 3
40214.0 0 4
40545.0 0 5
40352.5 0 6
40222.5 0 7
40008.0 0 8
40017.0 0 9
40126.0 0 10
40029.0 0 11
39681.5 0 12
39973.0 0 13
39903.0 0 14
39766.5 0 15
39784.0 0 16
39528.5 0 17
39513.5 0 18
... (continues for ~100,000 lines, so you can guess why I'm adamant to find an answer)

What I would like:

numpy_ndarray = [[38617.0, 40728.0, 40538.0, 40500.5, 40214.0, 40545.0, 40352.5, ... (continues until the last column value in the data above is 764) ], [begin next line, when x = 1, ... (until last y-value is 764)], ... [ ... (some last pixel value)]]

So it basically builds a matrix/image grid out of the pixel values in the first column of data that's associated with the (x,y) coordinate in the second and third columns.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.