【问题标题】:Regression using Python使用 Python 进行回归
【发布时间】: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


    【解决方案1】:

    return 语句应该在for 完成之后执行,所以它应该缩进到for 下,而不是进一步。

    【讨论】:

    • 如果在 for 下返回,现在我收到以下错误:第 58 行 for i in degree: ^ IndentationError: unexpected indent
    【解决方案2】:

    在行首

    n = 15
    

    你停止了识别。因此该部分不被识别为功能。这可以通过在从 n = 15 开始的所有行上放置 4 个空格来解决。

    【讨论】:

      猜你喜欢
      • 2018-07-23
      • 1970-01-01
      • 2015-10-03
      • 2020-09-27
      • 2020-05-16
      • 1970-01-01
      • 2014-12-22
      • 2013-07-14
      相关资源
      最近更新 更多