1#x->independent variable
2#y->dependent variable
3#model->algorithm
4def maxr2_score(model,x,y):
5 max_r_score=0
6 for r_state in range(42,101):
7
8 x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=r_state)
9 model.fit(x_train,y_train)
10 pred=model.predict(x_test)
11 score=r2_score(y_test,pred)
12
13 if score>max_r_score:
14 max_r_score=score
15 final_r_state=r_state
16 print('max_r2_score is at random_state ',final_r_state,' which is ',max_r_score)
17 return final_r_state
18
19