[Python] Change in random state value changes the model accuracy

Discussão em 'Python' iniciado por Stack, Maio 9, 2025.

  1. Stack

    Stack Membro Participativo

    While testing my linear regression model, I find that changing the random_state parameter in train_test_split changes the model accuracy.

    To elaborate, my train test split is as follows:

    boston_data = load_boston()
    X = pd.DataFrame(boston_data.data, columns=boston_data.feature_names)
    y = pd.DataFrame(boston_data.target, columns=['MEDV'])

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)


    And my model is as follows:

    steps = [
    ('regr', Lasso())
    ]
    pipeline = Pipeline(steps)

    parameters = {
    'regr__alpha': np.logspace(-4, -0.5, 40)
    }

    grid = GridSearchCV(pipeline, param_grid=parameters, cv=10, n_jobs=-1)
    grid.fit(X_train, y_train)
    grid.score(X_test, y_test)


    Now, for example, for the current scenario (where random_state = 42) the test score is 0.725. However, if I change it to 43, the test score comes down to 0.7.

    I understand that random_state signifies what set of data will be in test and training set. Having said that, I want to know, if there's way to get a stable result?

    Thanks!

    Continue reading...

Compartilhe esta Página