计算间隔边界和大小

时间:2020-08-03 15:09:28

标签: python numpy machine-learning statistics

我正在尝试根据ML模型确定预测的边界,但是我总是想出这些预测的大小相同。我正在尝试考虑差异,因为这会改变大小的幅度,但出现以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-250-50b9c97503ec> in <module>
      7 intervals = np.zeros((X_test.shape[0], 2)) #Creates empty 2D numpy array for saving prediction intervals
      8 err_dist = np.hstack([err_dist] * n_test) ##Stack arrays in sequence horizontally (column wise)
----> 9 err_dist = err_dist/ updated_residuals
     10 err_dist *= norm
     11 intervals[:, 0] = test_predictions - err_dist[0, :] #[0] creates lower boundary of the prediction interval

ValueError: operands could not be broadcast together with shapes (2,3644) (2916,) 

代码段

updated_residuals = np.abs(predictions-true_labels)
border = int(np.floor(0.05 * (updated_residuals.size + 1))) - 1
border = min(max(border, 0), updated_residuals.size - 1)
err_dist = np.vstack([updated_residuals[border], updated_residuals[border]])
test_predictions = test_predictions.flatten()
n_test = X_test.shape[0] #Takes shape of X_test row number
norm = np.ones(n_test) #Returns an array of X_test shape, filled with ones.
intervals = np.zeros((X_test.shape[0], 2)) #Creates empty 2D numpy array for saving prediction 
intervals
err_dist = np.hstack([err_dist] * n_test) ##Stack arrays in sequence horizontally (column wise)
err_dist = err_dist/ updated_residuals
err_dist *= norm
intervals[:, 0] = test_predictions - err_dist[0, :] #[0] creates lower boundary of the prediction 
interval
intervals[:, 1] = test_predictions + err_dist[1, :] #[1] creates upper boundary of the prediction 
interval
interval_size = intervals[:, 1] - intervals[:, 0] #Efficiency measure

输入数据示例

测试预测

array([[ 0.30200304],[ 0.33511445],[ 0.45157605],[ 0.44883877], [ 0.42324279], [-0.49296603]])

True_Labels

array([0.96547939, 0.43866967, 0.46089677, 0.55578728, 0.58520389,0.03926788])

电流输出

间隔大小

array([0.40789525, 0.40789525, 0.40789525, 0.40789525, 0.40789525, 0.40789525])

我想获得不同的间隔大小,但是不确定是否计算正确。

0 个答案:

没有答案
相关问题