MORE INFO - Array assigment with Numeric.py

Johann Hibschman johann at physics.berkeley.edu
Wed May 31 16:37:37 EDT 2000


Morris, Brad [WDLN2:2W40:EXCH] writes:

> This is the routine that I used to load data from an ascii 
> file (this essentially highlighted the problem). The data is 
> separated by blanks and each line is terminated with an EOL 
> character. The file can be either a single line with 
> multiple entries separated by spaces, a single entry per 
> line with multiple lines, or a both. The data is regular, a 
> vector or a matrix, with no missing elements. There is a 
> final empty line in the file with an EOL only.The shapes 
> returned are (1,x), (x,1), or (x,y) respectively. Which in 
> second case is not the same as assigning the data to an 
> array directly (x,). It's not clear to me if this is a 
> function of data.append or Numeric.array(data). Does this 
> mean something is returning (x,NewAxis)?

No, there is no NewAxis in here.  You're simply getting a 2D array out
because you're putting a 2D array in.  If you want to get something
different, you'll have to process the data further.

You're going either
  (1,3) -> array([[1.0, 2.0, 3.0]])
  (3,1) -> array([[1.0],[2.0],[3.0]])
  (3,3) -> array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.]])

If you want to get a 1D array, you'll have to explicitly ask for one:
  (3,)   -> array([1.0, 2.0, 3.0])

Any nested lists (like all of the first three examples) will lead to a
2D array.  If you want to get a 1D array out of your routine, you'll
have to check if one of the dimensions is 1, and if so, collapse that
dimension.  I think Janko gave a routine to do that.

Cheers,

--Johann

-- 
Johann Hibschman                           johann at physics.berkeley.edu



More information about the Python-list mailing list