python - 在 Python 中绘制多变量线性回归
问题描述
正如标题所说,我很难绘制 MLR。我可以用 2 个变量来做到这一点,但其他任何事情都会导致错误或难看的恐怖。我将如何策划这个?笔记本输出在破折号之间。
import pandas as pd
data = pd.read_excel('data.xls')
data.head()
---------------------------------------
Protein Fat B SNF Cells
0 3.01 3.44 7.76 313
1 3.18 4.14 8.57 673
2 3.59 4.13 9.06 18
3 3.35 3.86 8.47 34
4 3.41 3.81 8.60 42
---------------------------------------
X = data[['Fat B', 'SNF', 'Cells']]
Y = data['Protein']
from sklearn.model_selection import train_test_split
X_train, x_test, Y_train, y_test = train_test_split(X, Y, test_size=0.3, random_state=0)
from sklearn.linear_model import LinearRegression
reg = LinearRegression()
reg.fit(X_train,Y_train)
y_pred = reg.predict(x_test)
print(str(y_test) + " - " + str(y_pred))
---------------------------------------------------------------------------------------------------------------------
16930 3.45
13161 3.01
20475 2.73
11770 3.37
24860 3.55
...
16611 2.85
22789 3.46
12734 3.07
24144 3.13
21275 3.20
Name: Protein, Length: 9585, dtype: float64 - [3.35031197 3.24901896 2.98839754 ... 3.16469215 3.12187003 3.2994102 ]
---------------------------------------------------------------------------------------------------------------------
from sklearn.metrics import r2_score
print(r2_score(y_test,y_pred))
--------------------
0.7017923484495309
--------------------
解决方案
推荐阅读
- python - 在三个字符串列表中搜索元素的 Python 函数
- javascript - Uncaught (in promise) ReferenceError: axios is not defined
- python - 如何将顶点前驱数据框转换为路径?
- performance - 为什么 IR 的大小不同,但可执行文件的大小却相同?以及如何通过静态分析比较性能?
- javascript - 将文本更改为隐藏和显示的按钮
- charts - 如何使用 HighCharts 网络图表实现这一点
- python - pytorch 中 load_state_dict() 的问题
- python - 如何删除模式
- git - 使用多个 ssh 密钥/github 帐户时出现“找不到存储库”
- safari - 操作系统中出现密码模式时 Azure AD 登录弹出窗口未关闭