【发布时间】:2018-05-30 10:43:39
【问题描述】:
我有以下变量:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
np.random.seed(0)
n = 15
x = np.linspace(0,10,n) + np.random.randn(n)/5
y = np.sin(x)+x/6 + np.random.randn(n)/10
X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0)
def part1_scatter():
%matplotlib notebook
plt.figure()
plt.scatter(X_train, y_train, label='training data')
plt.scatter(X_test, y_test, label='test data')
plt.legend(loc=4);
还有以下问题:
编写一个函数,在训练数据 X_train 上拟合 1、3、6 和 9 阶的多项式 LinearRegression 模型。(使用 sklearn.preprocessing 中的 PolynomialFeatures 创建多项式特征,然后拟合线性回归模型)对于每个模型,在区间 x = 0 到 10 上找到 100 个预测值(例如 np.linspace(0,10,100))并将其存储在一个 numpy 数组中。该数组的第一行应对应于在 1 度、第二行 3 度、第三行 6 度和第四行 9 度上训练的模型的输出。
这是我的代码,但它不起作用:
def answer_one():
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
np.random.seed(0)
n = 15
x = np.linspace(0,10,n) + np.random.randn(n)/5
y = np.sin(x)+x/6 + np.random.randn(n)/10
X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0)
results = []
pred_data = np.linspace(0,10,100)
degree = [1,3,6,9]
y_train1 = y_train.reshape(-1,1)
for i in degree:
poly = PolynomialFeatures(degree=i)
pred_poly1 = poly.fit_transform(pred_data[:,np.newaxis])
X_F1_poly = poly.fit_transform(X_train[:,np.newaxis])
linreg = LinearRegression().fit(X_F1_poly, y_train1)
pred = linreg.predict(pred_poly1)
results.append(pred)
dataArray = np.array(results).reshape(4, 100)
return dataArray
我收到此错误:
line 58 for i
in degree: ^ IndentationError: unexpected
indent
你能告诉我问题出在哪里吗?
【问题讨论】:
标签: python numpy regression