由于某些原因,我无法再让这段代码正确运行:
import numpy as np
from sklearn.linear_model import LinearRegression
# Create linear data with some noise
x = np.random.uniform(0, 100, 1000)
y = 2. * x + 3. + np.random.normal(0, 10, len(x))
# Fit linear data with sklearn LinearRegression
lm = LinearRegression()
lm.fit(x.reshape(-1, 1), y)
Traceback (most recent call last):
File "<input>", line 2, in <module>
File "C:\Python37\lib\site-packages\sklearn\linear_model\_base.py", line 547, in fit
linalg.lstsq(X, y)
File "C:\Python37\lib\site-packages\scipy\linalg\basic.py", line 1224, in lstsq
% (-info, lapack_driver))
ValueError: illegal value in 4-th argument of internal None
我不确定为什么在这么简单的例子中会出现这个错误。这是我的当前版本:
scipy.__version__
'1.5.0'
sklearn.__version__
'0.23.1'
我正在64位的Windows 10企业版和Python 3.7.3上运行此代码。我尝试卸载并重新安装scipy和scikit-learn,尝试较早版本的scipy,以及卸载并重新安装Python,但这些都没有解决问题。
更新: 似乎与matplotlib有关。我之前在Pycharm中运行了这个例子,但现在直接从PowerShell中运行它。因此,如果我在Pycharm之外运行此代码,则不会出现错误。
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
# Create linear data with some noise
x = np.random.uniform(0, 100, 1000)
y = 2. * x + 3. + np.random.normal(0, 10, len(x))
# Fit linear data with sklearn LinearRegression
lm = LinearRegression()
lm.fit(x.reshape(-1, 1), y)
但是,如果我在其间绘制数据,则会出现错误:
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
# Create linear data with some noise
x = np.random.uniform(0, 100, 1000)
y = 2. * x + 3. + np.random.normal(0, 10, len(x))
# Plot data
plt.scatter(x, y)
plt.plot(np.linspace(0, 100, 10), 2. * np.linspace(0, 100, 10) + 3., ls='--', c='red')
# Fit linear data with sklearn LinearRegression
lm = LinearRegression()
lm.fit(x.reshape(-1, 1), y)
** On entry to DLASCLS parameter number 4 had an illegal value
Traceback (most recent call last):
File ".\run.py", line 18, in <module>
lm.fit(x.reshape(-1, 1), y)
File "C:\Python37\lib\site-packages\sklearn\linear_model\_base.py", line 547, in fit
linalg.lstsq(X, y)
File "C:\Python37\lib\site-packages\scipy\linalg\basic.py", line 1224, in lstsq
% (-info, lapack_driver))
ValueError: illegal value in 4-th argument of internal None
但是如果我将plt.plot(np.linspace(0, 100, 10), 2. * np.linspace(0, 100, 10) + 3., ls='--', c='red')
这一行注释掉,程序就可以正常运行。