由于某种原因,我无法再让这段代码正常运行:
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 Enterprise 和 Python 3.7.3 上运行它。我尝试卸载并重新安装 scipy 和 scikit-learn。我尝试过早期版本的 scipy.我尝试卸载并重新安装 Python,但都没有解决问题。
Update:
所以它似乎也与 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')
效果很好。