赞
踩
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()
问题就解决啦
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。