对于Python-如何在NumPy数组中添加额外的列感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍numpy数组添加一列,并为您提供关于Python/Cython:存储在numpy数组中的类的
对于Python-如何在NumPy数组中添加额外的列感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍numpy数组添加一列,并为您提供关于Python / Cython:存储在numpy数组中的类的开销、python – numpy数组中的奇怪赋值、python – 从N维Numpy数组中仅获取非零子数组、python – 删除numpy数组中的行无效的有用信息。
本文目录一览:- Python-如何在NumPy数组中添加额外的列(numpy数组添加一列)
- Python / Cython:存储在numpy数组中的类的开销
- python – numpy数组中的奇怪赋值
- python – 从N维Numpy数组中仅获取非零子数组
- python – 删除numpy数组中的行无效
Python-如何在NumPy数组中添加额外的列(numpy数组添加一列)
假设我有一个NumPy数组a:
a = np.array([ [1, 2, 3], [2, 3, 4] ])
我想添加一列零以获取一个数组b:
b = np.array([ [1, 2, 3, 0], [2, 3, 4, 0] ])
我如何在NumPy中轻松地做到这一点?
答案1
小编典典我认为,更简单,更快速的启动方法是执行以下操作:
import numpy as npN = 10a = np.random.rand(N,N)b = np.zeros((N,N+1))b[:,:-1] = a
和时间:
In [23]: N = 10In [24]: a = np.random.rand(N,N)In [25]: %timeit b = np.hstack((a,np.zeros((a.shape[0],1))))10000 loops, best of 3: 19.6 us per loopIn [27]: %timeit b = np.zeros((a.shape[0],a.shape[1]+1)); b[:,:-1] = a100000 loops, best of 3: 5.62 us per loop
Python / Cython:存储在numpy数组中的类的开销
有没有办法有效地做到以下几点?
一个示例类:
cdef class ClassWithAdditionFunction: cdef double value def __init__(self,double value): self.value = value cpdef add_one(self): self.value += 1
功能缓慢:
cdef unsigned long int i,ii cdef unsigned long int loops = pow(10,8) cdef double value addition_classes = np.array([None] * 10) for i in range(len(addition_classes)): addition_classes[i] = ClassWithAdditionFunction(value=0) for i in range(loops/10): for ii in range(10): addition_classes[ii].add_one()
非常感谢您的任何建议!
解决方法
要避免GetItem调用,您需要使用numpy的缓冲区接口或内存视图.这告诉cython数组的结构,并允许它更有效地从数组中提取项目.在下面的示例中,我使用了内存视图.如果你做类似的事情,请确保该数组实际上是一个充满ClassWithAdditionFunction实例的数组,否则你可能会遇到段错误.
为了避免GetAttr调用,声明一个ClassWithAdditionFunction类型的变量并对该变量进行方法调用,这样cython就知道该变量具有该方法的编译版本,可用于更快的调用.
最后你已经用cpdef方法定义了add_one,但我建议你也添加一个返回类型.通常我们可以放空,但因为这是一个cpdef函数而不是cdef函数,你可以使用int代替.
如果把所有这些放在一起它应该看起来像:
import numpy as np cimport cython cdef class ClassWithAdditionFunction: cdef double value def __init__(self,double value): self.value = value cpdef int add_one(self): self.value += 1 return 0 @cython.boundscheck(False) @cython.wraparound(False) def main(): cdef: unsigned long int i,ii,loops = 10 ** 6 ClassWithAdditionFunction addInstance double value,y addition_classes = np.array([None] * 10) cdef ClassWithAdditionFunction[:] arrayview = addition_classes for i in range(len(addition_classes)): addition_classes[i] = ClassWithAdditionFunction(value=0) for i in range(loops/10): for ii in range(10): addInstance = arrayview[ii] addInstance.add_one() return None
python – numpy数组中的奇怪赋值
N = np.array([[2,3,6],[12,6,9],[3,10,7],[8,5,1,0] ... ]) N[4] = [3,0] N[N[4]] = [[8,6] [12,9] [2,6]]
我正在构建一个修改N的函数,我需要为某些指定的x修改N [N [x]],这也是一个参数(例子中为4).我想将子流中的所有6更改为另一个数字(假设为0),所以我使用numpy.where来查找索引,这是
where_is_6 = np.where(N[N[4]] == 6)
现在,如果我像N [N [4]] [where_is_6] = 0那样直接替换,则没有变化.如果我之前的引用如var = N [N [4]]然后var [where_is_6]则进行了更改,但是本地更改为函数,并且N不会全局更改.在这种情况下我该怎么办?或者我做错了什么?
解决方法
row_idxs = N[4] r,c = np.where(N[row_idxs] == 6) N[row_idxs[r],c] = 0
python – 从N维Numpy数组中仅获取非零子数组
基本上’arr’有1756020个小阵列形状(28,4).在1756020阵列中,967210是“全零”,788810具有所有非零值.我想删除所有967210’全零’小数组.我使用条件arr [i] == 0.any()编写了一个if else循环,但这需要花费很多时间.有没有更好的方法呢?
解决方法
numpy.any
.
# set up 4d array of ones A = np.ones((5,3,4)) # make second of shape (3,4) = 0 A[1] = 0 # or A[1,...] = 0; or A[1,:,:] = 0 # find out which are non-zero res = np.any(A,axis=(1,2,3)) print(res) [True False True True True]
此功能在numpy v0.17向上提供.根据docs:
axis : None or int or tuple of ints,optional
If this is a tuple of ints,a reduction is performed on multiple axes,instead of a single axis or all the axes as before.
python – 删除numpy数组中的行无效
这是我的代码:
matrix = numpy.loadtxt("matrix_vals.data",delimiter=',',dtype=float) matrix_rows,matrix_cols = matrix.shape # training set randvals = numpy.random.randint(matrix_rows,size=50) train = matrix[randvals,:] test = numpy.delete(matrix,randvals,0) print matrix.shape print train.shape print test.shape
但我得到的输出是:
matrix.shape: (130,14) train.shape: (50,14) test.shape: (89,14)
这显然是错误的,因为来自train和test的行数应该加到矩阵中的总行数,但这里显然更多.谁能帮我弄清楚出了什么问题?
解决方法
使用重复索引进行索引将多次返回同一行,因此矩阵[randvals,:]可确保为您提供正好50行的输出,无论是否重复某些行.
相反,np.delete(矩阵,0)将仅删除唯一行索引,因此它将仅通过randvals中的唯一值的数量来减少行数.
试试比较:
print(np.unique(randvals).shape[0] == matrix_rows - test.shape[0]) # True
要生成0到1之间的唯一随机索引向量 – matrix_rows,可以使用np.random.choice
with replace = False:
uidx = np.random.choice(matrix_rows,size=50,replace=False)
然后矩阵[uidx] .shape [0] np.delete(矩阵,uidx,0).shape [0] == matrix_rows.
关于Python-如何在NumPy数组中添加额外的列和numpy数组添加一列的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Python / Cython:存储在numpy数组中的类的开销、python – numpy数组中的奇怪赋值、python – 从N维Numpy数组中仅获取非零子数组、python – 删除numpy数组中的行无效的相关信息,请在本站寻找。
本文标签: