我想弄清楚为什么sympy.diff没有sympy按预期区分多项式。通常,sympy.diff如果定义了符号变量并且多项式未使用sympy.Poly. 但是,如果函数是使用 定义的sympy.Poly,sympy.diff似乎实际上并没有计算导数。下面是一个代码示例,显示了我的意思:
import sympy as sy
# define symbolic variables
x = sy.Symbol('x')
y = sy.Symbol('y')
# define function WITHOUT using sy.Poly
f1 = x + 1
# define function WITH using sy.Poly
f2 = sy.Poly(x + 1, x, domain='QQ')
# compute derivatives and return results
df1 = sy.diff(f1,x)
df2 = sy.diff(f2,x)
print('f1: ',f1)
print('f2: ',f2)
print('df1: ',df1)
print('df2: ',df2)
Run Code Online (Sandbox Code Playgroud)
这将打印以下结果:
f1: x + 1
f2: Poly(x …Run Code Online (Sandbox Code Playgroud) 通过使用sympy求解多项式函数,我遇到了问题.以下示例显示了一个案例,该案例提供了我无法管理的错误消息.如果多项式变得更简单,则求解器正常工作.请复制并粘贴代码以检查系统上的错误.
import sympy
from sympy import I
omega = sympy.symbols('omega')
def function(omega):
return - 0.34*omega**4 \
+ 7.44*omega**3 \
+ 4.51*I*omega**3 \
+ 87705.64*omega**2 \
- 53.08*I*omega**2 \
- 144140.03*omega \
- 22959.95*I*omega \
+ 42357.18 + 50317.77*I
sympy.solve(function(omega), omega)
Run Code Online (Sandbox Code Playgroud)
你知道我怎么能取得成果吗?谢谢您的帮助.
编辑:
这是错误消息:
TypeError Traceback (most recent call last)
<ipython-input-7-512446a62fa9> in <module>()
1 def function(omega):
2 return - 0.34*omega**4 + 7.44*omega**3 + 4.51*I*omega**3 + 87705.64*omega**2 - 53.08*I*omega**2 - 144140.03*omega - 22959.95*I*omega + 42357.18 + 50317.77*I
----> 3 sympy.solve(function(omega), omega)
C:\Anaconda\lib\site-packages\sympy\solvers\solvers.pyc …Run Code Online (Sandbox Code Playgroud) 我想将(非常)高阶回归拟合到R中的一组数据,但是该poly()函数具有25阶的限制.
对于这个应用程序,我需要一个100到120范围内的订单.
model <- lm(noisy.y ~ poly(q,50))
# Error in poly(q, 50) : 'degree' must be less than number of unique points
model <- lm(noisy.y ~ poly(q,30))
# Error in poly(q, 30) : 'degree' must be less than number of unique points
model <- lm(noisy.y ~ poly(q,25))
# OK
Run Code Online (Sandbox Code Playgroud) 如何让 Python 将多项式作为输入,同时保持将 x 替换为实际值的能力?
这是我尝试过的:
fx=input("Enter a Polynomial: ")
x=float(input("At wich position should the polynomial be evaluated: "))
while True:
print(eval("fx"))
continue
Run Code Online (Sandbox Code Playgroud)
出现的问题是,Python 只会将 fx 计算为 x,而不是我通过第二个输入赋予 x 的值。
比如下面的一个结,二度,样条:
library(splines)
library(ISLR)
fit.spline <- lm(wage~bs(age, knots=c(42), degree=2), data=Wage)
summary(fit.spline)
Run Code Online (Sandbox Code Playgroud)
我看到了我没想到的估计.
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 57.349 3.950 14.518 < 2e-16 ***
bs(age, knots = c(42), degree = 2)1 59.511 5.786 10.285 < 2e-16 ***
bs(age, knots = c(42), degree = 2)2 65.722 4.076 16.122 < 2e-16 ***
bs(age, knots = c(42), degree = 2)3 37.170 9.722 3.823 0.000134 ***
Run Code Online (Sandbox Code Playgroud)
有没有办法在结之前和之后提取二次模型(及其系数)?也就是说,如何在切割点之前和之后提取两个二次模型age = 42?
使用summary(fit.spline)产量系数,但(据我的理解)它们对解释没有意义.
我是机器学习的新手,目前遇到了这个问题。首先,我使用线性回归来拟合训练集,但得到非常大的 RMSE。然后我尝试使用多项式回归来减少偏差。
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import mean_squared_error
poly_features = PolynomialFeatures(degree=2, include_bias=False)
X_poly = poly_features.fit_transform(X)
poly_reg = LinearRegression()
poly_reg.fit(X_poly, y)
poly_predict = poly_reg.predict(X_poly)
poly_mse = mean_squared_error(X, poly_predict)
poly_rmse = np.sqrt(poly_mse)
poly_rmse
Run Code Online (Sandbox Code Playgroud)
然后我得到了比线性回归稍微好一点的结果,然后我继续设置degree = 3/4/5,结果越来越好。但随着度数的增加,它可能会有些过拟合。
多项式的最佳次数应该是在交叉验证集中生成最低 RMSE 的次数。但我不知道如何实现这一目标。我应该使用 GridSearchCV 吗?或任何其他方法?
如果你能给我这个,非常感谢。
使用NumPy polyfit(或类似的东西)是否有一种简单的方法来获得将一个或多个系数限制为特定值的解决方案?
例如,我们可以使用以下公式找到普通的多项式拟合:
x = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
z = np.polyfit(x, y, 3)
Run Code Online (Sandbox Code Playgroud)
屈服
array([ 0.08703704, -0.81349206, 1.69312169, -0.03968254])
Run Code Online (Sandbox Code Playgroud)
但是,如果我想要最合适的多项式,其中第三个系数(在上述情况下z[2])要求为1,该怎么办?还是我需要从头开始编写配件?
使用Python 3.10.0和NumPy 1.21.4。
我试图理解为什么Polynomial.fit()计算出的系数值与polyfit().
在下面的代码中:
import numpy as np
def main():
x = np.array([3000, 3200, 3400, 3600, 3800, 4000, 4200, 4400, 4600, 4800, 5000, 5200, 5400, 5600, 5800, 6000, 6200, 6400, 6600, 6800, 7000])
y = np.array([5183.17702344, 5280.24520952, 5758.94478531, 6070.62698406, 6584.21169885, 8121.20863245, 7000.57326186, 7380.01493624, 7687.97802847, 7899.71417408, 8506.90860692, 8421.73816463, 8705.58403352, 9275.46094996, 9552.44715196, 9850.70796049, 9703.53073907, 9833.39941224, 9900.21604921, 9901.06392084, 9974.51206378])
c1 = np.polynomial.polynomial.polyfit(x, y, 2)
c2 = np.polynomial.polynomial.Polynomial.fit(x, y, 2).coef
print(c1)
print(c2)
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
c1包含: …
在 scikit-learn 的 PolynomialFeatures 预处理器中,有一个 include_bias 选项。这基本上只是向数据框添加一列 1。我想知道这样做有什么意义。当然,您可以将其设置为 False。但从理论上讲,有或没有一列 1 以及生成的多项式特征如何影响回归。
这是文档中的解释,但我似乎无法从中得到任何关于为什么应该使用或不使用它的有用信息。
include_bias : 布尔值
如果为 True(默认),则包括一个偏差列,其中所有多项式幂为零的特征(即一列 - 作为线性模型中的截距项)。
regression linear-regression scikit-learn polynomials data-science
polynomials ×10
python ×6
regression ×3
lm ×2
numpy ×2
r ×2
sympy ×2
algebra ×1
coefficients ×1
data-science ×1
haskell ×1
input ×1
scikit-learn ×1
scipy ×1
spline ×1