Geo*_*eyB 25 python arrays numpy list append
我在标题中有这个错误,不知道出了什么问题.当我使用np.hstack而不是np.append时,它正在工作,但我想让它更快,所以请使用append.
time_list浮动列表
高度是1d np.array的花车
j = 0
n = 30
time_interval = 1200
axe_x = []
while j < np.size(time_list,0)-time_interval:
if (not np.isnan(heights[j])) and (not np.isnan(heights[j+time_interval])):
axe_x.append(time_list[np.arange(j+n,j+(time_interval-n))])
Run Code Online (Sandbox Code Playgroud)
File "....", line .., in <module>
axe_x.append(time_list[np.arange(j+n,j+(time_interval-n))])
TypeError: only integer arrays with one element can be converted to an index
Run Code Online (Sandbox Code Playgroud)
Ana*_*mar 39
问题正如错误所示,time_list是一个普通的python列表,因此你不能使用另一个列表对其进行索引(除非另一个列表是一个包含单个元素的数组).示例 -
In [136]: time_list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
In [137]: time_list[np.arange(5,6)]
Out[137]: 6
In [138]: time_list[np.arange(5,7)]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-138-ea5b650a8877> in <module>()
----> 1 time_list[np.arange(5,7)]
TypeError: only integer arrays with one element can be converted to an index
Run Code Online (Sandbox Code Playgroud)
如果你想做那种索引,你需要做time_list一个numpy.array.示例 -
In [141]: time_list = np.array(time_list)
In [142]: time_list[np.arange(5,6)]
Out[142]: array([6])
In [143]: time_list[np.arange(5,7)]
Out[143]: array([6, 7])
Run Code Online (Sandbox Code Playgroud)
另外需要注意的是,在你的while循环中,你永远不会增加j,所以它可能会以无限循环结束,你也应该增加j一些量(也许time_interval?).
但根据您在评论中发布的要求 -
axe_x应该是time_list列表生成的1d浮点数组
您应该使用.extend()而不是.append(),.append将为您创建一个数组列表.但是如果你需要1D阵列,你需要使用.extend().示例 -
time_list = np.array(time_list)
while j < np.size(time_list,0)-time_interval:
if (not np.isnan(heights[j])) and (not np.isnan(heights[j+time_interval])):
axe_x.extend(time_list[np.arange(j+n,j+(time_interval-n))])
j += time_interval #Or what you want to increase it by.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51020 次 |
| 最近记录: |