python中的分布图

时间:2018-06-03 17:38:54

标签: python python-2.7 linear-regression

enter image description here

我需要你帮助理解分布情节。我正在阅读this link的教程。在帖子的最后他们提到了:

  

我们可以从图中看出,预测的大部分时间都是正确的(差异= 0)。

所以我无法理解他们如何分析图表。

2 个答案:

答案 0 :(得分:2)

您可以想象密度图表显示给定值下数据的相对出现次数。所讨论的值是观察值和拟合变量值之间的差异。如果拟合是完美的,那么所有的差异都是0,并且在0处只有一个条形。拟合不完美,并且存在大于或小于0的一些差异,但它们离零不太远。

作者得出的结论可能过于强烈:图表并未证明差异接近于零,但它表明差异以零为中心。通常,线性回归是一个很好的结果。

答案 1 :(得分:0)

要扩展评论中的讨论,请考虑运行以下代码:

plt.figure(figsize=(12,8))
plt.scatter(range(len(y_test)), y_test, marker='d', c='red')
plt.scatter(range(len(predictions)), predictions, marker='d', c='blue')
plt.scatter(range(len(y_test)), (y_test - predictions), marker='^', c='green')

它会显示以下情节。 y_test的分布以红色菱形显示。 predictions的分布以蓝色菱形显示。如果您使用y_test减去predictions的每个点,则会生成绿色三角形。由于我们正在尝试预测提示,因此我们希望最小化test data(实际数据)与我们使用机器学习所做的predictions之间的误差。

Scatter Distribution

如果您拍摄所有这些绿色三角形,并从中取出distplot,它会显示您附加到问题的图像。以下是每个变量的分布:

# Code to reproduce the plot below
fig = plt.figure(figsize=(12,8))

ax = fig.add_subplot(311)
sb.distplot(y_test)
plt.title('y_test')
plt.xlim([-10, 10])

ax = fig.add_subplot(312)
sb.distplot(predictions)
plt.title('predicted tips')
plt.xlim([-10, 10])

ax = fig.add_subplot(313)
sb.distplot(y_test - predictions)
plt.title('y_test - predicted tips')
plt.xlim([-10, 10])

plt.tight_layout()

plt.show()

Distplot of each variable