Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write for loop?
#1
Hello, my friends. This is my first post in python. My English is not very well,sorry...
I'm try to practice more. I do my best to express the question.
This is a loan data code to predict default or not.
I use Xgboost to predict.
I need to write for loops to random select rows or columns to test its accuracy.
I'm not sure how to write, and it could write or not.
Please give me some advices, thank you very much~~
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix

df = pd.read_csv('LendingClubLoanData.csv',encoding='big5')
df = df.fillna(df.mean())
part(1)
#df=df.sample(n=50000) # choose row, total 60000 rows, each 1000 need to test.EX:1000,2000,...,60000 
#for i in range(1000,60000,1000): # I try to write for loop in here
#    print(df.sample(i))
X=df.drop('loan_status',axis=1)
part(2)
#X=X.sample(20, axis=1) #choose columns, total 38 columns, each 10 need to test.EX:10,20,30,38
Y=df['loan_status']
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, random_state=7)

model = XGBClassifier()
model.fit(X_train, y_train)
print(model)
y_pred = model.predict(X_test)

accuracy = accuracy_score(y_test, y_pred.round())
print("Accuracy: %.2f%%" % (accuracy * 100.0))
Outcome:part(1)
sizes Accurancy
1000 0.67
2000 0.68
3000 0.71
.... ....
Outcome:part(2)
feature Accurancy
10 0.66
20 0.74
30 0.73
38 0.71
Accuracy is not actual, it is only assumption.
If code have any wrong, please tell me~
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