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

今天正在尝试使用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()

你可能感兴趣的:(人工智能,python,numpy,bug)