【问题标题】:How are these X and Y variables used in Linear regression?这些 X 和 Y 变量如何用于线性回归?
【发布时间】:2019-10-15 05:12:51
【问题描述】:

我正在尝试使用简单的线性回归来预测某个项目的成本。作为输入数据,我使用项目的成本。

代码似乎可以工作,但我无法理解在应用线性回归时如何使用 X 和 Y。我使用 X 作为项目成本,使用 Y 作为标签(使用 X 的移位数据创建一个新行)

df = df[['Item Price']]
forecast_col = 'Item Price'
forecast_out = int(math.ceil(0.0000005 * len(df)))
df['label'] = df[forecast_col].shift(-forecast_out)

X = df[['Item Price']]
X = preprocessing.scale(X)
X_lately = X[forecast_out:]
X = X[:-forecast_out]

df.dropna(inplace=True)

y = np.array(df['label'])

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
clf = LinearRegression(n_jobs=-1)
clf.fit(X_train, y_train)

forecast_set = clf.predict(X)

在求解 Y = a + bX 的线性回归方程时 X 和 Y 变量如何使用

【问题讨论】:

    标签: python pandas machine-learning linear-regression


    【解决方案1】:

    你的线路:

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
    

    将每个 X 和 y 分成两个样本:一个包含 80% 数据的训练和一个包含另外 20% 数据的测试。然后行:

    clf = LinearRegression(n_jobs=-1)
    

    创建一个线性模型。最后一行:

    clf.fit(X_train, y_train)
    

    线性模型使用 X_train 和 Y_train 中的所有 (x,y) 来计算最佳线性回归量。


    在更数学的方法中,该算法使用 X_train 和 Y_train 中包含的所有 (x, y) 来找到最小化方程 E 的 a 和 b:

    E = SUM(y_i - a*x_i - b)

    通过求E的导数和E的二阶导数等于0的地方找到a和b值

    【讨论】:

    • 我现在明白了!!谢谢。
    猜你喜欢
    • 2021-10-03
    • 2020-07-04
    • 2015-06-10
    • 2019-05-15
    • 1970-01-01
    • 2021-10-05
    • 2022-01-17
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多