Python 为什么每次我刷新我的随机森林回归器。MSE和MAE的变化?为什么会有所不同,取决于什么?

Python 为什么每次我刷新我的随机森林回归器。MSE和MAE的变化?为什么会有所不同,取决于什么?,python,machine-learning,output,random-forest,Python,Machine Learning,Output,Random Forest,我正在使用jupyter笔记本,因此我可以尽可能经常地刷新块以适应/预测/评估。每次刷新时,MSE/MAE/RMSE返回一个不同的值,即使训练数据没有被洗牌。为什么会这样 我已经尝试查找问题,但似乎没有帮助,所以我想知道这是因为我的代码还是我缺乏理解 我经常刷新这一块 rf1 = SklearnExtra(clf = RandomForestRegressor(), seed = Seed, params = tune) rf1.fit(x_train, y_train) evaluate(rf

我正在使用jupyter笔记本,因此我可以尽可能经常地刷新块以适应/预测/评估。每次刷新时,MSE/MAE/RMSE返回一个不同的值,即使训练数据没有被洗牌。为什么会这样

我已经尝试查找问题,但似乎没有帮助,所以我想知道这是因为我的代码还是我缺乏理解

我经常刷新这一块

rf1 = SklearnExtra(clf = RandomForestRegressor(), seed = Seed, params = tune)
rf1.fit(x_train, y_train)
evaluate(rf1, x_test, y_test)
print('Test MAPE '+ str(mean_absolute_percentage_error(rf1, y_test, x_test)))

每个树都建立在数据的随机部分(引导)和/或所有特征的子样本上,因此模型每次都不同。这是一片随机森林;-)

您可以使用
randomForestRegressionor(bootstrap=False)
打开bootstap采样,但每次从特征采样中仍然会得到略有不同的结果

但是,如果希望每次都得到相同的结果,可以将
randon\u state
参数设置为固定值,例如
randomfrestreservator(random\u state=42)
:-)

这是Sycorax在CrossValidated上的一个很好的解释

def evaluate(model, test_features, test_labels):
    predictions = model.predict(test_features)
    errors = metrics.mean_absolute_error(test_labels, predictions)
    MSerrors = metrics.mean_squared_error(test_labels, predictions)
    RMSE = np.sqrt(metrics.mean_squared_error(test_labels, predictions))
    RMSLE = np.sqrt(np.mean(np.power(np.log1p(predictions) - np.log1p(test_labels), 2)))
    print('Model Perfomance')
    print('MAE Error: {:0.4f} degrees. '.format(errors))
    print('Average MSE Error: {:0.4f} degrees. '.format(MSerrors))
    print('Average RMS Error: {:0.4f} degrees. '.format(RMSE))
    print('Average RMSLE Error: {:0.4f} degrees. '.format(RMSLE))
    return 'end of test'
class SklearnExtra(object):
    def __init__(self, clf, seed = 0, params = None):
        params['random_state'] = seed
        self.clf = clf

    def train(self, x, y):
        self.clf.fit(x, y)

    def predict(self, x):
        return self.clf.predict(x)

    def fit(self, x, y):
        return self.clf.fit(x,y)

    def feature_importances(self, x, y):
        clf2 = self.clf.fit(x,y)
        return (clf2.feature_importances_)

    def name(self):
        return str(self.clf)
Test Data
Model Perfomance
MAE Error: 26.3329 degrees. 
Average MSE Error: 1950.4288 degrees. 
Average RMS Error: 44.1637 degrees. 
Average RMSLE Error: 0.3016 degrees. 
Test MAPE 24.11994617834992

#next refresh
Test Data
Model Perfomance
MAE Error: 29.7638 degrees. 
Average MSE Error: 2479.5202 degrees. 
Average RMS Error: 49.7948 degrees. 
Average RMSLE Error: 0.3129 degrees. 
Test MAPE 25.520876708239378