【问题标题】:Plotting a meshgrid绘制网格
【发布时间】:2021-07-11 21:01:16
【问题描述】:

我试图在X_test 数据上绘制meshgrid,但是当我运行代码时出现以下异常:

ValueError:连接轴的所有输入数组维度必须完全匹配,但沿维度 0,索引 0 处的数组大小为 196548,索引 1 处的数组大小为 14550

变量xx 与所有问题有关,但我看不到解决问题的方法。

classifier = BaggingClassifier(base_estimator = KNeighborsClassifier(),
                               max_samples = 10,
                               n_estimators = 100)

X0, X1 = X_test.iloc[:,0], X_test.iloc[:, 1]
xx, yy = make_meshgrid(X0, X1)


def make_meshgrid(x, y, h=0.02):
 
    x_min, x_max = x.min() - 1, x.max() + 1
    y_min, y_max = y.min() - 1, y.max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h), sparse=True)
    return xx, yy
 
def plot_contours(ax, clf, xx, yy, **params):
 
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    out = ax.contourf(xx, yy, Z, **params)
     
    return out

 
# Pass the data. make_meshgrid will automatically identify the min and max points to draw the grid
plot_contours(plt, classifier, xx, yy,
                  cmap=plt.cm.coolwarm,
                  alpha=0.8)

# plot the meshgrid
plt.scatter(X0, X1, cmap=plt.cm.coolwarm,s=20, edgecolors='k', alpha=0.2)

【问题讨论】:

  • 具体在哪里?请使用完整的错误跟踪更新您的问题。此外,问题与deep-learning 无关 - 请不要向无关标签发送垃圾邮件(已删除)。

标签: python matplotlib scikit-learn


【解决方案1】:

这部分导致问题:

xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h), sparse=True)

你得到那个错误是因为当你设置sparse=True时它没有默认你的两列,你可以比较一下:

_x,_y = np.meshgrid([0.1,0.2,0.3],[1,2])
len(_x.ravel()),len(_y.ravel())
(6, 6)

_x,_y = np.meshgrid([0.1,0.2,0.3],[1,2],sparse=True)
len(_x.ravel()),len(_y.ravel())
(3, 2)

不太确定绘制这么多点是否现实,所以如果你不返回稀疏:

def make_meshgrid(x, y, h=0.2):
 
    x_min, x_max = x.min() - 1, x.max() + 1
    y_min, y_max = y.min() - 1, y.max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))
    
    return xx, yy

def plot_contours(ax, clf, xx, yy, **params):
 
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    out = ax.contourf(xx, yy, Z, **params)
     
    return out

然后使用示例数据集,我们绘制:

from sklearn.ensemble import BaggingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=100, n_features=2,
                            n_informative=2, n_redundant=0,
                            random_state=0, shuffle=False)

classifier = BaggingClassifier(base_estimator = KNeighborsClassifier(),
                               max_samples = 10,
                               n_estimators = 100)

X_train = pd.DataFrame(X[:50,])
y_train = y[:50]

classifier.fit(X_train,y_train)

X_test = pd.DataFrame(X[:50,])
y_test = y[50:]
 
xx, yy = make_meshgrid(X_test.iloc[:,0], X_test.iloc[:, 1])

plot_contours(plt, classifier, xx, yy,
                  cmap=plt.cm.coolwarm,
                  alpha=0.8)

plt.scatter(X0, X1, cmap=plt.cm.coolwarm,s=20, edgecolors='k', alpha=0.2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-25
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 2011-05-10
    • 2016-02-01
    相关资源
    最近更新 更多