Python:为什么我的线性回归图给出了许多凌乱的彩色线?

标签 python machine-learning scikit-learn linear-regression

这是我得到的代码,但我不确定为什么它会给我一个如此糟糕的情节。我哪里出错了?

import pandas as pd

df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data', header = None, sep = '\s+')
df.columns = ["CRIM", "ZN", 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT','MEDV']
# print df
X = df.iloc[:,:-2].values
y = df.iloc[:,-1].values

from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(X, y)

import matplotlib.pyplot as plt
plt.plot(X, lr.predict(X))
plt.show()

最佳答案

我怀疑您的问题/困惑在于问题的规模。我的理解是,您期望看到一条线穿过像 here 这样的点。其中 line 函数接受 X 并输出 lr.predict(X)。您不会看到这一行,因为您的 X 变量有 12 列,因此您有12 个不同的自变量

如果您尝试将所有这些与您的一个因变量 lr.predict(X) 一起绘制,那么您将需要 13 个维度。 plot 方法不会为您绘制该图。相反,它尝试为 X 中的每个维度绘制 12 条不同的线。这就是您所看到的。

关于Python:为什么我的线性回归图给出了许多凌乱的彩色线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36940182/

相关文章:

python - wx.NotificationMessage 属性错误: 'module' object has no attribute 'NotificationMessage'

python - 在Python中打印伪对齐文本的最有效方法?

python - cross_val_score 和 StratifiedKFold 给出不同的结果

machine-learning - 了解朴素贝叶斯

python - 有没有办法使用 Python(例如 : TensorFlow or Sci-kit learn libs) in Flutter apps?

python - 如何使用 scikit-learn 对大文本数据进行分类?

python - Pandas 基于一列合并不同大小的数据帧

python - 我如何让这个容器自行删除?

Azure机器学习计算二头肌模板命名错误

python - 让 PyC​​harm 导入 sklearn