python - 如何理解程序输出的多项式系数?
问题描述
我试图获得代表 4 个变量的表面的多项式方程:泄漏、压力、尺寸和速度。基本上我试图找到方程泄漏= f(压力,尺寸,速度)。我设法得到多项式系数和截距,如下所示这篇文章,但我不知道如何在多项式方程中解释它们(即:z = ao + alx + a2Y + a3XY + a4x2 + a5y2 + a6 x3 + a7x2 y + a8 x y2 + ag 等)。有人可以帮忙吗?:
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
# my data
data=np.column_stack((speed, dimension3,pressure3,leakage3))
# Generate polynomial features of desired degree
d = 6
poly = PolynomialFeatures(degree=d, include_bias=False)
X = poly.fit_transform(data[:, :-1])
y = data[:,-1]
# Define and fit linear regression
clf = LinearRegression()
clf.fit(X, y)
# Check results
print(clf.coef_)
print(clf.intercept_)
[ 1.21064489e-09 2.51751918e-11 3.17543952e-12 -3.66443110e-13
-3.62188623e-14 1.13794085e-14 2.33351780e-15 8.76551176e-16
9.65867527e-16 6.69545284e-16 -1.67396381e-16 -1.57313479e-16
-8.47927583e-17 -3.38219081e-16 1.83324692e-17 -3.10419931e-16
1.43757683e-16 -2.25732234e-16 -2.37769462e-16 -1.25305377e-18
4.30862718e-18 -3.03569002e-16 6.43054057e-19 2.88496876e-15
2.13470938e-14 3.85650361e-20 4.65962202e-16 -2.18466792e-13
-2.30089604e-13 -4.53158981e-22 3.96214571e-17 6.38462456e-13
1.48896917e-12 1.52973108e-13 -1.18405974e-14 4.30024113e-15
-2.52978182e-13 5.34046635e-16 2.40414556e-12 1.77892418e-11
3.60577799e-17 3.88296991e-13 -1.82055655e-10 -1.91741331e-10
-4.76611883e-19 3.30372428e-14 5.32052044e-10 1.24080759e-09
1.27477605e-10 2.12356214e-18 1.47504991e-15 1.81132053e-10
3.25304547e-10 7.36098343e-11 1.75235266e-11 2.36581268e-18
-6.40351208e-19 4.91896560e-17 -1.01893976e-17 -3.16647219e-16
-3.52899091e-15 1.99753728e-16 -6.70331612e-15 3.37679794e-14
3.84231696e-14 -1.53920338e-15 1.15182270e-13 -1.08869747e-14
-3.29823619e-13 8.93971247e-14 2.18311227e-15 -8.17692841e-13
-4.15197656e-13 -3.45795442e-12 1.67485115e-11 -2.44352687e-11
2.13680892e-15 1.46360317e-12 1.90178331e-12 -4.17133327e-11
2.89651154e-10 -1.07175872e-09 1.32403379e-09]
9.16822354513272
解决方案
解释
如果您 print poly.powers_
,您应该能够解释每个值的含义。
array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
[2, 0, 0],
[1, 1, 0],
...
[0, 4, 2],
[0, 3, 3],
[0, 2, 4],
[0, 1, 5],
[0, 0, 6]], dtype=int64)
每行都是一个特征,您的变量被提升到相应的幂。
例如:[2, 3, 1]
表示speed^2 * dimension^3 * pressure
。
作为 6 次多项式,对于[x, y, z]
变量的任何幂集,此规则适用:x + y + z <= 6
当您将它们拟合到线性回归模型时,您正在尝试找到最能描述独立变量和因变量(泄漏)之间关系的每个特征的系数。
因此,您可以这样解释它们:
clf.intercept_ +
y0 * speed + # not mentioning "* dimension^0 * pressure^0" which equals 1
y1 * dimension +
y2 * pressure +
y3 * speed^2 +
y4 * speed * dimension +
... +
y79 * dimension^4 * pressure^2 +
y80 * dimension^3 * pressure^3 +
y81 * dimension^2 * pressure^4 +
y82 * dimension * pressure^5 +
y83 * pressure^6
~= leakage
预言
要利用方程中的系数并预测泄漏,您可以调用transform
和predict
为此目的制作的方法。
import numpy as np
x = [[1, 2, 3], [4, 5, 6]]
# the input must be a matrix of shape n_rows * 3 columns
y_pred = poly.transform(np.array(x).reshape(-1, poly.n_input_features_))
y_pred = clf.predict(y_pred)
# timeit:
# 91.5 µs ± 1.73 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
由于 numpy 的广播功能,这相当于(但比)以下功能。IMO 它有助于了解幕后发生的事情。
import numpy as np
def custom_predict(x, clf, poly):
# any number of rows, 3 columns in our case
x = np.array(x).reshape(-1, poly.n_input_features_)
return np.array([
(clf.coef_ * np.product(np.power(row, poly.powers_), axis=1)).sum() + clf.intercept_
for row in x
])
y_pred = custom_predict([[1,2,3], [4, 5, 6]], clf, poly)
# timeit:
# 447 µs ± 3.74 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
如前所述,您的输入 x 必须具有与原始 相同数量的特征(列)fit_transform
,但您可以传递任意数量的观察值(行)。
推荐阅读
- swagger - 如何在 Open api yml 中的地图对象中定义多个对象时间
- extract - 如何修改正则表达式以便提取两个字段的相同字段?
- python - 如何使用 Python Robotframework 中的 Webdriver Manager 根据浏览器版本自动将 .exe 文件下载到项目目录
- python-3.x - 预提交钩子可以克隆吗?
- javascript - 创建新的 RegExp 对象从变量中删除一个字符
- c# - 如何修复异常消息 - .Net 4.5 中的“身份验证失败,因为远程方已关闭传输流”
- android - 跟踪原生库 Android 中的所有函数
- typo3 - 如何为较新的 TYPO3 版本调整 TYPO3 扩展?
- php - 工匠迁移 Symfony\Component\Debug\Exception\FatalThrowableError 出错:找不到类“Doctrine\DBAL\Driver\PDOMySql\Driver”
- html - 停止 IMG 标签溢出