为什么需要“预测空间”?

Rak*_*san 3 python machine-learning linear-regression scikit-learn

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

Q1. 我为什么要创建“预测空间”?它有什么用呢?

Q2。“预测空间”上计算预测的关系?

import numpy as np
import pandas as pd

# Read the CSV file into a DataFrame: df
df = pd.read_csv('gapminder.csv')
Run Code Online (Sandbox Code Playgroud)

数据看起来像这样;

国家、年份、寿命、人口、收入、地区

阿富汗,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)
Run Code Online (Sandbox Code Playgroud)

小智 5

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

我也偶然发现了这一点,答案是prediction_spacey_pred用于构造图中的直线

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

# Plot regression line
plt.plot(prediction_space, y_pred, color='black', linewidth=3)
plt.show()
Run Code Online (Sandbox Code Playgroud)