向Numpy数组添加新列的最佳方法 [英] Best way to add a new column to a Numpy array

查看:1000
本文介绍了向Numpy数组添加新列的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将向量添加到矩阵.

I want to add a vector to a matrix.

向量当前是一个列表(尽管很容易转换为一维Numpy数组).

The vector is currently a list (although easily converted to a 1D Numpy array).

矩阵当前是一个Numpy数组.

And the matrix is currently a Numpy array.

我当时想将矩阵重塑为矩阵,然后遍历最后一列,添加所需的值.但是,我不确定如何以这种方式重塑矩阵(即添加一列).我也希望我不必使用for循环.

I was thinking I could reshape the matrix to a matrix and then loop through the last column adding the desired values. However, I wasn't sure how I could reshape a matrix this way (i.e. adding a column). I was also hoping I didn't have to use a for loop.

我调查了使用np.concatenatenp.hstacknp.append的情况.但是,我相信我需要将我的原始矩阵创建为矩阵与列全部为None.这对我不起作用,因为在将最后一个向量添加到该矩阵之前,我将其用于许多计算.

I looked into using np.concatenate, np.hstack, and np.append. However, I believe I need to create my original matrix as a matrix with the column all None. This will not work for me because I use this matrix for many calculations before I need to add this last vector to it.

推荐答案

您可以使用


但是请注意,最好先分配所需大小(和最大)的数组,因为np.column_stacknp.append之类的操作可能需要为更大的数组和分配新的空间.将arr中的所有值复制到新数组中.那可能很慢,而且内存效率低下. (为什么只需要一个,就为两个几乎相同的数组分配空间?)


Note, however, that it would be better to allocate the right-sized (and biggest) array needed first, since operations like np.column_stack or np.append may need to allocate new space for the bigger array and copy all the values from arr into the new array. That could be slow as well as memory-inefficient. (Why allocate space for two almost identical arrays, when you only need one?)

因此,您可以使用

arr = np.empty((3, 5))  # the size of the final, biggest array
smallarr = arr[:, :-1]  

由于arr[:, :-1]arr的基本组成部分,所以smallarr是arr的视图.修改smallarr也会影响arr.

Since arr[:, :-1] is a basic slice of arr, smallarr is a view of arr. Modifying smallarr will affect arr as well.

例如:

In [117]: arr = np.empty((3, 5))

In [118]: smallarr = arr[:, :-1]

In [119]: smallarr[...] = np.arange(12).reshape(3,4)

In [123]: arr[:, -1] = v

In [124]: arr
Out[124]: 
array([[  0.,   1.,   2.,   3.,   1.],
       [  4.,   5.,   6.,   7.,   2.],
       [  8.,   9.,  10.,  11.,   3.]])

分配给smallarr时,请务必使用smallarr[...] = ...而不是smallarr = ...,因为您想就地修改smallarr,请勿将变量名重定向到新对象.

When assigning to smallarr just be sure to use smallarr[...] = ... instead of smallarr = ... since you want to modify smallarr in place, not redirect the variable name to a new object.

您还可以使用许多NumPy函数中提供的out参数来修改smallarr.除了返回值,该函数还将值写入out参数`指定的数组.

You can also modify smallarr by using the out parameter available in many NumPy functions. In addition to returning the value, the function writes the value to the array specified by the out parameter`.

因此,您可以在smallarr上进行计算,并且已经修改了arr并且具有正确的大小,并且全部以节省内存的方式完成了.

Thus you can do your calculations on smallarr and have arr already modified and of the right size and all done in a memory-efficient way.

这篇关于向Numpy数组添加新列的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆