2012-03-25 17 views
-2

Hier ist der Kern des Problems. Wenn Sie möchten, können Sie den Code lesen.Liste anhängende seltsame Fehler: Python

ich zwei numpy Array-Liste am anhängt folgenden Befehl one..by:

np.append(list1,list2) 

Was ich erwarte, ist, dass Ausgabeliste der Länge von len (list1) zu + len (list2) Aber stattdessen ist die Ausgabe ganz unterschiedlich lang. Der Code ist down .. Es ist lang ... sorry darüber. Gibt es irgendwelche "gotchas" für solche anhängigen Operationen? Dank

Ich bin, um herauszufinden, nicht in der Lage, was mache ich falsch ?? Lassen Sie mich schreiben Sie einfach den Code und die aus put..and, was ich erwartet hatte und was ich erhalte :( auch die Ausgabe lesen. . ich habe hervorgehoben, wo der Fehler

So I input two list and their corresponding labels. 
import numpy as np 
def list_appending(list_1, list_2, y_one, y_two): 
count_1 = len(list_1) 
count_2 = len(list_2) 
    #if one list is bigger than other.. then add synthetic values shorter list 
    #and its target y variable 
if count_1 > count_2: 

    diff = count_1 - count_2 
    new_y = np.zeros(diff) 
    new_feature_list = generate_synthetic_data(list_2,diff) 

    print "appended ", len(new_feature_list)," synthetic entries to first class (label0)" 
elif count_2 > count_1: 
    diff = count_2 - count_1 
    new_feature_list = generate_synthetic_data(list_1,diff) 
    new_y = np.ones(diff) 
    print "appended ", len(new_feature_list)," synthetic entries to second class (label1)" 
else: 
    diff = 0 
    new_feature_list = [] 
    print "nothing appended" 
print "class 1 y x",len(y_one), len(list_1) 
print "class 2 y x",len(y_two), len(list_2) 

print "len l1 l2 ",len(list_1) , len(list_2) 
f_list = np.append(list_1,list_2) # error is in this line.. unexpected.. see output 
print "first x append ", len(f_list) 
f_list = np.append(f_list,new_feature_list) 
print "second x append ", len(f_list) 
print "new features ", len(new_y), len(new_feature_list) 
new_y_list = np.append(y_one,y_two) 
print "first y append ", len(new_y_list) 
new_y_list = np.append(new_y_list,new_y) 
print "second y append ", len(new_y_list) 
# print features_list_1.shape, features_list_2.shape, new_feature_list.shapes 
#print len(features_list_1[0]), len(features_list_2[0]), len(new_feature_list[0]) 


print "appended ", len(f_list), len(new_y_list) 
return f_list, new_y_list 

Ausgang ist.

appended 35839 synthetic entries to first class (label0) 
class 1 y x 42862 42862 
class 2 y x 7023 7023 
len l1 l2 42862 7023 
first x append 349195 <----- This is the error line 42862 + 7023 = 49885 
second x append 600068 
new features 35839 35839 
first y append 49885 <------------This append was just fine.. 
second y append 85724 
appended 600068 85724 
+0

kann jemand erklären warum -1 ?? – Fraz

+0

Was ist NP? Bin ich nur blind? – tchap

+0

oh np ist import numpy als np – Fraz

Antwort

2

Vom doc page:

If axis is None, out is a flattened array. 

Meine Vermutung ist, dass Sie nicht die Listen glätten wollte.

1

der Code ist sehr, sehr schwer zu verstehen

Wie ich es sehe, haben Sie zwei Listen - list1 und list2 - und Sie möchten die kürzere auffüllen, um sie gleich lang zu machen. Um das zu tun ordentlich:

n, m = len(list1), len(list2) 

if n > m: 
    # Make the shorter list come first 
    list1, list2 = list2, list1 
    n, m = m, n 

list1.append(generate_padding(m-n) if n < m else [])