Answers for "classfication best random_state"

0

classfication best random_state

#x->independent variables
#y->dependent variable
#model->algorithm
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score,recall_score
from sklearn.linear_model import LogisticRegression
def maxaccuracy_score(model,x,y):
    max_accuracy=0
    for r_state in range(42,101):
        
        x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=r_state,stratify=y)
        model.fit(x_train,y_train)
        pred=model.predict(x_test)
        score=accuracy_score(y_test,pred)
        roc_score=roc_auc_score(y_test,pred)
        if score>max_accuracy:
            max_accuracy=score
            final_r_state=r_state
    print('max_accuracy_score is at random_state  ',final_r_state,'  which is  ',max_accuracy,'and roc_auc_score=',roc_score)
    return final_r_state
Posted by: Guest on August-11-2020

Python Answers by Framework

Browse Popular Code Answers by Language