avoid randon state in python model code example
Example 1: random_state
Here you pass an integer, which will act as the seed for the random number generator during the split. Or, you can also pass an instance of the RandomState class, which will become the number generator. If you don’t pass anything, the RandomState instance used by np.random will be used instead.
Example 2: regression best random_state
#x->independent variable
#y->dependent variable
#model->algorithm
def maxr2_score(model,x,y):
max_r_score=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)
model.fit(x_train,y_train)
pred=model.predict(x_test)
score=r2_score(y_test,pred)
if score>max_r_score:
max_r_score=score
final_r_state=r_state
print('max_r2_score is at random_state ',final_r_state,' which is ',max_r_score)
return final_r_state
Example 3: what is random state
The random state is simply the lot number of the set generated randomly in any operation. We can specify this lot number whenever we want the same set again.