scikit-learn - 同时预测

标签 scikit-learn linear-regression

我已经使用 sklearn.linear_model.LinearRegression 拟合了一个线性模型

我们称它为模型

我有一个 X_1, X_2, ..., X_n 的列表

我所做的是像这样一一预测它们:

for X_i in list:
    model.predict(X_i)

有没有更快的方法来做到这一点?也许我可以将所有 X_i 连接在一起,然后一次预测它们?

最佳答案

您可以使用 numpy.array 调用 predict 并返回 numpy.array 预测:

看一下这个 MVCE,使用拟合 X 的奇数 y = 2X 来预测 X 的偶数:

from sklearn.linear_model import LinearRegression
import numpy as np

X = [1, 3, 5, 7, 9]
y = [2, 6, 10, 14, 18]
lr = LinearRegression()
X = np.array(X)
# However, you need to reshape your X array to be 2-D instead of 1-D.
X = X[:, None]

lr.fit(X, y)

X_pred = [2, 4, 6, 8]
# Combine numpy array and reshape into one statement
X_pred = np.array(X_pred)[:, None] 

y_pred = lr.predict(X_pred)
y_pred

输出:

array([4.,  8., 12., 16.])

关于scikit-learn - 同时预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55670096/

相关文章:

python - 为什么 DBSCAN.fit() 速度更快,功能更多?

python - TestDome 数据科学 : Not getting correct answer

python - 在 scikit-learn 中对多个随机森林模型进行平均

python - 如何让 SVM 很好地处理 scikit-learn 中的缺失数据?

linear-regression - Java非负多元线性回归库

python - 为什么TF-IDF的值和IDF_不一样?

python - Seaborn中如何通过log对x轴和y轴进行等比例缩放?

java - 如何使用NNLS进行非负多元线性回归?

Python - 计算线性回归线的持续 1 个标准偏差

gnuplot - 使用 Gnuplot 进行时间序列的线性回归