Python Numpy - 将行添加到数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3881453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:10:25  来源:igfitidea点击:

Numpy - add row to array

pythonarraysnumpyrows

提问by Darren J. Fitzpatrick

How does one add rows to a numpy array?

如何将行添加到 numpy 数组?

I have an array A:

我有一个数组 A:

A = array([[0, 1, 2], [0, 2, 0]])

I wish to add rows to this array from another array X if the first element of each row in X meets a specific condition.

如果 X 中每行的第一个元素满足特定条件,我希望从另一个数组 X 向该数组添加行。

Numpy arrays do not have a method 'append' like that of lists, or so it seems.

Numpy 数组没有像列表那样的“追加”方法,或者看起来如此。

If A and X were lists I would merely do:

如果 A 和 X 是列表,我只会做:

for i in X:
    if i[0] < 3:
        A.append(i)

Is there a numpythonicway to do the equivalent?

有没有一种numpythonic 的方法来做等价的?

Thanks, S ;-)

谢谢,S ;-)

采纳答案by eumiro

What is X? If it is a 2D-array, how can you then compare its row to a number: i < 3?

什么是X?如果它是一个二维数组,那么你如何将它的行与一个数字进行比较:i < 3

EDIT after OP's comment:

在 OP 评论后编辑:

A = array([[0, 1, 2], [0, 2, 0]])
X = array([[0, 1, 2], [1, 2, 0], [2, 1, 2], [3, 2, 0]])

add to Aall rows from Xwhere the first element < 3:

AX第一个元素添加到所有行< 3

import numpy as np
A = np.vstack((A, X[X[:,0] < 3]))

# returns: 
array([[0, 1, 2],
       [0, 2, 0],
       [0, 1, 2],
       [1, 2, 0],
       [2, 1, 2]])

回答by jknair

well u can do this :

你可以这样做:

  newrow = [1,2,3]
  A = numpy.vstack([A, newrow])

回答by cam

You can also do this:

你也可以这样做:

newrow = [1,2,3]
A = numpy.concatenate((A,newrow))

回答by user2475529

If you can do the construction in a single operation, then something like the vstack-with-fancy-indexing answer is a fine approach. But if your condition is more complicated or your rows come in on the fly, you may want to grow the array. In fact the numpythonic way to do something like this - dynamically grow an array - is to dynamically grow a list:

如果您可以在单个操作中完成构建,那么类似于 vstack-with-fancy-indexing 的答案是一个很好的方法。但是,如果您的条件更复杂,或者您的行在运行中出现,您可能需要增加数组。事实上,做这样的事情的 numpythonic 方法 - 动态增长一个数组 - 是动态增长一个列表:

A = np.array([[1,2,3],[4,5,6]])
Alist = [r for r in A]
for i in range(100):
    newrow = np.arange(3)+i
    if i%5:
        Alist.append(newrow)
A = np.array(Alist)
del Alist

Lists are highly optimized for this kind of access pattern; you don't have convenient numpy multidimensional indexing while in list form, but for as long as you're appending it's hard to do better than a list of row arrays.

列表针对这种访问模式进行了高度优化;在列表形式中,您没有方便的 numpy 多维索引,但是只要您附加,就很难比行数组列表做得更好。

回答by Charles Chow

You can use numpy.append()to append a row to numpty array and reshape to a matrix later on.

您可以使用numpy.append()将一行附加到 numpty 数组并稍后重塑为矩阵。

import numpy as np
a = np.array([1,2])
a = np.append(a, [3,4])
print a
# [1,2,3,4]
# in your example
A = [1,2]
for row in X:
    A = np.append(A, row)

回答by Flora PJ Li

As this question is been 7 years before, in the latest version which I am using is numpy version 1.13, and python3, I am doing the same thing with adding a row to a matrix, remember to put a double bracketto the second argument, otherwise, it will raise dimension error.

由于这个问题是 7 年前的问题,在我使用的最新版本是 numpy 1.13 版和 python3 中,我正在做同样的事情,向矩阵添加一行,记得在第二个参数上放一个双括号,否则会引起尺寸误差。

In here I am adding on matrix A

在这里,我添加矩阵 A

1 2 3
4 5 6

with a row

与一排

7 8 9

same usage in np.r_

相同的用法 np.r_

A= [[1, 2, 3], [4, 5, 6]]
np.append(A, [[7, 8, 9]], axis=0)

    >> array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])
#or 
np.r_[A,[[7,8,9]]]

Just to someone's intersted, if you would like to add a column,

只是为了有人感兴趣,如果你想添加一个列,

array = np.c_[A,np.zeros(#A's row size)]

array = np.c_[A,np.zeros(#A's row size)]

following what we did before on matrix A, adding a column to it

按照我们之前对矩阵 A 所做的,向其中添加一列

np.c_[A, [2,8]]

>> array([[1, 2, 3, 2],
          [4, 5, 6, 8]])

回答by RAno

I use 'np.vstack' which is faster, EX:

我使用速度更快的“np.vstack”,例如:

import numpy as np

input_array=np.array([1,2,3])
new_row= np.array([4,5,6])

new_array=np.vstack([input_array, new_row])

回答by rbasham

If no calculations are necessary after every row, it's much quicker to add rows in python, then convert to numpy. Here are timing tests using python 3.6 vs. numpy 1.14, adding 100 rows, one at a time:

如果每一行之后都不需要计算,那么在python中添加行要快得多,然后转换为numpy。以下是使用 python 3.6 与 numpy 1.14 进行的计时测试,每次添加 100 行:

import numpy as np 
from time import perf_counter, sleep

def time_it():
    # Compare performance of two methods for adding rows to numpy array
    py_array = [[0, 1, 2], [0, 2, 0]]
    py_row = [4, 5, 6]
    numpy_array = np.array(py_array)
    numpy_row = np.array([4,5,6])
    n_loops = 100

    start_clock = perf_counter()
    for count in range(0, n_loops):
       numpy_array = np.vstack([numpy_array, numpy_row]) # 5.8 micros
    duration = perf_counter() - start_clock
    print('numpy 1.14 takes {:.3f} micros per row'.format(duration * 1e6 / n_loops))

    start_clock = perf_counter()
    for count in range(0, n_loops):
        py_array.append(py_row) # .15 micros
    numpy_array = np.array(py_array) # 43.9 micros       
    duration = perf_counter() - start_clock
    print('python 3.6 takes {:.3f} micros per row'.format(duration * 1e6 / n_loops))
    sleep(15)

#time_it() prints:

numpy 1.14 takes 5.971 micros per row
python 3.6 takes 0.694 micros per row

So, the simple solution to the original question, from seven years ago, is to use vstack() to add a new row after converting the row to a numpy array. But a more realistic solution should consider vstack's poor performance under those circumstances. If you don't need to run data analysis on the array after every addition, it is better to buffer the new rows to a python list of rows (a list of lists, really), and add them as a group to the numpy array using vstack() before doing any data analysis.

因此,七年前原始问题的简单解决方案是在将行转换为 numpy 数组后使用 vstack() 添加新行。但更现实的解决方案应该考虑到 vstack 在这些情况下的性能不佳。如果每次添加后不需要对数组运行数据分析,最好将新行缓冲到python行列表(实际上是列表列表),并将它们作为一个组添加到numpy数组中在进行任何数据分析之前使用 vstack()。

回答by naman1994

import numpy as np
array_ = np.array([[1,2,3]])
add_row = np.array([[4,5,6]])

array_ = np.concatenate((array_, add_row), axis=0)