Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write for loop?
#4
Hello,Sciadm.
Your code is really perfect,and solve my problem, thank you so much.
And I change a little to select sample data.
However I still have some question?
I'm not sure how to write it run 20 times and get mean and than fill in the accuracy?
I guess need to write next for loops insides, and I have little idea to write.

df = pd.read_csv('LoanStats_2017+2016-39VARIABLES.csv',encoding='big5')
df = df.fillna(df.mean())
accuracies = []
acc = []
i=[]
for counter in range(1000,220000,5000):
    j=20# I guess write run 20 times place
    i.append(counter)
    c=df.sample(counter)
    X=c.drop('loan_status',axis=1)
    #X=X.sample(counter, axis=1)
    Y=c['loan_status']
    model = XGBClassifier()
    X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3)
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred.round())[j] #run j time
    acc.append(accuracy)
    accuracy = np.mean(acc) # I need to write 
    accuracies.append(accuracy)
print('Total number of splsplittings: ', len(accuracies), 'Mean accuracy score: ', np.mean(accuracies), 'std.dev.: ', np.std(accuracies))
I'm not sure how to write code, I think it have some mistakes.
Please give me some advice~~thanks you very much.
Reply


Messages In This Thread
How to write for loop? - by sandy49992 - Aug-29-2018, 04:25 PM
RE: How to write for loop? - by scidam - Aug-30-2018, 01:09 AM
RE: How to write for loop? - by sandy49992 - Aug-30-2018, 11:27 AM
RE: How to write for loop? - by sandy49992 - Aug-30-2018, 02:49 PM
RE: How to write for loop? - by sandy49992 - Aug-30-2018, 04:52 PM
RE: How to write for loop? - by scidam - Aug-31-2018, 12:31 AM
RE: How to write for loop? - by sandy49992 - Sep-04-2018, 03:54 PM
RE: How to write for loop? - by sandy49992 - Sep-21-2018, 01:47 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020