【Python 多维数组求下标】教程文章相关的互联网学习教程文章

python – Swig和多维数组【代码】

我正在使用Swig将Cthon与C代码连接起来. 我想调用一个C函数,它接受一个包含int ** var的结构:typedef struct {(...)int** my2Darray; } myStruct;void myCFunction( myStruct struct );我正在努力研究多维数组. 我的代码看起来像这样: 在接口文件中,我使用这样的carray:%include carrays.i %array_class( int, intArray ); %array_class( intArray, intArrayArray );在python中,我有:myStruct = myModule.myStruct() var = myM...

python – 简单的问题:在numpy中你如何创建一个多维数组的数组?【代码】

是的,也许我应该使用普通的Python列表,但是这里有: 我想要一个94多维数组/矩阵(无论如何)我想要存储数组.这些数组将是1维的,长度为4096. 所以,我希望能够像这样column = 0 #column to insert into row = 7 #row to insert into storageMatrix[column,row][0] = NEW_VALUE storageMatrix[column,row][4092] = NEW_VALUE_2 etc..我很欣赏我可以在这里做一...

python – 多维数组中列的平方和的平方根【代码】

我正在使用numpy的多维列表 我有一份清单.l = [[0 2 8] [0 2 7] [0 2 5] [2 4 5] [ 8 4 7]]我需要找到平方列之和的平方根.0 2 8 0 2 7 0 2 5 2 4 5 8 4 7输出为,l = [sqrt((square(0) + square(0) + square(0) + square(2) + square(8)) sqrt((square(2) + square(2) + square(2) + square(4) + square(4)) sqrt((square(8) + square(7) + square(5)) + square(5) + square(7))]解决方法: >>> import numpy as np >>> a = np.arra...

python – numpy.concatenate多维数组【代码】

我正在寻找一种算法,将给定数量的多维数组(每个相同的形状)合并到给定的比例(x,y,z). 例如,具有形状(128,128,128)的4个阵列和形状(128,128,512)的阵列的比例(1,1,4).或2个阵列形状(64,64,64)和比例(1,2,1)到形状的阵列(64,128,64) 我知道如何使用np.concatenate手动完成它,但我需要一个通用的算法来做到这一点. (np.reshape不起作用 – 这会弄乱订单) 编辑:比例可能是(1,2,3),那么有必要比较框的left_edge,知道放置它的位置.每个数...

python – 沿多维数组的一维向量点积【代码】

我想使用Theano计算两个多维数组的一个维度的和积. 我将首先使用numpy准确描述我想要做的事情. numpy.tensordot和numpy.dot似乎总是做一个矩阵产品,而我本质上是在寻找一个等价的矢量产品.给定x和y,我想像这样计算z:x = np.random.normal(size=(200, 2, 2, 1000)) y = np.random.normal(size=(200, 2, 2))# this is how I now approach it: z = np.sum(y[:,:,:,np.newaxis] * x, axis=1)# z is of shape (200, 2, 1000)现在我知道...

python – Numpy:多维数组的真实比较【代码】

所以我正在编写一个数独求解器,使用99数组作为网格,并使用9x9x9数组来实现它的可能性.由于我正在使用的回溯算法,我必须检查数独是否仍然有效,也就是: 如果有一个字段不包含数字且没有可能性,则返回False.到目前为止,我实现了如下:for j in range(9):for i in range(9):if puzzle[j,i] == 0 and (solving[j,i] == 0).sum() == 9:return Falsereturn True如果(j,i)处的方格包含例如选项2,3和7,则相应的可能性数组将是:solving[j,i...

python – Numpy:如何为多维数组的各个元素赋值?【代码】

我正在使用三维数组(出于本示例的目的,您可以想象它们代表屏幕的X,Y坐标处的RGB值).>>> import numpy as np >>> a = np.floor(10 * np.random.random((2, 2, 3))) >>> a array([[[ 7., 3., 1.],[ 9., 6., 9.]],[[ 4., 6., 8.],[ 8., 1., 1.]]])我想做的是,为那些G通道已经低于5的像素设置G通道的任意值.我可以设法隔离我感兴趣的像素:>>> a[np.where(a[:, :, 1] < 5)] array([[ 7., 3., 1.],[ 8., 1., 1.]])但我很难理...

在多维数组中查找连续值 – python【代码】

我真的对python完全不熟悉.我需要一些多维数组的帮助.我正在为剧院制作座位预订系统. (只是练习).我已经建立了一个5排乘10座的矩阵.而我真正需要做的就是为每个座位分配“可用”或“不可用”.我显然可以做到这一点,但我不知道怎么做才能搜索或循环一行,看看是否有6个席位可供选择.任何帮助都会很棒.请记住,我是python的新手.解决方法:我能想到的最简单的方法是迭代行,跟踪行号作为索引. 然后,我们计算可用的座位,直到我们找到六个座...

python – Numpy拆分多维数组【代码】

我有一个多维的numpy数组,我想根据特定的列拆分. 防爆. [[1,0,2,3],[1,2,3,4],[2,3,4,5]]假设我想通过第二列将此数组拆分为表达式x< = 2.然后我会得到两个数组[[1,0,2,3],[1,2,3,4]]和[[2,3,4,5]].我目前正在使用这个声明,我认为这是正确的. splits = np.split(S, np.where(S[:, a] <= t)[0][:1]) #splits S based on t#a is the column number解决方法: >>> import numpy as np >>> a = np.asarray([[1,0,2,3],[1,2,3,4],[2,3,4,5...

计算Python中多维数组中达到或超过阈值的次数【代码】

我有一个numpy数组,我从一个形状为(930,360,720)的netCDF文件中引入,它被组织为(时间,纬度,经度). 在930个时间戳中的每一个的每个纬度/经度对上,我需要计算该值达到或超过阈值“x”(例如0.2或0.5等)的次数,并最终计算阈值的百分比在每个点超出,然后输出结果,以便稍后绘制它们. 我尝试了很多方法,但这是我最近的方法:lat_length = len(lats) #where lats has been defined earlier when unpacked from the netCDF datasetlon_lengt...

python – 跨多维数组的矢量化NumPy linspace【代码】

假设我有2个numpy 2D数组,分钟和最大值,它们将始终是彼此相同的维度.我想创建第三个数组,结果,这是将linspace应用于max和min值的结果.是否有一些“numpy”/矢量化方式来做到这一点?示例非矢量化代码如下所示,以显示我想要的结果.import numpy as npmins = np.random.rand(2,2) maxs = np.random.rand(2,2)# Number of elements in the linspace x = 3m, n = mins.shape results = np.zeros((m, n, x))for i in range(m):for j in ...

Python : numpy多维数组-最大最小平均值、求和

“”"" 多维数组-最大最小平均值、求和 “”" import numpy as np n1 = np.random.randint(0,150,size=(2,4,5)) print(n1) 运行结果: [[[144 132 99 140 19] [122 12 37 109 24] [143 96 71 110 80] [ 79 107 116 90 38]][[ 84 146 112 117 110] [ 99 113 7 113 66] [120 28 103 77 76] [ 95 66 22 139 83]]] print(n1.max(axis=0)) 运行结果: [[144 146 112 140 110] [122 113 37 113 66] [143 96 103 11...

python – 为什么numpy.random.dirichlet()不接受多维数组?【代码】

在numpy page他们给出了例子s = np.random.dirichlet((10, 5, 3), 20)这一切都很好,很棒;但是如果你想从二维alpha阵列生成随机样本怎么办?alphas = np.random.randint(10, size=(20, 3))如果您尝试np.random.dirichlet(alphas),np.random.dirichlet([x表示alphas中的x])或np.random.dirichlet((x表示x中的x)),它会导致ValueError:对象太深,不适合所需的数组.似乎唯一有用的是:y = np.empty(alphas.shape) for i in xrange(np.al...

42-python中的矩阵、多维数组----numpy

xzcfightingup ?python中的矩阵、多维数组----numpy1. 引言 最近在将一个算法由matlab转成python,初学python,很多地方还不熟悉,总体感觉就是上手容易,实际上很优雅地用python还是蛮难的。目前为止,觉得就算法仿真研究而言,还是matlab用得特别舒服,可能是比较熟悉的缘故吧。matlab直接集成了很多算法工具箱,函数查询、调用、变量查询等非常方便,或许以后用久了python也会感觉很好用。与python相比,最喜欢的莫过于...

[Python ] Python 多维数组转换的维度对齐问题【代码】

转载自https://stackoverflow.com/questions/48373228/valueerror-could-not-broadcast-input-array-from-shape-25-1-into-shape-25 通过几个例子简单了解一下: Here’s an example of the error: >>> a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,224))] >>> np.array(a) ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224) or, different type of input, but the...