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

做图时遇到内存错误

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()

做出的努力

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

最终采取的解决办法

修改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()

问题就解决啦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值