当前位置:   article > 正文

MemoryError: Unable to allocate 1.43 TiB for an array with shape (3700, 5300) and data type float64_meshgrid函数memoryerror

meshgrid函数memoryerror

做图时遇到内存错误

MemoryError: Unable to allocate 1.43 TiB for an array with shape (3700, 5300) and data type float64

源代码如下:

# 第二步 绘制决策边界
def plot_decision_boundary(pred_func):
    x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
    y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5    
    h = 0.01
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
 
# 用预测函数预测一下
    Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
 
# 然后画出图
    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)
 
from sklearn.linear_model import LogisticRegressionCV

# 训练逻辑回归分类器
clf = sklearn.linear_model.LogisticRegressionCV()
clf.fit(X, y)
 
# 画一下决策边界
#plot_decision_boundary(lambda x: clf.predict(x))
plt.figure(figsize=(16, 8))
plot_decision_boundary(clf.predict)
plt.title("Logistic Regression")
plt.show()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

做出的努力

搜很多但都是关于计算机内存设置的,结果并没有很有用,而且很麻烦

最终采取的解决办法

修改h,从0.01到1
解决问题的思路:因为大概了解到这个是关于计算机内存不够的原因,而 h 是关于步长设置,步长设置大一点整体计算量就会小一点

# 第二步 绘制决策边界
def plot_decision_boundary(pred_func):
    x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
    y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5    
    h = 0.01
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
 
# 用预测函数预测一下
    Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
 
# 然后画出图
    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)
 
from sklearn.linear_model import LogisticRegressionCV

# 训练逻辑回归分类器
clf = sklearn.linear_model.LogisticRegressionCV()
clf.fit(X, y)
 
# 画一下决策边界
#plot_decision_boundary(lambda x: clf.predict(x))
plt.figure(figsize=(16, 8))
plot_decision_boundary(clf.predict)
plt.title("Logistic Regression")
plt.show()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

问题就解决啦

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/311589
推荐阅读
相关标签
  

闽ICP备14008679号