Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

How to append an empty list in a loop in Python

I have made predictions, however the predictions tend to vary so I would like to perform many in a loop, save the prediction within a list and then find the average.

I am stuck at the list making step, here is what I have tried.

EmptyList =[]

for i in range(3):
    model = DecisionTreeClassifier()
    model.fit(X_train_selected, y_train)
    predictions = model.predict(X_validation_selected)
    Acc = accuracy_score(y_validation, predictions)
    EmptyList[i] + Acc
    print(EmptyList)

Can anyone suggest how to perform n number of predictions and then store these in a list?

question from:https://stackoverflow.com/questions/66061330/how-to-append-an-empty-list-in-a-loop-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the .append method:

EmptyList =[]

for i in range(3):
    model = DecisionTreeClassifier()
    model.fit(X_train_selected, y_train)
    predictions = model.predict(X_validation_selected)
    Acc = accuracy_score(y_validation, predictions)
    EmptyList.append(Acc)
    print(EmptyList)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...