Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write for loop?
#8
pd.np.random.seed(123) 
 
df = pd.read_csv('LoanStats_2017+2016-39orgin.csv', encoding='big5')
df = df.fillna(df.mean())
accuracies = defaultdict(list) 
f1err=defaultdict(list)
f2err=defaultdict(list)
dummy_code=pd.get_dummies(df[['home_ownership','purpose','verification_status','grade']])
df=df.drop(['home_ownership','purpose','mths_since_last_delinq','verification_status','grade'],axis=1)
df=df.join(dummy_code)

N_replications = 10
for counter in range(73000, 240000, 12000):
    c = df.sample(counter) # behaviour of this function is controlled by numpy RandomState, we set it value above
    X = c.drop('loan_status', axis=1) 
    #dtrain = xgb.DMatrix(X, label=[0,1])
    #X = X.sample(counter, axis=1)
    Y = c['loan_status']
    for i in range(N_replications):
        X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3)
        model=XGBClassifier(max_depth = 5,objective='binary:logistic')
        model.fit(X_train.round(), y_train.round())
        y_train=y_train.tolist()
        y_pred = model.predict(X_test)
        y_pre = [int(item>0) for  item in y_pred]
        predictions = [round(value) for value in y_pre]
        accuracies[counter].append((accuracy_score(y_test , predictions)))
        f1err[counter].append(1-precision_score(y_test, predictions))
        f2err[counter].append(1-recall_score(y_test,  predictions))
hello, Scidam.
thank you for your help.
Now I find error in my code, but I'm not sure why it happen?
--->error:accuracies[counter].append((accuracy_score(y_test , predictions)))
Classification metrics can't handle a mix of continuous and binary targets
Could you help me? Where it's wrong?
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