在python中生成列表时出错

在python中生成列表时出错,python,list,numpy,nested-lists,Python,List,Numpy,Nested Lists,我正在尝试用python构建一个列表。该列表包含列表。单个内部列表由音频信号的各种特征组成,如标准偏差、平均频率等。但当我打印外部列表时,会得到一个空白列表。这是我的密码 from scipy.io.wavfile import read # to read wavfiles import matplotlib.pyplot as plotter from sklearn.tree import DecisionTreeClassifier as dtc import numpy as np

我正在尝试用python构建一个列表。该列表包含列表。单个内部列表由音频信号的各种特征组成,如标准偏差、平均频率等。但当我打印外部列表时,会得到一个空白列表。这是我的密码

from scipy.io.wavfile import read  # to read wavfiles
import matplotlib.pyplot as plotter
from sklearn.tree import DecisionTreeClassifier as dtc
import numpy as np
import os
import scipy
import math

np.set_printoptions(precision=4)

def __init__(self, criterion="gini", splitter="best", max_depth=None, min_samples_split=10, min_samples_leaf=1, min_weight_fraction_leaf=0, max_features=None, random_state=None, max_leaf_nodes=None, min_impurity_split=1e-7, class_weight=None, presort=False):

        super(DecisionTreeClassifier, self).__init__(criterion=criterion, splitter=splitter, max_depth=max_depth, min_samples_split=min_samples_split, min_samples_leaf=min_samples_leaf, min_weight_fraction_leaf=min_weight_fraction_leaf, max_features=max_features, max_leaf_nodes=max_leaf_nodes, class_weight=class_weight, random_state=random_state, min_impurity_split=min_impurity_split, presort=presort)


fList = []  #feature list
mfList = [] #main feature list
labels = ["angry", "angry", "angry", "angry", "angry", "angry", "fear", "fear", "happy", "happy", "happy", "sad", "sad", "sad", "sad", "sad", "sad", "sad", "sad", "sad", "sad", "sad", "sad", "sad", "sad"]
label = [1,2,3,4,5,6,7,8,9,10]

def stddev(lst,mf):
    sum1 = 0
    len1 = len(lst)-1
    for i in range(len(lst)):
        sum1 += pow((lst[i]-mf),2)
    sd = np.sqrt(sum1/len1)
    fList.append(sd)

def find_iqr(num,num_array=[],*args):
    num_array.sort()
    l=int((int(num)+1)/4)
    m=int((int(num)+1)/2)
    med=num_array[m]
    u=int(3*(int(num)+1)/4)
    fList.append(num_array[l])  #first quantile
    fList.append(med)   #median
    fList.append(num_array[u])  #third quantile
    fList.append(num_array[u]-num_array[l]) #inter quantile range


def build(path1):
    dirlist=os.listdir(path1)
    n=1
    mf=0
    for name in dirlist:
        path=path1+name
        print ("File ",n)
        fs, x = read(path) #fs will have sampling rate and x will have sample #
        #print ("The sampling rate: ",fs)
        #print ("Size: ",x.size)
        #print ("Duration: ",x.size/float(fs),"s")

        '''
        plotter.plot(x)
        plotter.show() #x-axis is in samples 
        t = np.arange(x.size)/float(fs) #creating an array with values as time w.r.t samples
        plotter.plot(t)   #plot t w.r.t x
        plotter.show()
        y = x[100:600]
        plotter.plot(y)
        plotter.show()  # showing close-up of samples 
        '''
        j=0     
        med=0
        for i in x:
            j=j+1
            mf=mf+i
        mf=mf/j
        fList.append(np.max(abs(x)))    #amplitude
        fList.append(mf)    #mean frequency
        find_iqr(j,x)
        fList.append((3*med)-(2*mf))    #mode
        stddev(x,mf)
        #fftc = np.fft.rfft(x).tolist()
        #mr = 20*scipy.log10(scipy.absolute(x)).tolist()
        #fList.append(fftc) #1D dft
        #fList.append(mr)   #magnitude response
        mfList.append(fList)
        fList[:] = []
        n=n+1

path1 = '/home/vishnu/Desktop/Trainingsamples/'
path2 = '/home/vishnu/Desktop/TestSamples/'
clf = dtc() # this class is used to make decision tree
build(path1)
print(mfList)
clf.fit(mfList,label)
mfList[:] = []  #clear mflist
tlist = build(path2)
res = clf.predict(tlist)
print(res)
以下是我的输出屏幕:

('File ', 1)
SA1.py:50: RuntimeWarning: invalid value encountered in sqrt
  sd = np.sqrt(sum1/len1)
('File ', 2)
('File ', 3)
('File ', 4)
('File ', 5)
('File ', 6)
('File ', 7)
('File ', 8)
('File ', 9)
('File ', 10)
[[], [], [], [], [], [], [], [], [], []]
Traceback (most recent call last):
  File "SA1.py", line 111, in <module>
    clf.fit(mfList,label)
  File "/home/vishnu/.local/lib/python2.7/site-packages/sklearn/tree/tree.py", line 739, in fit
    X_idx_sorted=X_idx_sorted)
  File "/home/vishnu/.local/lib/python2.7/site-packages/sklearn/tree/tree.py", line 122, in fit
    X = check_array(X, dtype=DTYPE, accept_sparse="csc")
  File "/home/vishnu/.local/lib/python2.7/site-packages/sklearn/utils/validation.py", line 424, in check_array
    context))
ValueError: Found array with 0 feature(s) (shape=(10, 0)) while a minimum of 1 is required.
('File',1)
SA1.py:50:RuntimeWarning:在sqrt中遇到无效值
sd=np.sqrt(sum1/len1)
(“文件”,2)
('File',3)
('File',4)
(“文件”,5)
(“文件”,6)
('File',7)
('File',8)
('File',9)
(‘文件’,10)
[[], [], [], [], [], [], [], [], [], []]
回溯(最近一次呼叫最后一次):
文件“SA1.py”,第111行,在
clf.fit(制造商列表、标签)
文件“/home/vishnu/.local/lib/python2.7/site-packages/sklearn/tree/tree.py”,第739行,适合
X_idx_排序=X_idx_排序)
文件“/home/vishnu/.local/lib/python2.7/site-packages/sklearn/tree/tree.py”,第122行,适合
X=检查数组(X,dtype=dtype,accept\u sparse=“csc”)
check_数组中的文件“/home/vishnu/.local/lib/python2.7/site packages/sklearn/utils/validation.py”,第424行
上下文)
ValueError:找到具有0个功能(形状=(10,0))的数组,但至少需要1个。

在这里可以看到,行
print(mfList)
打印输出
[]、[]、[]、[]、[]、[]、[]、[]、[]、[]、[]、[]、[]、[]、[]
。这是一个空列表列表。我的错误在哪里?请引导。

问题来自于最后调用的
fList[:]=[]
。我做了一个小例子来测试它:

l = []
ml = []

def f(x):
    for i in range(0, x):
        l.append(i)
    ml.append(l)
    l[:] = []

f(10)
f(5)
print(ml)
这将打印包含两个空列表的
ml

>>> [[], []]
如果我删除
l[:]=[]
并将其替换为
l=[]
我会得到两个列表,其中的内容在
ml
中:

>>> [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4]]

fList[:]=[]
表示“用空项替换
fList
中的所有项”。您在此处使用引用,并且仅仅因为您已将
fList
附加到该范围内的
mfList
,并不意味着您仍然无法通过
fList
访问这些元素。因此,如果用新的元素替换
fList
中的元素(在本例中为
[]
),也会影响
mfList

问题来自最后调用的
fList[:]=[]
。我做了一个小例子来测试它:

l = []
ml = []

def f(x):
    for i in range(0, x):
        l.append(i)
    ml.append(l)
    l[:] = []

f(10)
f(5)
print(ml)
这将打印包含两个空列表的
ml

>>> [[], []]
如果我删除
l[:]=[]
并将其替换为
l=[]
我会得到两个列表,其中的内容在
ml
中:

>>> [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4]]

fList[:]=[]
表示“用空项替换
fList
中的所有项”。您在此处使用引用,并且仅仅因为您已将
fList
附加到该范围内的
mfList
,并不意味着您仍然无法通过
fList
访问这些元素。因此,如果将
fList
中的元素替换为新的元素(在本例中为
[]
),也会影响
mfList

,第103行出现错误

fList[:] = []
会发生什么?您可以通过mfList.append(fList)将fList附加到mfList。fList被引用调用。因此,将指针附加到值,而不是值。如果运行上面的代码段,将删除指针引用的这些值。因此,mfList也是空的

相反,您可以使用以下剪接:

fList = []

你在103行有个错误

fList[:] = []
会发生什么?您可以通过mfList.append(fList)将fList附加到mfList。fList被引用调用。因此,将指针附加到值,而不是值。如果运行上面的代码段,将删除指针引用的这些值。因此,mfList也是空的

相反,您可以使用以下剪接:

fList = []

请根据这些准则修改代码,嗯,我删除了我的答案,因为我不确定它是否正确。在调用循环之前,是否可以检查
dirlist
包含的内容?dirlist包含所有文件名。它工作正常我认为问题在于你在函数末尾调用的
fList[:]=[]
。也就是说,在每个文件读取后清除列表。请根据这些准则修改你的代码。嗯,我删除了我的答案,因为我不确定它是否正确。在调用循环之前,是否可以检查
dirlist
包含的内容?dirlist包含所有文件名。它工作正常我认为问题在于你在函数末尾调用的
fList[:]=[]
,即在每次读取文件后清除列表