2017-03-20 22 views
1

pythonでリストを作成しようとしています。リストにはリストが含まれています。単一の内部リストは、標準偏差、平均周波数などのオーディオ信号のさまざまな機能で構成されます。しかし、私が外側のリストを印刷すると、空のリストが得られます。ここに私のコードです。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. 

1が出力[[], [], [], [], [], [], [], [], [], []]を印刷しprint(mfList)ラインを見ることができるように。これは空のリストです。私の間違いはどこにあるの?ご案内ください。

+0

うーんhttps://stackoverflow.com/help/mcve – klutt

+0

これらのガイドラインに従って、あなたのコードを変更してください、私は私の答えを削除しますそれが正しいのならば。ループを呼び出す前に 'dirlist'が何を含んでいるかチェックできますか? – rbaleksandar

+0

dirlistにはすべてのファイル名が含まれています。正常に動作しています – Midhun

答えて

0

あなたは何が起こるか103

fList[:] = [] 

ラインに間違いがありますか? ffListをmfList.append(fList)で追加します。 fListは、参照番号によって呼び出されるです。したがって、値に対処する代わりに値へのポインタを追加します。上記のコードスニペットを実行すると、ポインタによって参照されるこれらの値を削除します。したがって、mfListも空です。

代わりに次の切り取ら使用することができます私はよく分からないので

fList = [] 
0

問題は最後に電話するfList[:] = []から発生します。私は私の中にその内容を持つ2つのリストを取得しl[:]=[]を削除し、l = []と交換した場合

>>> [[], []] 

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含む2つの空のリストを出力します。私はそれをテストするために、小さな例をしましたml

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

fList[:]=[]は、「すべてのアイテムのインを置き換え意味「fList空の項目です」と表示されます。ここで参照を使用しています。fListmfListをそのスコープ内に添付しても、fListでこれらの要素にアクセスすることはできません。したがって、fListの要素を新しい要素(この場合は[])に置き換えると、mfListにも影響します。

関連する問題