【问题标题】:Why "prediction space" is needed?为什么需要“预测空间”?
【发布时间】:2019-02-14 16:07:30
【问题描述】:

使用回归探索 Gapminder 数据进行预测是一个老问题。他们使用“预测空间”来计算预测。

第一季度。我为什么要创建“预测空间”?它有什么用?

第二季度。 “预测空间”上的计算预测关系?

import numpy as np
import pandas as pd

# Read the CSV file into a DataFrame: df
df = pd.read_csv('gapminder.csv')

数据看起来是这样的;

国家、年份、生活、人口、收入、地区

阿富汗,1800,28.211,3280000,603.0,南亚

斯洛伐克共和国,1960,70.47800000000001,4137224,8693.0,欧洲和中亚

# Create arrays for features and target variable
y = df.life.values
X = df.fertility.values

# Reshape X and y
y = y.reshape(-1,1)
X = X.reshape(-1,1)

# Create the regressor: reg
reg = LinearRegression()

# Create the prediction space
prediction_space = np.linspace(min(X_fertility), max(X_fertility)).reshape(-1,1)

# Fit the model to the data
reg.fit(X_fertility, y)

# Compute predictions over the prediction space: y_pred
y_pred = reg.predict(prediction_space)

【问题讨论】:

  • 不看数据怎么解释?您应该在问题中提供一些示例数据。
  • 你是对的。我已按要求提供了样品。
  • 我认为你把代码弄混了,你给 X 赋值然后使用 X_fertility 来适应。

标签: python machine-learning scikit-learn linear-regression


【解决方案1】:

自带 y_pred 做一个baseline,供你计算残差,进一步得到R^2值。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
【解决方案2】:

我相信您正在学习 DataCamp 的课程

我也偶然发现了这个,答案是prediction_space和y_pred是用来构造图中的直线

注意:对于那些正在阅读本文但不明白我在说什么的人,代码 sn-p 实际上缺少图形绘制代码

# Plot regression line
plt.plot(prediction_space, y_pred, color='black', linewidth=3)
plt.show()

【讨论】:

    猜你喜欢
    • 2020-09-15
    • 2016-10-21
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    相关资源
    最近更新 更多