Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用数组索引numpy数组_Python_List_Function_Numpy_Indexing - Fatal编程技术网

Python 使用数组索引numpy数组

Python 使用数组索引numpy数组,python,list,function,numpy,indexing,Python,List,Function,Numpy,Indexing,我正在尝试将普通python标准偏差函数转换为numpy形式,该函数使用变量number定义的n个索引进行计算。然而,numpy代码是错误的,这意味着只有整数标量数组才能转换为标量索引是否有任何方法可以绕过此过程 变数 import numpy as np number = 5 list_= np.array([457.334015,424.440002,394.795990,408.903992,398.821014,402.152008,435.790985,423.204987,411.5

我正在尝试将普通python标准偏差函数转换为numpy形式,该函数使用变量
number
定义的n个索引进行计算。然而,numpy代码是错误的,这意味着
只有整数标量数组才能转换为标量索引
是否有任何方法可以绕过此过程

变数

import numpy as np
number = 5
list_= np.array([457.334015,424.440002,394.795990,408.903992,398.821014,402.152008,435.790985,423.204987,411.574005,
404.424988,399.519989,377.181000,375.467010,386.944000,383.614990,375.071991,359.511993,328.865997,
320.510010,330.079010,336.187012,352.940002,365.026001,361.562012,362.299011,378.549011,390.414001,
400.869995,394.773010,382.556000])
香草蟒蛇

std= np.array([list_[i:i+number].std() for i in range(0, len(list_)-number)])
Numpy表格

counter = np.arange(0, len(list_)-number, 1)
std = list_[counter:counter+number].std()
取自

def滚动窗口(a,窗口):
形状=a.shape[:-1]+(a.shape[-1]-窗口+1,窗口)
步幅=a.步幅+(a.步幅[-1],)
返回np.lib.stride\u tricks.as\u striped(a,shape=shape,stripes=stripes)
np.std(滚动窗口(列表5),1)
顺便说一下,您的普通python代码是错误的。应该是:

std=np.array([list_u0;[i:i+number].std()表示范围(0,len(list_0;)-number+1)内的i)
我们可以将
std
移出循环。制作一个二维的
窗口数组
,并使用
轴应用
std

In [48]: np.array([arr[i:i+number] for i in range(0, len(arr)-number)]).std(axis
    ...: =1)
Out[48]: 
array([22.67653383, 10.3940773 , 14.60076482, 13.82801944, 13.68038469,
       12.54834004, 13.13574418, 15.24698722, 14.65383773, 11.62092989,
        8.57331689,  4.76392583,  9.49404494, 21.20874383, 24.91417226,
       20.84991841, 13.22152789, 10.83343482, 16.01294245, 13.80007894,
       10.51866421,  8.29287433, 11.24933733, 15.43661128, 13.65945978])
我们还可以生成带有索引的窗口。一种方便的方法是使用
linspace

In [63]: idx = np.arange(0,len(arr)-number)
In [64]: idx = np.linspace(idx,idx+number,number, endpoint=False,dtype=int)
In [65]: idx
Out[65]: 
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
        16, 17, 18, 19, 20, 21, 22, 23, 24],
         ...
       [ 4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
        20, 21, 22, 23, 24, 25, 26, 27, 28]])
In [66]: arr[idx].std(axis=0)
Out[66]: 
array([22.67653383, 10.3940773 , 14.60076482, 13.82801944, 13.68038469,
       12.54834004, 13.13574418, 15.24698722, 14.65383773, 11.62092989,
        8.57331689,  4.76392583,  9.49404494, 21.20874383, 24.91417226,
       20.84991841, 13.22152789, 10.83343482, 16.01294245, 13.80007894,
       10.51866421,  8.29287433, 11.24933733, 15.43661128, 13.65945978])
使用
的滚动窗口可能会更快,但可能更难理解

In [67]: timeit std= np.array([arr[i:i+number].std() for i in range(0, len(arr)-
    ...: number)])
1.05 ms ± 7.01 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [68]: timeit np.array([arr[i:i+number] for i in range(0, len(arr)-number)]).s
    ...: td(axis=1)
74.7 µs ± 108 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [69]: %%timeit
    ...: idx = np.arange(0,len(arr)-number)
    ...: idx = np.linspace(idx,idx+number,number, endpoint=False,dtype=int)
    ...: arr[idx].std(axis=0)
117 µs ± 240 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [73]: timeit np.std(rolling_window(arr, 5), 1)
74.5 µs ± 625 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
使用更直接的方法生成滚动指数:

In [81]: %%timeit
    ...: idx = np.arange(len(arr)-number)[:,None]+np.arange(number)
    ...: arr[idx].std(axis=1)
57.9 µs ± 87.5 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
你的错误
In[82]:arr[np.array([1,2,3]):np.array([4,5,6])]
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
arr[np.数组([1,2,3]):np.数组([4,5,6])]
TypeError:只能将整数标量数组转换为标量索引

不能使用
numpy
数组(arange
的结果)作为片的开始或停止<代码>阵列[1:10]
正常,
阵列[np.数组([1,2,3]:np.数组([4,5,6])
不是!你希望它能产生什么?谢谢你的详细解释,我没有办法避免for循环,我正试图让我的代码运行得更快。我的错,我不是故意不尊重你。我只是不理解
asstrided
部分简单地将
std
移出循环就得到了b超过10倍的改进。升级版的
不是最快的,所以如果你不理解它,不要担心。如何获取多个切片的问题经常出现。一个简单的切片很快(视图),但多个需要某种拷贝高级索引或连接。是的,我想我可以为我的程序实现最后一个,它需要
57.9µs
才能完成。我的程序使用了一个长度超过200万的非常长的列表,它崩溃了,错误消息是
无法为具有形状的数组分配14.2 GiB(2640651, 1440)和数据类型int32
。但无论如何,感谢man。是的,对于较大的数组,可能需要像最初那样进行迭代。即使没有内存错误,内存管理和迭代之间也存在时间权衡。您还可以考虑使用
numba
来编译任务。我正在尝试更快地运行代码。有办法吗我可以在没有for循环的情况下生成该函数。使用我在第一个框中编写的代码。它应该可以在没有for循环的情况下给出所需的结果。
In [81]: %%timeit
    ...: idx = np.arange(len(arr)-number)[:,None]+np.arange(number)
    ...: arr[idx].std(axis=1)
57.9 µs ± 87.5 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [82]: arr[np.array([1,2,3]):np.array([4,5,6])]
Traceback (most recent call last):
  File "<ipython-input-82-3358e59f8fb5>", line 1, in <module>
    arr[np.array([1,2,3]):np.array([4,5,6])]
TypeError: only integer scalar arrays can be converted to a scalar index