classfication best random state

Solutions on MaxInterview for classfication best random state by the best coders in the world

showing results for - "classfication best random state"
Leah
13 Jul 2017
1#x->independent variables
2#y->dependent variable
3#model->algorithm
4from sklearn.model_selection import train_test_split
5from sklearn.metrics import roc_auc_score,recall_score
6from sklearn.linear_model import LogisticRegression
7def maxaccuracy_score(model,x,y):
8    max_accuracy=0
9    for r_state in range(42,101):
10        
11        x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=r_state,stratify=y)
12        model.fit(x_train,y_train)
13        pred=model.predict(x_test)
14        score=accuracy_score(y_test,pred)
15        roc_score=roc_auc_score(y_test,pred)
16        if score>max_accuracy:
17            max_accuracy=score
18            final_r_state=r_state
19    print('max_accuracy_score is at random_state  ',final_r_state,'  which is  ',max_accuracy,'and roc_auc_score=',roc_score)
20    return final_r_state