以下xarray的使用对我来说是直观的,但不起作用:
import xarray as xr
import numpy as np
data = xr.DataArray(np.random.randn(2, 3),
[
("x", [10, 20]),
("y", [0, 1, 2])
])
# x values are 10 and 20. Here I want to mo
我有一个变量X,它包含一个列表(Python列表),包含10个Numpy一维数组(基本上是向量)。如果我请求列表索引,它会抛出一个错误: IndexError: list index out out range
这完全有道理,但是,当我请求X:100时,它不会抛出错误,而是返回整个列表!为什么会这样呢?
我正在尝试运行W2V算法。我发现索引错误,并且不知道我在哪里出错。以下是错误:
IndexError:只有整数、切片(:)、省略号(...)、numpy.newaxis (None)和整数或布尔数组才是有效的索引。
这是密码:
def makeFeatureVec(words, model, num_features):
# Function to average all of the word vectors in a given
# paragraph
#
# Pre-initialize an empty numpy array (for speed)
featureVe
我是python的新手,我正在编写一些项目。我需要保存一些字典格式的数据{name: numpy数组}(而不仅仅是一个字典),将其保存到文本文件中,然后也从这个文件中读取它。为此,我使用np.savetxt。
import numpy as np
from DIP import dip
name = 'name'
val = dip() #just some function to make an array
dict = {name : val} #create a dictionary
np.savetxt('text.txt',
我有一个形状为(4,13,13)的三维数组,其中4行中的每一行都是13x13的子矩阵。 如何使用np.ix_对所有行的子数组进行索引? a = np.zeros((4,13,13))
to_select = np.ix_([0,2], [0,2])
a[:, to_select] # returns the error below
a[to_select] # works without error, but is not accessing what I want 返回的错误为"IndexError:只有整数、切片(:)、省略号(...)、numpy.newaxis (None)和
如何使用dtype=object访问numpy数组中的数据
b = numpy.array({"a":[1,2,3]}, dtype=object)
下面将引发一个IndexError。
print(b["a"])
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
我正在尝试定义以下函数,但我认为我在某个地方犯了一个错误-
import numpy as np
import math
def banana(A,B,C1,C2,N,keep,init):
R = init*keep + N*keep
x1=x2=0
bimat = np.zeros((N,2))
for r in range(1,R+1):
x1=np.random.normal((B*x2+C1)/(A*(x2**2)+1),math.sqrt(1/(A*(x2**2)+1)))
x2=np.random.normal((B*x1+C2)/(A*(x1**2)+1
我是Python新手,在处理多个数据文件方面遇到了问题。我希望将多个数据文件读取到多个数组中,例如,我希望在1c.txt中读取数据以数组c1,将2c.txt中的数据读入c2等。我尝试了以下代码:
import numpy as np
for i in range(1,15):
globals()['c%s' % i] = np.loadtxt(['%sc.txt' % i], usecols=(0,1,2))
但是它提示使用IndexError: list index out of range,我将usecols=(0,1,2)改为usecols=(0
我有一个从NIfTI文件读取到SimpleITK (使用python)的3D图像,取每个轴向切片,对其做一些操作,并将新的2D切片重新插入到具有(希望)适当尺寸的3D体积中。例如,
output = sitk.Image(original.GetSize(), sitk.sitkFloat32)
output.CopyInformation(original)
for z in numpy.arange(original.GetDepth()):
image = original[:,:,z]
<< Do Something in SimpleITK>>
我正试图将Matlab代码转换为Python,但遇到了一个问题。
t = linspace(0,1,256);
s = sin(2*pi*(2*t+5*t.^2));
h = conj(s(length(s):-1:1));
上面用于h的行是用来计算脉冲响应的,但是我的Python代码:
import numpy as np
t = np.linspace(0,1,256)
s = np.sin(2*np.pi*(2*t+5*t**2))
h = np.conj(s[len(s),-1,1])
给我一个错误IndexError: index 256 is out of bounds for
我有一个数组,我想从数组的每个元素中提取一部分。
id = [000222000,000333000,000444000]
id2 = [222,333,444]
为了能够获得id2数组,我使用了一个for循环,如下所示:
id2 = [i[3:5] for i in id]
但是,我得到了一个标量变量的IndexError:无效索引。我不明白为什么我会犯这个错误,以及如何克服它?另外,如果我有字符串数组而不是数字,这也是一个相同的原则吗?
我做了如下的np.array test = np.array({'a': 1}) 我只想通过键'a‘获取test的值,如下所示 test['a'] 我遇到了下面的错误 IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices 如何在np.array中获取键'a‘的值
我有一张1000码的清单,data_0。我想参考一个位于索引250的元素。因此,为此,我尝试运行以下代码:
data_0[len_0/4]
在这里,len是一个值为"1000“的整数。即使使用这个简单的语句,python整数也会抛出以下错误:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
在其他地方,我得到了类似症状的以下错误:TypeError: slice indices
我很好奇,为什么df.ix[2]和df[2:3]都可以工作,而df[2]却不受支持。
In [26]: df.ix[2]
Out[26]:
A 1.027680
B 1.514210
C -1.466963
D -0.162339
Name: 2000-01-03 00:00:00
In [27]: df[2:3]
Out[27]:
A B C D
2000-01-03 1.02768 1.51421 -1.466963 -0.162339
我期望df[2]的工作方式与df[2:3]