【发布时间】: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