gpt4 book ai didi

python - ValueError : illegal value in 4-th argument of internal None when running sklearn LinearRegression(). fit()

转载 作者:行者123 更新时间:2023-12-03 09:30:56 26 4
gpt4 key购买 nike

出于某种原因,我无法再让这段代码正常运行:

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,但这些都没有解决问题。
更新 :
所以它似乎也与 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')它工作正常。

最佳答案

似乎只有在使用 matplotlib 打印图形时才会发生这种情况,否则您可以根据需要多次运行拟合算法。
但是,如果您将数据类型从 float64 更改为 float32(Grzesik 答案),那么奇怪的是错误消失了。 对我来说感觉像个错误 为什么更改数据类型会影响 matplotlib 和 sklearn 中的 lapack_function 之间的交互?
与其说是一个答案,不如说是一个问题,但发现这些跨函数和数据类型的意外交互有点可怕。

import numpy as np
import sklearn
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt


def main(print_matplotlib=False,dtype=np.float64):
x = np.linspace(-3,3,100).astype(dtype)
print(x.dtype)
y = 2*np.random.rand(x.shape[0])*x + np.random.rand(x.shape[0])
x = x.reshape((-1,1))

reg=LinearRegression().fit(x,y)
print(reg.intercept_,reg.coef_)

yh = reg.predict(x)

if print_matplotlib:
plt.scatter(x,y)
plt.plot(x,yh)
plt.show()


没有绘图
if __name__ == "__main__":
np.random.seed(64)
main(print_matplotlib = False, dtype=np.float64)
np.random.seed(64)
main(print_matplotlib = False, dtype=np.float64)
pass

float64
0.5957165420019624 [0.91960601]
float64
0.5957165420019624 [0.91960601]

绘制 dtype = np.float64
if __name__ == "__main__":
np.random.seed(64)
main(print_matplotlib = True, dtype=np.float64)
np.random.seed(64)
main(print_matplotlib = True, dtype=np.float64)
pass

float64
0.5957165420019624 [0.91960601]
Plot 1
float64
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-52593a548324> in <module>
3 main(print_matplotlib = True)
4 np.random.seed(64)
----> 5 main(print_matplotlib = True)
6
7 pass

<ipython-input-1-11139051f2d3> in main(print_matplotlib, dtype)
11 x = x.reshape((-1,1))
12
---> 13 reg=LinearRegression().fit(x,y)
14 print(reg.intercept_,reg.coef_)
15

~\Anaconda3\lib\site-packages\sklearn\linear_model\_base.py in fit(self, X, y, sample_weight)
545 else:
546 self.coef_, self._residues, self.rank_, self.singular_ = \
--> 547 linalg.lstsq(X, y)
548 self.coef_ = self.coef_.T
549

~\AppData\Roaming\Python\Python37\site-packages\scipy\linalg\basic.py in lstsq(a, b, cond, overwrite_a, overwrite_b, check_finite, lapack_driver)
1249 if info < 0:
1250 raise ValueError('illegal value in %d-th argument of internal %s'
-> 1251 % (-info, lapack_driver))
1252 resids = np.asarray([], dtype=x.dtype)
1253 if m > n:

ValueError: illegal value in 4-th argument of internal None

绘制 dtype=np.float32
if __name__ == "__main__":
np.random.seed(64)
main(print_matplotlib = True, dtype=np.float32)
np.random.seed(64)
main(print_matplotlib = True, dtype=np.float32)
pass

Output 2

关于python - ValueError : illegal value in 4-th argument of internal None when running sklearn LinearRegression(). fit(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62561902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com