【问题标题】:Is it possible to predict using a single variable in multiple regression?是否可以在多元回归中使用单个变量进行预测?
【发布时间】:2021-02-01 11:12:45
【问题描述】:

在使用多个训练数据创建的线性回归中,我必须只使用一个变量进行预测。

一种可能的场景结果如下:

import numpy as np
from sklearn.linear_model import LinearRegression

x = [[0, 1], [5, 1], [15, 2], [25, 5], [35, 11], [45, 15], [55, 34], [60, 35]]
y = [4, 5, 20, 14, 32, 22, 38, 43]
x, y = np.array(x), np.array(y)

model = LinearRegression().fit(x, y)

test_x = np.array([5, 20, 14, 32, 22, 38])
model.predict(test_x.reshape(-1,1))
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 2 is different from 1)

有什么办法可以做到吗?

【问题讨论】:

  • Test_x 集应该类似于 x 数组,您有两个功能并尝试使用 1 个功能进行测试。
  • @MhDG7 但是在这里你使用了 2 个变量。 x1: 5,14,22 x2: 20,32,38 假设您创建了一个模型,该模型使用车辆价格和马力来估计销售数量。然后,我们想仅使用马力而不使用价格信息进行估算。
  • 我认为这是不可能的。也许您可以将第二列归零。
  • 是否可以在二维散点图中显示结果? @Grayrigel
  • 理想情况下,您比较 y_pred、y_test(x1 维)。您不比较 y_pred 和 x_test。因此,您可以在 X 轴上显示 y_test,在 Y 轴上显示 y_pred,反之亦然

标签: python pandas machine-learning scikit-learn data-analysis


【解决方案1】:

特征矩阵中的每个观察值都由 2 个值组成(对于 2 个特征)。您试图一次传递 6 个值,而不是将这 6 个值分成 3 个数组,每个数组包含 2 个值(表示数据中的观察值)。

import numpy as np
from sklearn.linear_model import LinearRegression

x = [[0, 1], [5, 1], [15, 2], [25, 5], [35, 11], [45, 15], [55, 34], [60, 35]]
y = [4, 5, 20, 14, 32, 22, 38, 43]
x, y = np.array(x), np.array(y)

model = LinearRegression().fit(x, y)

test_x = np.array([[5, 20], [14, 32], [22, 38]])
model.predict(test_x)

我可以为您推荐两种方法:

  1. 您可以对在预测输出时不希望使用的列使用零值。
  2. 您可以根据所需功能训练模型。
"""create dummy data"""

import pandas as pd
import numpy as np

# construct a few features
features = np.array([[2, 2],
                     [4, 6],
                     [9, 1],
                     [6, 2]])

# construct a target
target = np.array([15, 20, 50, 18])

# construct a dataframe
dataframe = pd.DataFrame()

dataframe['Price'] = features[:, 0]

dataframe['HorsePower'] = features[:, 1]

dataframe['Cost'] = target

# p.s. I've used the long method to construct my dataframe, you may pass data using the 'data' parameter.
print(dataframe)
print(' ')

# separate features matrix and target vector
features = dataframe.iloc[:, 0:2]
target = dataframe.iloc[:, -1]

# import package
from sklearn.linear_model import LinearRegression

# create instance of LR
algorithm = LinearRegression()

# train the model on both features
model = algorithm.fit(features, target)

# view parameters and hyperparameters
print(model)

# create observation passing values for both features
observation = [[9, 1]]

# obtain predictions
predictions = model.predict(observation)

# print prediction
print(predictions)

plt.scatter(dataframe.index, target, color='crimson', marker='v', edgecolors='black', label='Target_Value')
plt.scatter(dataframe.index, model.predict(features), color='silver', marker='d', edgecolors='black', label='Predicted_Value')
plt.title('Scatter Plot Showing Predicted Target Values Vs Actual Target Values', color='blue')
plt.xlabel('Observation Number', color='blue')
plt.ylabel('Value', color='blue')
plt.legend(numpoints=1, loc='best')
plt.show()

# train model, this time on desired feature (s)
model = algorithm.fit(np.array(features.iloc[:, 0]).reshape(-1, 1), 
    target)

# obtain prediction
prediction = model.predict([[2]])

# print predictions
print(prediction)

【讨论】:

  • 但是这里你使用了 2 个变量。 x1: 5,14,22 x2: 20,32,38 假设您创建了一个模型,该模型使用车辆价格和马力来估计销售数量。然后,我们想仅使用马力而不使用价格信息进行估算。
  • @UğurEren,你是什么意思?
  • 我唯一改变的方面是 test_x 数组。
  • 根据我使用线性回归的经验,我总是为我用来训练模型的列提供输入。
  • 您很可能会绘制一个图表,显示实际目标值与预测目标值。
【解决方案2】:

线性回归的目的是找到输入值和输出值之间的线性关系。

基本上它是:y=θx+Ɛy 你的预测,θ 模型参数(通过训练微调),x 你的输入和Ɛ 一个误差系数。训练的目的是找到最好的θƐ,让你的预测尽可能准确。

To illustrate with a picture, θ and Ɛ are the red curve

您不能训练具有特定维度(输入和输出)的线性回归模型并使用另一个维度进行预测:

在您的示例中,您谈到了[price, horsepower],一个(2,1) 矩阵,在公式中是x,以确定价格y,它是一个标量。所以,θ 应该是一个(1,2) 矩阵,Ɛ 应该是一个标量

如果您只想使用价格或马力,则必须为每种输入创建不同的模型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-11
    • 2023-03-03
    • 1970-01-01
    • 2022-08-24
    • 2020-08-30
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多