您的位置:首页 > 编程语言 > Python开发

使用numpy的polyfit()方法, 报SVD did not converge in Linear Least Squares

2020-07-14 05:46 696 查看

今天正在尝试使用numpy.polyfit()来做多项式拟合的案例
我的代码如下:

# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as mpl
p = [-3, 1, 2, 1]
x = np.linspace(-10, 10, 1000)
y = np.polyval(p, x)

mpl.figure('nihe', facecolor='gray')
mpl.title("polyfit", fontsize=10)
mpl.plot(x, y, color='red', linestyle=':')

P = np.polyfit(x, y, 2)
y1 = np.polyval(P, x)
mpl.plot(x, y1, color='blue', linestyle='-')
mpl.show()

结果出现这个错误

raise LinAlgError("SVD did not converge in Linear Least Squares")
numpy.linalg.LinAlgError: SVD did not converge in Linear Least Squares

同样的代码在同学的电脑下可以跑,而我的就报错了,难受。
我的numpy的版本是1.18.5

不知道有没有大佬知道这个怎么解决。

已解决
P = np.polyfit(x, y, 2)
y1 = np.polyval(P, x)
写在最前面就ok了

# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as mpl
p = [-3, 1, 2, 1]
x = np.linspace(-10, 10, 1000)
y = np.polyval(p, x)

P = np.polyfit(x, y, 2)
y1 = np.polyval(P, x)

mpl.figure('nihe', facecolor='gray')
mpl.title("polyfit", fontsize=10)
mpl.plot(x, y, color='red', linestyle=':')

mpl.plot(x, y1, color='blue', linestyle='-')
mpl.show()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐