在Python中使用表达式填充多维numpy数组的列表时出现IndexError
从一个csv文件开始,我有一个多维的numpy数组(尺寸是617 x 9),我只需要这个数组中的一列。在这一列中存储的是一些连续的数据,像下面这样:
[0,0,0,0,0,0,620,625,622,710,658,2150,2142,2569,2600,21,24,30,45,32,14,1100,1119,1150 ...]
这些数据以或多或少的循环方式表示了一组组相似的值。我需要把这些值的平均数和标准差放到一个列表里。所以在这个具体的例子中,我会有(我只计算平均数,抱歉):
[0, 647.0, 2365.25, 27.67, 1126.33, ...]
我对python不是很熟悉,所以我想的是先填充一个列表的列表,里面放那些第n个值和下一个n+1个值之间差200的值,然后再进行处理,像这样:
[[0,0,0,0,0,0], [620,625,622,710,658], [2150,2142,2569,2600], [21,24,30,45,32,14], [1100,1119,1150] ...]
我写的初学者代码是这样的:
import numpy as np
import os, time, argparse, matplotlib, glob, datetime, sys, math
path = "/path_to_file/file.csv"
data = np.loadtxt(data, delimiter=',', skiprows=1)
x = [[]]
n_entries = len(data[:,0])
count = 0
m = 0
for n in range(n_entries-1):
if (math.isclose(data[n,1], data[n+1,1], abs_tol = 200)):
x[count][m] = data[n,1]
m += 1
else:
count += 1
m = 0
不幸的是,我得到了这样的输出:
Traceback (most recent call last):
File "/path_to_python_file/file.py", line 49, in <module>
x[count][m] = data[n,1]
IndexError: list assignment index out of range
首先,我希望能得到对这个错误的解释,虽然我在网上查了一些资料,感觉我需要先初始化这个列表……但实际上我并不知道它的尺寸。其次,如果有人觉得自己比我聪明,能给我一些其他的方法建议,我会非常感激!
提前谢谢大家!
1 个回答
0
谢谢你,Barmar!你的建议让我豁然开朗!我会把这个答案发上来,以防将来有人需要快速找到它!肯定会有更优雅的方法来实现这个,但现在这样也能很好地工作!
import statistics as st
import numpy as np
import math
def cyclic_values_finder(array,value):
m_crutch = []
m = 0
for n in range(n_entries-1):
if not (math.isclose(array[n,value], array[n+1,value], abs_tol = 200)):
m_crutch.append(n+1)
m_crutch.append(n_entries)
return m_crutch
def get_array_values(array, value):
arr = []
for ii in range(n_entries):
arr.append(array[ii,value])
return arr
n = 1 # whatever columns of data you'll need to process
path = "/path_to_file/file.csv"
data = np.loadtxt(path, delimiter=',', skiprows=1)
data_needed = get_array_values(data,n)
n_rows = np.array(cyclic_values_finder(data,n))
splitted_data = np.split(data_needed)
avg = []
stdev = []
for row in splitted_data :
avg.append(st.mean(row))
stdev.append(st.stdev(row))