'lo everyone! Been fiddling around with NumPy lately, trying to use it for some simple clustering algorithms. Those require constant merging or rows and columns. Since the arrays are static in size I've been looking at the FAQ and doku for a function that helps with this, but have been unable to find any. What I am doing right now is: Set up a new matrix with one less row or column (zeros()), then copy all the rows/columns but the one I'd like to delete to the new matrix. However, this requires setting up a second matrix for each delete action. A combined row/column delete requires two copy actions, making it even slower. Is there a better/faster way to do this? Ie, parse through the old matrix and copy individual positions instead of rows/columns? Any help would be appreciated! Oliver Hofmann -- Oliver Hofmann - University of Cologne - Department of Biochemistry o.hofmann@smail.uni-koeln.de - setar@gmx.de - connla@thewell.com "It's too bad she won't live. But then, who does?" <Gaff, Blade Runner>
What I am doing right now is: Set up a new matrix with one less row or column (zeros()), then copy all the rows/columns but the one I'd like to delete to the new matrix.
I haven't done any comparisons, but I suspect that Numeric.take() is faster for large arrays. Try this: def delete_row(matrix, row): return Numeric.take(matrix, range(row) + range(row+1, matrix.shape[0])) def delete_column(matrix, column): return Numeric.take(matrix, range(column) + range(column+1, matrix.shape[1]), axis = 1) Konrad. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@cnrs-orleans.fr Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.56.24 Rue Charles Sadron | Fax: +33-2.38.63.15.17 45071 Orleans Cedex 2 | Deutsch/Esperanto/English/ France | Nederlands/Francais -------------------------------------------------------------------------------
Konrad Hinsen (hinsen@cnrs-orleans.fr) wrote:
I haven't done any comparisons, but I suspect that Numeric.take() is faster for large arrays. Try this:
Yes, indeed. Actually, it is so fast that the first time I rewrote the script last week I was quite convinced it had to be buggy. Thanks again everyone, Oliver -- Oliver Hofmann - University of Cologne - Department of Biochemistry o.hofmann@smail.uni-koeln.de - setar@gmx.de - connla@thewell.com "It's too bad she won't live. But then, who does?" <Gaff, Blade Runner>
What I am doing right now is: Set up a new matrix with one less row or column (zeros()), then copy all the rows/columns but the one I'd like to delete to the new matrix.
I haven't done any comparisons, but I suspect that Numeric.take() is faster for large arrays. Try this: def delete_row(matrix, row): return Numeric.take(matrix, range(row) + range(row+1, matrix.shape[0])) def delete_column(matrix, column): return Numeric.take(matrix, range(column) + range(column+1, matrix.shape[1]), axis = 1) Konrad. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@cnrs-orleans.fr Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.56.24 Rue Charles Sadron | Fax: +33-2.38.63.15.17 45071 Orleans Cedex 2 | Deutsch/Esperanto/English/ France | Nederlands/Francais -------------------------------------------------------------------------------
participants (4)
-
hinsen@dirac.cnrs-orleans.fr
-
Konrad Hinsen
-
Oliver Hofmann
-
Oliver Hofmann