如何在 Matplotlib 中使用带有大数组的 meshgrid?

How to use meshgrid with large arrays in Matplotlib?

我在 sklearn 的 100x85 数组上训练了一个机器学习二进制 classifier。我希望能够改变数组中的 2 个特征,比如第 0 列和第 1 列,并生成等高线或曲面图,显示属于一个类别的预测概率如何在整个曲面上变化。

在我看来,我会使用类似以下的内容:

X = 100 x 85 数据数组用于训练集 clf = 受过训练的 2-class classifier

x = np.array(X)
y = np.array(X)

x[:,0] = np.linspace(0, 100, 100)
y[:,1] = np.linspace(0, 100, 100)
xx, yy = meshgrid(x,y)

下一步将使用

clf.predict_proba(<input arrays>)

然后进行绘图,但使用 meshgrid 会导致两个 8500x8500 矩阵无法在我的 classifier 中使用。

如何在网格中的每个点获得必要的 100x85 向量以将 pred_proba 与我的 classifier 一起使用?

感谢您提供的任何帮助。

正如@wflynny 上面所说,你需要给 np.meshgrid 两个一维数组。我们可以使用 X.shape 创建您的 xy 数组,如下所示:

X=np.zeros((100,85)) # just to get the right shape here

print X.shape
# (100, 85)

x=np.arange(X.shape[0])
y=np.arange(X.shape[1])

print x.shape
# (100,)
print y.shape
# (85,)

xx,yy=np.meshgrid(x,y,indexing='ij')

print xx.shape
#(100, 85)
print yy.shape
#(100, 85)