Scikit-learn:我的线性回归不是直线,而是杂乱无章的

我试图简单地绘制一条回归线,但得到的线条非常杂乱。这是由于我使用了两个特征来拟合模型,因此唯一合适的可视化方式应该是三维平面吗?

import numpy as npimport pandas as pdimport matplotlib.pyplot as pltfrom sklearn.datasets import load_bostonfrom sklearn.linear_model import LinearRegression# prepare databoston = load_boston()X = pd.DataFrame(boston.data, columns=boston.feature_names)[['AGE','RM']]y = boston.target# split dataset into training and test datafrom sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test = train_test_split(    X, y, test_size=0.20, random_state=33)# apply linear regression on datasetlm = LinearRegression()lm.fit(X_train, y_train)pred_train = lm.predict(X_train)pred_test = lm.predict(X_test)#plot relationship between RM and priceplt.scatter(X_train['RM'],            y_train,            c='g',            s=40,            alpha=0.5)plt.plot(X_train['RM'], pred_train, color='r')plt.title('Relationship between RM and Price')plt.ylabel('Price')plt.xlabel('RM')

enter image description here


回答:

你说的对。你使用多个特征(即AGE和RM)进行训练,但你绘制的是只有一个特征(即RM)的二维图。尝试绘制三维图。一般来说,具有两个特征的线性回归会得到一个平面。这仍然是线性回归。这就是我们使用“超平面”这个术语的原因。对于单个特征,它会变为一条线;对于两个特征,它会变为一个平面,依此类推。

这是三维的输出:

plt3d = plt.figure().gca(projection='3d')plt3d.view_init(azim=135)plt3d.plot_trisurf(X_train['RM'].values, X_train['AGE'].values, pred_train, alpha=0.7, antialiased=True)

enter image description here

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注