473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Numpy, adding a row to a matrix

Hello,
I have a numpy array (2 rows 3 colums)

import numpy
a=numpy.array( [ [1,2,3] , [3,3,1] ])

I wish to add a row, this is how i do it

s=a.shape
numpy.resize(a,s[0]+1,s[1])
a[s[0]]=new row vector.

Q: Is this a costly operation? What happens if i have to it several
(and unknown) number of times?
is there a simpler way to add a row?

Thank you in advance
Saptarshi
Jun 27 '08 #1
2 23547
sapsi wrote:
Hello,
I have a numpy array (2 rows 3 colums)

import numpy
a=numpy.array( [ [1,2,3] , [3,3,1] ])

I wish to add a row, this is how i do it

s=a.shape
numpy.resize(a,s[0]+1,s[1])
a[s[0]]=new row vector.

Q: Is this a costly operation?
It can be if you have large arrays.
What happens if i have to it several
(and unknown) number of times?
is there a simpler way to add a row?
numpy.vstack([a, newrow])

Generally speaking, you shouldn't resize numpy arrays. If you need to construct
an array by appending, build up a list instead and use vstack() (or hstack() or
dstack() or column_stack() or concatenate() depending on the geometry).

We also have a numpy mailing list, which you should direct future numpy
questions to:

http://www.scipy.org/Mailing_Lists

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jun 27 '08 #2
Hello,
Thank you. Yes, I will post to the numpy mailing list in future.
Regards
Saptarshi
On Jun 9, 4:04*pm, Robert Kern <robert.k...@gmail.comwrote:
sapsi wrote:
Hello,
I have a numpy array (2 rows 3 colums)
import numpy
a=numpy.array( *[ [1,2,3] , [3,3,1] ])
I wish to add a row, this is how i do it
s=a.shape
numpy.resize(a,s[0]+1,s[1])
a[s[0]]=new row vector.
Q: Is this a costly operation?

It can be if you have large arrays.
What happens if i have to it several
(and unknown) number of times?
is there a simpler way to add a row?

numpy.vstack([a, newrow])

Generally speaking, you shouldn't resize numpy arrays. If you need to construct
an array by appending, build up a list instead and use vstack() (or hstack() or
dstack() or column_stack() or concatenate() depending on the geometry).

We also have a numpy mailing list, which you should direct future numpy
questions to:

* *http://www.scipy.org/Mailing_Lists

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
* that is made terrible by our own mad attempt to interpret it as thoughit had
* an underlying truth."
* *-- Umberto Eco
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: J | last post by:
Hi I hope the title of this message indicates my question. I am looking for basic array functionality in Python and it turns out that there are all these packages which are somehow related....
2
by: Chris Smith | last post by:
Howdy, I'm a college student and for one of we are writing programs to numerically compute the parameters of antenna arrays. I decided to use Python to code up my programs. Up to now I haven't...
18
by: robert | last post by:
Is there a ready made function in numpy/scipy to compute the correlation y=mx+o of an X and Y fast: m, m-err, o, o-err, r-coef,r-coef-err ? Or a formula to to compute the 3 error ranges? ...
1
by: Jianzhong Liu | last post by:
Hello, Guys, I have a question about the linear_least_squares in Numpy. My linear_least_squares cannot give me the results. I use Numpy1.0. The newest version. So I checked online and get...
5
by: auditory | last post by:
I am a newbie here I am trying to read "space separated floating point data" from file I read about csv module by searching this group, but I couldn't read space separated values with csv....
1
by: Michael O'Brien | last post by:
Hola~ I have a large array of points (over a million). I would like to multiply each point in the array by a 4x4 matrix. I keep thinking there should be an easy way to do this using numpy, but I...
3
by: lancered | last post by:
Hi dear all, I am using Python2.4.2+NumPy1.0.1 to deal with a parameter estimation problem with the least square methods. During the calculations, I use NumPy package to deal with matrix...
2
by: devnew | last post by:
hi i am looking for some info about mapping btw values in an array and corresponding columns of a matrix i have an numpy array= and a numpy matrix object= matrix((, , , ))
1
by: dazzler | last post by:
Hi! I have problem with numpy, multiplying with an inversed matrix will crash python :( this works fine: from numpy import matrix A = matrix(,]) B = matrix(,]) print A.I #inverse matrix
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.