简体   繁体   English

我收到此ValueError:新数组的总大小必须保持不变。 谁能解决?

[英]I am getting this ValueError: total size of new array must be unchanged error. Can anyone resolve?

I want to plot the data according to the classes given using Support Vector Machine. 我想根据使用支持向量机提供的类来绘制数据。 I am stuck in this code. 我陷入了这段代码。

Error is shown at -> Z = Z.reshape(XX.shape) 错误显示在-> Z = Z.reshape(XX.shape)

Please note the code is imported from sklearn-svm . 请注意,该代码是从sklearn-svm导入的。 When I try to change the datasets given to the application the above error is shown. 当我尝试更改提供给应用程序的数据集时,会显示上述错误。

    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn import svm,datasets

    iris = datasets.load_iris()
    X = iris.data[:, :2]  # we only take the first two features. We could
                             # avoid this ugly slicing by using a two-dim dataset
    Y = iris.target
    Y = np.array(Y)
    # figure number
    fignum = 1

    # fit the model
    for kernel in ('linear', 'poly', 'rbf'):
        clf = svm.SVC(kernel=kernel, gamma=2)
        clf.fit(X, Y)

        # plot the line, the points, and the nearest vectors to the plane
        plt.figure(fignum, figsize=(4, 3))
        plt.clf()

        plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=80,
                    facecolors='none', zorder=10)
        plt.scatter(X[:, 0], X[:, 1], c=Y, zorder=10, cmap=plt.cm.Paired)

        plt.axis('tight')
        x_min = 0
        x_max = 10
        y_min = 0
        y_max = 10

        XX, YY = np.mgrid[x_min:x_max:200j, y_min:y_max:200j]
        Z = clf.decision_function(np.c_[XX.ravel(), YY.ravel()])
        # Put the result into a color plot
        Z = Z.reshape(XX.shape)
        #print Z
        plt.figure(fignum, figsize=(4, 3))
        plt.pcolormesh(XX, YY, Z > 0, cmap=plt.cm.Paired)
        plt.contour(XX, YY, Z, colors=['k', 'k', 'k'], linestyles=['--', '-', '--'],levels=[-.5, 0, .5])
        plt.xlim(x_min, x_max)
        plt.ylim(y_min, y_max)

        plt.xticks(())
        plt.yticks(())
        fignum = fignum + 1
    plt.show()

XX and YY both have the shape (200, 200) , but your Z has shape (40000, 3) . XXYY都具有形状(200, 200) ,但是Z却具有形状(40000, 3) So XX and YY both contain 40000 values, while Z has 120000 values and can therefore not simply be reshaped to the shape of XX or YY as you would lose values. 因此XXYY都包含40000个值,而Z具有120000个值,因此不能简单地重塑为XXYY的形状,因为会丢失值。 The call to decision_function() does not return a matrix of shape (N_samples, 1) as you seem to expect. 如您所料,对decision_function()的调用不会返回形状矩阵(N_samples, 1) Instead, according to scikit documentation , it returns (N_samples, n_class * (n_class-1) / 2) . 相反,根据scikit文档 ,它返回(N_samples, n_class * (n_class-1) / 2) You can replace the call with predict() to get the (probably) desired result, ie 您可以将调用替换为predict()以获取(可能)所需的结果,即

Z = clf.predict(np.c_[XX.ravel(), YY.ravel()])

svm结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:新数组的总大小必须保持不变 - ValueError: total size of new array must be unchanged 重塑错误/ ValueError:新数组的总大小必须保持不变 - reshape Error/ValueError: total size of new array must be unchanged Scipy ValueError:新数组的总大小必须保持不变 - Scipy ValueError: Total size of new array must be unchanged ValueError:新数组的总大小必须保持不变(从keras重塑) - ValueError: total size of new array must be unchanged (Reshape from keras) np.reshape 返回错误“ValueError:新数组的总大小必须保持不变” - np.reshape returns error 'ValueError: total size of new array must be unchanged' Keras 中的 Segnet:新数组的总大小必须保持不变错误 - Segnet in Keras: total size of new array must be unchanged error 新阵列的总大小必须保持不变(netCDF) - Total Size of New Array Must Be Unchanged (netCDF) 新数组的总大小必须保持不变 - total size of new array must be unchanged python,修复损坏的npy文件。 ValueError:新数组的总大小必须保持不变 - python, fix broken npy file. ValueError: total size of new array must be unchanged 添加 MaxPooling 2D - ValueError:新数组的总大小必须不变 - Addition of MaxPooling 2D - ValueError: total size of new array must be unchanged
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM